overleaf/services/web/frontend/js/shared/context/application-context.js
Miguel Serrano 77c35e3715 Merge pull request #3633 from overleaf/msm-react-context-validation
Added PropTypes validation to react context

GitOrigin-RevId: 86950bdacf366035d1cfd923c7e7674d543b380f
2021-02-11 03:04:33 +00:00

43 lines
1,008 B
JavaScript

import React, { createContext, useContext } from 'react'
import PropTypes from 'prop-types'
import ExposedSettings from '../../main/exposed-settings'
export const ApplicationContext = createContext()
ApplicationContext.Provider.propTypes = {
value: PropTypes.shape({
user: PropTypes.shape({
id: PropTypes.string.isRequired
}),
exposedSettings: PropTypes.shape({
enableSubscriptions: PropTypes.bool
})
})
}
export function ApplicationProvider({ children }) {
const applicationContextValue = {
user: window.user,
exposedSettings: ExposedSettings
}
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
}