2024-02-01 04:41:12 -05:00
|
|
|
import { createContext, FC, useContext, useMemo } from 'react'
|
2024-01-26 04:23:48 -05:00
|
|
|
import getMeta from '../../utils/meta'
|
|
|
|
import { User } from '../../../../types/user'
|
|
|
|
|
|
|
|
export const UserContext = createContext<User | undefined>(undefined)
|
|
|
|
|
|
|
|
export const UserProvider: FC = ({ children }) => {
|
2024-02-01 04:41:12 -05:00
|
|
|
const user = useMemo(() => getMeta('ol-user'), [])
|
2024-01-26 04:23:48 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|