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'
|
2024-10-17 14:07:48 -04:00
|
|
|
import { User } from '../../../../types/user'
|
2024-01-26 04:23:48 -05:00
|
|
|
|
2024-10-17 14:07:48 -04:00
|
|
|
export const UserContext = createContext<User | undefined>(undefined)
|
2024-01-26 04:23:48 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|