project-context.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { createContext, useContext, useMemo } from 'react'
  2. import PropTypes from 'prop-types'
  3. import useScopeValue from '../hooks/use-scope-value'
  4. import getMeta from '@/utils/meta'
  5. const ProjectContext = createContext()
  6. export const projectShape = {
  7. _id: PropTypes.string.isRequired,
  8. name: PropTypes.string.isRequired,
  9. rootDocId: PropTypes.string,
  10. members: PropTypes.arrayOf(
  11. PropTypes.shape({
  12. _id: PropTypes.string.isRequired,
  13. })
  14. ),
  15. invites: PropTypes.arrayOf(
  16. PropTypes.shape({
  17. _id: PropTypes.string.isRequired,
  18. })
  19. ),
  20. features: PropTypes.shape({
  21. collaborators: PropTypes.number,
  22. compileGroup: PropTypes.oneOf(['alpha', 'standard', 'priority']),
  23. trackChangesVisible: PropTypes.bool,
  24. references: PropTypes.bool,
  25. mendeley: PropTypes.bool,
  26. zotero: PropTypes.bool,
  27. versioning: PropTypes.bool,
  28. gitBridge: PropTypes.bool,
  29. }),
  30. publicAccessLevel: PropTypes.string,
  31. owner: PropTypes.shape({
  32. _id: PropTypes.string.isRequired,
  33. email: PropTypes.string.isRequired,
  34. }),
  35. useNewCompileTimeoutUI: PropTypes.string,
  36. tags: PropTypes.arrayOf(
  37. PropTypes.shape({
  38. _id: PropTypes.string.isRequired,
  39. name: PropTypes.string.isRequired,
  40. color: PropTypes.string,
  41. })
  42. ).isRequired,
  43. }
  44. ProjectContext.Provider.propTypes = {
  45. value: PropTypes.shape(projectShape),
  46. }
  47. export function useProjectContext(propTypes) {
  48. const context = useContext(ProjectContext)
  49. if (!context) {
  50. throw new Error(
  51. 'useProjectContext is only available inside ProjectProvider'
  52. )
  53. }
  54. PropTypes.checkPropTypes(
  55. propTypes,
  56. context,
  57. 'data',
  58. 'ProjectContext.Provider'
  59. )
  60. return context
  61. }
  62. // when the provider is created the project is still not added to the Angular
  63. // scope. A few props are populated to prevent errors in existing React
  64. // components
  65. const projectFallback = {
  66. _id: window.project_id,
  67. name: '',
  68. features: {},
  69. }
  70. export function ProjectProvider({ children }) {
  71. const [project] = useScopeValue('project', true)
  72. const {
  73. _id,
  74. name,
  75. rootDoc_id: rootDocId,
  76. members,
  77. invites,
  78. features,
  79. publicAccesLevel: publicAccessLevel,
  80. owner,
  81. showNewCompileTimeoutUI,
  82. } = project || projectFallback
  83. const tags = useMemo(
  84. () =>
  85. getMeta('ol-projectTags', [])
  86. // `tag.name` data may be null for some old users
  87. .map(tag => ({ ...tag, name: tag.name ?? '' })),
  88. []
  89. )
  90. // temporary override for new compile timeout
  91. const forceNewCompileTimeout = new URLSearchParams(
  92. window.location.search
  93. ).get('force_new_compile_timeout')
  94. const newCompileTimeoutOverride =
  95. forceNewCompileTimeout === 'active'
  96. ? 'active'
  97. : forceNewCompileTimeout === 'changing'
  98. ? 'changing'
  99. : undefined
  100. const value = useMemo(() => {
  101. return {
  102. _id,
  103. name,
  104. rootDocId,
  105. members,
  106. invites,
  107. features,
  108. publicAccessLevel,
  109. owner,
  110. showNewCompileTimeoutUI:
  111. newCompileTimeoutOverride || showNewCompileTimeoutUI,
  112. tags,
  113. }
  114. }, [
  115. _id,
  116. name,
  117. rootDocId,
  118. members,
  119. invites,
  120. features,
  121. publicAccessLevel,
  122. owner,
  123. showNewCompileTimeoutUI,
  124. newCompileTimeoutOverride,
  125. tags,
  126. ])
  127. return (
  128. <ProjectContext.Provider value={value}>{children}</ProjectContext.Provider>
  129. )
  130. }
  131. ProjectProvider.propTypes = {
  132. children: PropTypes.any,
  133. }