2021-06-25 04:14:07 -04:00
|
|
|
import { createContext, useContext } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2022-03-31 07:22:36 -04:00
|
|
|
import getMeta from '../../utils/meta'
|
2021-06-25 04:14:07 -04:00
|
|
|
|
|
|
|
export const UserContext = createContext()
|
|
|
|
|
|
|
|
UserContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
2022-03-31 06:36:37 -04:00
|
|
|
isAdmin: PropTypes.boolean,
|
2021-10-06 04:33:24 -04:00
|
|
|
email: PropTypes.string,
|
2021-06-25 04:14:07 -04:00
|
|
|
allowedFreeTrial: PropTypes.boolean,
|
|
|
|
first_name: PropTypes.string,
|
|
|
|
last_name: PropTypes.string,
|
2022-07-07 04:00:50 -04:00
|
|
|
alphaProgram: PropTypes.boolean,
|
|
|
|
betaProgram: PropTypes.boolean,
|
2022-09-21 06:02:37 -04:00
|
|
|
labsProgram: PropTypes.boolean,
|
2022-04-22 09:49:26 -04:00
|
|
|
features: PropTypes.shape({
|
|
|
|
dropbox: PropTypes.boolean,
|
|
|
|
github: PropTypes.boolean,
|
|
|
|
mendeley: PropTypes.boolean,
|
|
|
|
zotero: PropTypes.boolean,
|
|
|
|
references: PropTypes.boolean,
|
2022-12-20 08:01:54 -05:00
|
|
|
compileTimeout: PropTypes.number,
|
2022-04-22 09:49:26 -04:00
|
|
|
}),
|
|
|
|
refProviders: PropTypes.shape({
|
|
|
|
mendeley: PropTypes.any,
|
|
|
|
zotero: PropTypes.any,
|
|
|
|
}),
|
2021-06-25 04:14:07 -04:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
export function UserProvider({ children }) {
|
2022-03-31 07:22:36 -04:00
|
|
|
const user = getMeta('ol-user')
|
2021-06-25 04:14:07 -04:00
|
|
|
|
|
|
|
return <UserContext.Provider value={user}>{children}</UserContext.Provider>
|
|
|
|
}
|
|
|
|
|
|
|
|
UserProvider.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useUserContext(propTypes) {
|
|
|
|
const data = useContext(UserContext)
|
2022-04-22 09:49:26 -04:00
|
|
|
if (!data) {
|
|
|
|
throw new Error(
|
|
|
|
'useUserContext is only available inside UserContext, or `ol-user` meta is not defined'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-25 04:14:07 -04:00
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'UserContext.Provider')
|
|
|
|
return data
|
|
|
|
}
|