editor-context.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import {
  2. createContext,
  3. Dispatch,
  4. FC,
  5. SetStateAction,
  6. useCallback,
  7. useContext,
  8. useEffect,
  9. useMemo,
  10. useState,
  11. } from 'react'
  12. import useScopeValue from '../hooks/use-scope-value'
  13. import useBrowserWindow from '../hooks/use-browser-window'
  14. import { useIdeContext } from './ide-context'
  15. import { useProjectContext } from './project-context'
  16. import { useDetachContext } from './detach-context'
  17. import getMeta from '../../utils/meta'
  18. import { useUserContext } from './user-context'
  19. import { saveProjectSettings } from '@/features/editor-left-menu/utils/api'
  20. import { PermissionsLevel } from '@/features/ide-react/types/permissions'
  21. export const EditorContext = createContext<
  22. | {
  23. cobranding?: {
  24. logoImgUrl: string
  25. brandVariationName: string
  26. brandVariationId: number
  27. brandId: number
  28. brandVariationHomeUrl: string
  29. publishGuideHtml?: string
  30. partner?: string
  31. brandedMenu?: boolean
  32. submitBtnHtml?: string
  33. }
  34. hasPremiumCompile?: boolean
  35. loading?: boolean
  36. renameProject: (newName: string) => void
  37. setPermissionsLevel: (permissionsLevel: PermissionsLevel) => void
  38. showSymbolPalette?: boolean
  39. toggleSymbolPalette?: () => void
  40. insertSymbol?: (symbol: string) => void
  41. isProjectOwner: boolean
  42. isRestrictedTokenMember?: boolean
  43. permissionsLevel: 'readOnly' | 'readAndWrite' | 'owner'
  44. deactivateTutorial: (tutorial: string) => void
  45. inactiveTutorials: [string]
  46. currentPopup: string | null
  47. setCurrentPopup: Dispatch<SetStateAction<string | null>>
  48. writefullAdClicked: boolean
  49. setWritefullAdClicked: Dispatch<SetStateAction<boolean>>
  50. }
  51. | undefined
  52. >(undefined)
  53. export const EditorProvider: FC = ({ children }) => {
  54. const ide = useIdeContext()
  55. const { id: userId } = useUserContext()
  56. const { role } = useDetachContext()
  57. const { owner, features, _id: projectId } = useProjectContext()
  58. const cobranding = useMemo(() => {
  59. return (
  60. window.brandVariation && {
  61. logoImgUrl: window.brandVariation.logo_url,
  62. brandVariationName: window.brandVariation.name,
  63. brandVariationId: window.brandVariation.id,
  64. brandId: window.brandVariation.brand_id,
  65. brandVariationHomeUrl: window.brandVariation.home_url,
  66. publishGuideHtml: window.brandVariation.publish_guide_html,
  67. partner: window.brandVariation.partner,
  68. brandedMenu: window.brandVariation.branded_menu,
  69. submitBtnHtml: window.brandVariation.submit_button_html,
  70. }
  71. )
  72. }, [])
  73. const [loading] = useScopeValue('state.loading')
  74. const [projectName, setProjectName] = useScopeValue('project.name')
  75. const [permissionsLevel, setPermissionsLevel] =
  76. useScopeValue('permissionsLevel')
  77. const [showSymbolPalette] = useScopeValue('editor.showSymbolPalette')
  78. const [toggleSymbolPalette] = useScopeValue('editor.toggleSymbolPalette')
  79. const [inactiveTutorials, setInactiveTutorials] = useState(() =>
  80. getMeta('ol-inactiveTutorials', [])
  81. )
  82. const [writefullAdClicked, setWritefullAdClicked] = useState(false)
  83. const [currentPopup, setCurrentPopup] = useState<string | null>(null)
  84. const deactivateTutorial = useCallback(
  85. tutorialKey => {
  86. setInactiveTutorials([...inactiveTutorials, tutorialKey])
  87. },
  88. [inactiveTutorials]
  89. )
  90. useEffect(() => {
  91. if (ide?.socket) {
  92. ide.socket.on('projectNameUpdated', setProjectName)
  93. return () =>
  94. ide.socket.removeListener('projectNameUpdated', setProjectName)
  95. }
  96. }, [ide?.socket, setProjectName])
  97. const renameProject = useCallback(
  98. (newName: string) => {
  99. setProjectName((oldName: string) => {
  100. if (oldName !== newName) {
  101. saveProjectSettings(projectId, { name: newName }).catch(
  102. (response: any) => {
  103. setProjectName(oldName)
  104. const { data, status } = response
  105. if (status === 400) {
  106. return ide.showGenericMessageModal(
  107. 'Error renaming project',
  108. data
  109. )
  110. } else {
  111. return ide.showGenericMessageModal(
  112. 'Error renaming project',
  113. 'Please try again in a moment'
  114. )
  115. }
  116. }
  117. )
  118. }
  119. return newName
  120. })
  121. },
  122. [ide, setProjectName, projectId]
  123. )
  124. const { setTitle } = useBrowserWindow()
  125. useEffect(() => {
  126. const parts = []
  127. if (role === 'detached') {
  128. parts.push('[PDF]')
  129. }
  130. if (projectName) {
  131. parts.push(projectName)
  132. parts.push('-')
  133. }
  134. parts.push('Online LaTeX Editor')
  135. parts.push(window.ExposedSettings.appName)
  136. const title = parts.join(' ')
  137. setTitle(title)
  138. }, [projectName, setTitle, role])
  139. const insertSymbol = useCallback((symbol: string) => {
  140. window.dispatchEvent(
  141. new CustomEvent('editor:insert-symbol', {
  142. detail: symbol,
  143. })
  144. )
  145. }, [])
  146. const value = useMemo(
  147. () => ({
  148. cobranding,
  149. hasPremiumCompile: features?.compileGroup === 'priority',
  150. loading,
  151. renameProject,
  152. permissionsLevel,
  153. setPermissionsLevel,
  154. isProjectOwner: owner?._id === userId,
  155. isRestrictedTokenMember: getMeta('ol-isRestrictedTokenMember'),
  156. showSymbolPalette,
  157. toggleSymbolPalette,
  158. insertSymbol,
  159. inactiveTutorials,
  160. deactivateTutorial,
  161. currentPopup,
  162. setCurrentPopup,
  163. writefullAdClicked,
  164. setWritefullAdClicked,
  165. }),
  166. [
  167. cobranding,
  168. features?.compileGroup,
  169. owner,
  170. userId,
  171. loading,
  172. renameProject,
  173. permissionsLevel,
  174. setPermissionsLevel,
  175. showSymbolPalette,
  176. toggleSymbolPalette,
  177. insertSymbol,
  178. inactiveTutorials,
  179. deactivateTutorial,
  180. currentPopup,
  181. setCurrentPopup,
  182. writefullAdClicked,
  183. setWritefullAdClicked,
  184. ]
  185. )
  186. return (
  187. <EditorContext.Provider value={value}>{children}</EditorContext.Provider>
  188. )
  189. }
  190. export function useEditorContext() {
  191. const context = useContext(EditorContext)
  192. if (!context) {
  193. throw new Error('useEditorContext is only available inside EditorProvider')
  194. }
  195. return context
  196. }