Files
overleaf-cep/services/web/frontend/js/features/ide-react/context/editor-open-doc-context.tsx
Tim Down 409d5f7b3e Merge pull request #26326 from overleaf/td-clean-up-scope-store
Create separate window.overleaf.unstable.store based on React context values

GitOrigin-RevId: 68f070a6fc5e2965a82720024d91b16fa622b28b
2025-07-02 08:05:53 +00:00

64 lines
1.7 KiB
TypeScript

import {
createContext,
Dispatch,
FC,
PropsWithChildren,
SetStateAction,
useContext,
useState,
} from 'react'
import { DocId } from '../../../../../types/project-settings'
import useExposedState from '@/shared/hooks/use-exposed-state'
import { DocumentContainer } from '@/features/ide-react/editor/document-container'
type EditorOpenDocContextValue = {
currentDocumentId: DocId | null
setCurrentDocumentId: Dispatch<SetStateAction<DocId | null>>
openDocName: string | null
setOpenDocName: Dispatch<SetStateAction<string | null>>
currentDocument: DocumentContainer | null
setCurrentDocument: Dispatch<SetStateAction<DocumentContainer | null>>
}
export const EditorOpenDocContext = createContext<
EditorOpenDocContextValue | undefined
>(undefined)
export const EditorOpenDocProvider: FC<PropsWithChildren> = ({ children }) => {
const [currentDocumentId, setCurrentDocumentId] =
useExposedState<DocId | null>(null, 'editor.open_doc_id')
const [openDocName, setOpenDocName] = useExposedState<string | null>(
null,
'editor.open_doc_name'
)
const [currentDocument, setCurrentDocument] =
useState<DocumentContainer | null>(null)
const value = {
currentDocumentId,
setCurrentDocumentId,
openDocName,
setOpenDocName,
currentDocument,
setCurrentDocument,
}
return (
<EditorOpenDocContext.Provider value={value}>
{children}
</EditorOpenDocContext.Provider>
)
}
export const useEditorOpenDocContext = (): EditorOpenDocContextValue => {
const context = useContext(EditorOpenDocContext)
if (!context) {
throw new Error(
'useEditorOpenDocContext is only available inside EditorOpenDocContext.Provider'
)
}
return context
}