overleaf/services/web/frontend/js/shared/context/application-context.js
Jakob Ackermann f5942f1a7b Merge pull request #4098 from overleaf/msm-fix-application-provider-required-user
Fixed `isRequired` usages for `ApplicationProvider.value.user`

GitOrigin-RevId: 0f3db77fa5da1cb0aec29ef112d1044173df88e0
2021-05-28 02:08:46 +00:00

46 lines
1 KiB
JavaScript

import React, { createContext, useContext } 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 applicationContextValue = {
gitBridgePublicBaseUrl: window.gitBridgePublicBaseUrl,
}
if (window.user.id) {
applicationContextValue.user = window.user
}
return (
<ApplicationContext.Provider value={applicationContextValue}>
{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
}