application-context.js 1008 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React, { createContext, useContext } from 'react'
  2. import PropTypes from 'prop-types'
  3. import ExposedSettings from '../../main/exposed-settings'
  4. export const ApplicationContext = createContext()
  5. ApplicationContext.Provider.propTypes = {
  6. value: PropTypes.shape({
  7. user: PropTypes.shape({
  8. id: PropTypes.string.isRequired
  9. }),
  10. exposedSettings: PropTypes.shape({
  11. enableSubscriptions: PropTypes.bool
  12. })
  13. })
  14. }
  15. export function ApplicationProvider({ children }) {
  16. const applicationContextValue = {
  17. user: window.user,
  18. exposedSettings: ExposedSettings
  19. }
  20. return (
  21. <ApplicationContext.Provider value={applicationContextValue}>
  22. {children}
  23. </ApplicationContext.Provider>
  24. )
  25. }
  26. ApplicationProvider.propTypes = {
  27. children: PropTypes.any
  28. }
  29. export function useApplicationContext(propTypes) {
  30. const data = useContext(ApplicationContext)
  31. PropTypes.checkPropTypes(
  32. propTypes,
  33. data,
  34. 'data',
  35. 'ApplicationContext.Provider'
  36. )
  37. return data
  38. }