overleaf/services/web/frontend/js/shared/context/user-context.js
Alf Eaton 845b2fbc04 Move useScopeValue hook to shared hooks folder and wrap setValue in a function (#5386)
GitOrigin-RevId: b1a6a4e871af3b52fb3d100a83b479c834cb75ca
2021-10-11 08:03:29 +00:00

33 lines
867 B
JavaScript

import { createContext, useContext } from 'react'
import PropTypes from 'prop-types'
import useScopeValue from '../hooks/use-scope-value'
export const UserContext = createContext()
UserContext.Provider.propTypes = {
value: PropTypes.shape({
user: PropTypes.shape({
id: PropTypes.string,
email: PropTypes.string,
allowedFreeTrial: PropTypes.boolean,
first_name: PropTypes.string,
last_name: PropTypes.string,
}),
}),
}
export function UserProvider({ children }) {
const [user] = useScopeValue('user', true)
return <UserContext.Provider value={user}>{children}</UserContext.Provider>
}
UserProvider.propTypes = {
children: PropTypes.any,
}
export function useUserContext(propTypes) {
const data = useContext(UserContext)
PropTypes.checkPropTypes(propTypes, data, 'data', 'UserContext.Provider')
return data
}