| | import React, { createContext, useContext, useState, useMemo } from 'react'; |
| |
|
| | |
| | |
| | |
| | |
| | interface MutationContextType { |
| | isMutating: boolean; |
| | setIsMutating: React.Dispatch<React.SetStateAction<boolean>>; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | interface CodeContextType { |
| | currentCode?: string; |
| | setCurrentCode: React.Dispatch<React.SetStateAction<string | undefined>>; |
| | } |
| |
|
| | const MutationContext = createContext<MutationContextType | undefined>(undefined); |
| | const CodeContext = createContext<CodeContextType | undefined>(undefined); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | export function EditorProvider({ children }: { children: React.ReactNode }) { |
| | const [isMutating, setIsMutating] = useState(false); |
| | const [currentCode, setCurrentCode] = useState<string | undefined>(); |
| |
|
| | const mutationValue = useMemo(() => ({ isMutating, setIsMutating }), [isMutating]); |
| | const codeValue = useMemo(() => ({ currentCode, setCurrentCode }), [currentCode]); |
| |
|
| | return ( |
| | <MutationContext.Provider value={mutationValue}> |
| | <CodeContext.Provider value={codeValue}>{children}</CodeContext.Provider> |
| | </MutationContext.Provider> |
| | ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function useMutationState() { |
| | const context = useContext(MutationContext); |
| | if (context === undefined) { |
| | throw new Error('useMutationState must be used within an EditorProvider'); |
| | } |
| | return context; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function useCodeState() { |
| | const context = useContext(CodeContext); |
| | if (context === undefined) { |
| | throw new Error('useCodeState must be used within an EditorProvider'); |
| | } |
| | return context; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function useEditorContext() { |
| | const mutation = useMutationState(); |
| | const code = useCodeState(); |
| | return { ...mutation, ...code }; |
| | } |
| |
|