mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
0cde5be165
Convert React context providers to TypeScript [don't squash!] GitOrigin-RevId: d92a91798286978410956ab791d73c17c5086d86
41 lines
932 B
TypeScript
41 lines
932 B
TypeScript
import { createContext, FC, useContext, useMemo } from 'react'
|
|
import getMeta from '../../utils/meta'
|
|
|
|
type SplitTestVariants = Record<string, any>
|
|
type SplitTestInfo = Record<string, any>
|
|
|
|
export const SplitTestContext = createContext<
|
|
| {
|
|
splitTestVariants: SplitTestVariants
|
|
splitTestInfo: SplitTestInfo
|
|
}
|
|
| undefined
|
|
>(undefined)
|
|
|
|
export const SplitTestProvider: FC = ({ children }) => {
|
|
const value = useMemo(
|
|
() => ({
|
|
splitTestVariants: getMeta('ol-splitTestVariants') || {},
|
|
splitTestInfo: getMeta('ol-splitTestInfo') || {},
|
|
}),
|
|
[]
|
|
)
|
|
|
|
return (
|
|
<SplitTestContext.Provider value={value}>
|
|
{children}
|
|
</SplitTestContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useSplitTestContext() {
|
|
const context = useContext(SplitTestContext)
|
|
|
|
if (!context) {
|
|
throw new Error(
|
|
'useSplitTestContext is only available within SplitTestProvider'
|
|
)
|
|
}
|
|
|
|
return context
|
|
}
|