mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-02 12:49:33 -05:00
c443322a41
GitOrigin-RevId: 299ed9d568650ce37edba87643112d1cd6d12fd4
23 lines
648 B
TypeScript
23 lines
648 B
TypeScript
import { createContext, FC, useContext, useMemo } from 'react'
|
|
import getMeta from '../../utils/meta'
|
|
import { User } from '../../../../types/user'
|
|
|
|
export const UserContext = createContext<User | undefined>(undefined)
|
|
|
|
export const UserProvider: FC = ({ children }) => {
|
|
const user = useMemo(() => getMeta('ol-user'), [])
|
|
|
|
return <UserContext.Provider value={user}>{children}</UserContext.Provider>
|
|
}
|
|
|
|
export function useUserContext() {
|
|
const context = useContext(UserContext)
|
|
|
|
if (!context) {
|
|
throw new Error(
|
|
'useUserContext is only available inside UserContext, or `ol-user` meta is not defined'
|
|
)
|
|
}
|
|
|
|
return context
|
|
}
|