overleaf-cep/services/web/frontend/js/features/ide-react/context/editor-open-doc-context.tsx
Antoine Clausse 07b1701b72 [web] Convert EditorProviders and some test files to Typescript (#26512)
* Rename files to tsx

* Update types

* Remove props that aren't typed

* Add `LayoutContextOwnStates`

* Use `LayoutContextOwnStates`

* Ignore ts errors about `SocketIOMock`

* Address comments: remove `satisfies`, update `BroadcastChannel` fixture

* Add types to `makeEditorOpenDocProvider`. Update `openDocId`->`currentDocumentId`

* misc.

* Type sockets as `SocketIOMock & Socket`

* Fix remaining typing errors

* Fix type of `ideReactContextValue`

GitOrigin-RevId: 2734ac707517d56c452b0bf06ea3438f947a64be
2025-07-07 08:05:40 +00:00

66 lines
1.8 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'
export interface EditorOpenDocContextState {
currentDocumentId: DocId | null
openDocName: string | null
currentDocument: DocumentContainer | null
}
interface EditorOpenDocContextValue extends EditorOpenDocContextState {
setCurrentDocumentId: Dispatch<SetStateAction<DocId | null>>
setOpenDocName: Dispatch<SetStateAction<string | 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
}