import { createContext, type Dispatch, type FC, type PropsWithChildren, type SetStateAction, useContext, useMemo, useState, } from 'react' import type { EditorSelection } from '@codemirror/state' export const EditorSelectionContext = createContext< | { editorSelection: EditorSelection | undefined setEditorSelection: Dispatch> } | undefined >(undefined) export const EditorSelectionProvider: FC = ({ children, }) => { const [editorSelection, setEditorSelection] = useState() const value = useMemo(() => { return { editorSelection, setEditorSelection } }, [editorSelection]) return ( {children} ) } export const useEditorSelectionContext = () => { const context = useContext(EditorSelectionContext) if (!context) { throw new Error( 'useEditorSelectionContext is only available inside EditorSelectionProvider' ) } return context }