mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-02 15:23:03 -05:00
0cde5be165
Convert React context providers to TypeScript [don't squash!] GitOrigin-RevId: d92a91798286978410956ab791d73c17c5086d86
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { createContext, FC, useContext, useState } from 'react'
|
|
|
|
type ContextMenuCoords = { top: number; left: number }
|
|
|
|
const FileTreeMainContext = createContext<
|
|
| {
|
|
refProviders: object
|
|
reindexReferences: () => void
|
|
setRefProviderEnabled: (provider: string, value: boolean) => void
|
|
setStartedFreeTrial: (value: boolean) => void
|
|
contextMenuCoords: ContextMenuCoords | null
|
|
setContextMenuCoords: (value: ContextMenuCoords | null) => void
|
|
}
|
|
| undefined
|
|
>(undefined)
|
|
|
|
export function useFileTreeMainContext() {
|
|
const context = useContext(FileTreeMainContext)
|
|
|
|
if (!context) {
|
|
throw new Error(
|
|
'useFileTreeMainContext is only available inside FileTreeMainProvider'
|
|
)
|
|
}
|
|
|
|
return context
|
|
}
|
|
|
|
export const FileTreeMainProvider: FC<{
|
|
reindexReferences: () => void
|
|
refProviders: object
|
|
setRefProviderEnabled: (provider: string, value: boolean) => void
|
|
setStartedFreeTrial: (value: boolean) => void
|
|
}> = ({
|
|
refProviders,
|
|
reindexReferences,
|
|
setRefProviderEnabled,
|
|
setStartedFreeTrial,
|
|
children,
|
|
}) => {
|
|
const [contextMenuCoords, setContextMenuCoords] =
|
|
useState<ContextMenuCoords | null>(null)
|
|
|
|
return (
|
|
<FileTreeMainContext.Provider
|
|
value={{
|
|
refProviders,
|
|
reindexReferences,
|
|
setRefProviderEnabled,
|
|
setStartedFreeTrial,
|
|
contextMenuCoords,
|
|
setContextMenuCoords,
|
|
}}
|
|
>
|
|
{children}
|
|
</FileTreeMainContext.Provider>
|
|
)
|
|
}
|