Files
overleaf-cep/services/web/frontend/js/shared/context/application-context.js
T
Alf Eaton 7c97f8ab6e Switch to new JSX runtime (#4225)
* Use new JSX runtime and update Babel Node target
* Update .eslintrc
* Remove React imports

GitOrigin-RevId: 559de0267f8f2934c56a860ea8701bb522aa861a
2021-06-24 02:06:59 +00:00

51 lines
1.1 KiB
JavaScript

import { createContext, useContext, useMemo } from 'react'
import PropTypes from 'prop-types'
export const ApplicationContext = createContext()
ApplicationContext.Provider.propTypes = {
value: PropTypes.shape({
user: PropTypes.shape({
id: PropTypes.string.isRequired,
firstName: PropTypes.string,
lastName: PropTypes.string,
}),
gitBridgePublicBaseUrl: PropTypes.string.isRequired,
}),
}
export function ApplicationProvider({ children }) {
const value = useMemo(() => {
const value = {
gitBridgePublicBaseUrl: window.gitBridgePublicBaseUrl,
}
if (window.user.id) {
value.user = window.user
}
return value
}, [])
return (
<ApplicationContext.Provider value={value}>
{children}
</ApplicationContext.Provider>
)
}
ApplicationProvider.propTypes = {
children: PropTypes.any,
}
export function useApplicationContext(propTypes) {
const data = useContext(ApplicationContext)
PropTypes.checkPropTypes(
propTypes,
data,
'data',
'ApplicationContext.Provider'
)
return data
}