overleaf/services/web/frontend/js/shared/context/user-context.tsx
Eric Mc Sween b2d991d74a Merge pull request #21212 from overleaf/revert-20969-ac-subscription-layout-react
Revert "[web] Set Subscription pages to have `layout-react`"

GitOrigin-RevId: eaf3a651160e278adcb103864c5ea81ea3e35aa7
2024-10-18 08:06:05 +00:00

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
}