editor-context.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import { useModalsContext } from '@/features/ide-react/context/modals-context'
  22. type writefullAdButtons = '' | 'try-it' | 'log-in'
  23. export const EditorContext = createContext<
  24. | {
  25. cobranding?: {
  26. logoImgUrl: string
  27. brandVariationName: string
  28. brandVariationId: number
  29. brandId: number
  30. brandVariationHomeUrl: string
  31. publishGuideHtml?: string
  32. partner?: string
  33. brandedMenu?: boolean
  34. submitBtnHtml?: string
  35. }
  36. hasPremiumCompile?: boolean
  37. loading?: boolean
  38. renameProject: (newName: string) => void
  39. setPermissionsLevel: (permissionsLevel: PermissionsLevel) => void
  40. showSymbolPalette?: boolean
  41. toggleSymbolPalette?: () => void
  42. insertSymbol?: (symbol: string) => void
  43. isProjectOwner: boolean
  44. isRestrictedTokenMember?: boolean
  45. permissionsLevel: 'readOnly' | 'readAndWrite' | 'owner'
  46. deactivateTutorial: (tutorial: string) => void
  47. inactiveTutorials: string[]
  48. currentPopup: string | null
  49. setCurrentPopup: Dispatch<SetStateAction<string | null>>
  50. writefullAdClicked: writefullAdButtons
  51. setWritefullAdClicked: Dispatch<SetStateAction<writefullAdButtons>>
  52. setOutOfSync: (value: boolean) => void
  53. }
  54. | undefined
  55. >(undefined)
  56. export const EditorProvider: FC = ({ children }) => {
  57. const ide = useIdeContext()
  58. const { id: userId } = useUserContext()
  59. const { role } = useDetachContext()
  60. const { showGenericMessageModal } = useModalsContext()
  61. const { owner, features, _id: projectId } = useProjectContext()
  62. const cobranding = useMemo(() => {
  63. const brandVariation = getMeta('ol-brandVariation')
  64. return (
  65. brandVariation && {
  66. logoImgUrl: brandVariation.logo_url,
  67. brandVariationName: brandVariation.name,
  68. brandVariationId: brandVariation.id,
  69. brandId: brandVariation.brand_id,
  70. brandVariationHomeUrl: brandVariation.home_url,
  71. publishGuideHtml: brandVariation.publish_guide_html,
  72. partner: brandVariation.partner,
  73. brandedMenu: brandVariation.branded_menu,
  74. submitBtnHtml: brandVariation.submit_button_html,
  75. }
  76. )
  77. }, [])
  78. const [loading] = useScopeValue('state.loading')
  79. const [projectName, setProjectName] = useScopeValue('project.name')
  80. const [permissionsLevel, setPermissionsLevel] =
  81. useScopeValue('permissionsLevel')
  82. const [outOfSync, setOutOfSync] = useState(false)
  83. const [showSymbolPalette] = useScopeValue('editor.showSymbolPalette')
  84. const [toggleSymbolPalette] = useScopeValue('editor.toggleSymbolPalette')
  85. const [inactiveTutorials, setInactiveTutorials] = useState(
  86. () => getMeta('ol-inactiveTutorials') || []
  87. )
  88. const [writefullAdClicked, setWritefullAdClicked] =
  89. useState<writefullAdButtons>('')
  90. const [currentPopup, setCurrentPopup] = useState<string | null>(null)
  91. const deactivateTutorial = useCallback(
  92. tutorialKey => {
  93. setInactiveTutorials([...inactiveTutorials, tutorialKey])
  94. },
  95. [inactiveTutorials]
  96. )
  97. useEffect(() => {
  98. if (ide?.socket) {
  99. ide.socket.on('projectNameUpdated', setProjectName)
  100. return () =>
  101. ide.socket.removeListener('projectNameUpdated', setProjectName)
  102. }
  103. }, [ide?.socket, setProjectName])
  104. const renameProject = useCallback(
  105. (newName: string) => {
  106. setProjectName((oldName: string) => {
  107. if (oldName !== newName) {
  108. saveProjectSettings(projectId, { name: newName }).catch(
  109. (response: any) => {
  110. setProjectName(oldName)
  111. const { data, status } = response
  112. showGenericMessageModal(
  113. 'Error renaming project',
  114. status === 400 ? data : 'Please try again in a moment'
  115. )
  116. }
  117. )
  118. }
  119. return newName
  120. })
  121. },
  122. [setProjectName, projectId, showGenericMessageModal]
  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(getMeta('ol-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: outOfSync ? 'readOnly' : 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. setOutOfSync,
  166. }),
  167. [
  168. cobranding,
  169. features?.compileGroup,
  170. owner,
  171. userId,
  172. loading,
  173. renameProject,
  174. permissionsLevel,
  175. setPermissionsLevel,
  176. showSymbolPalette,
  177. toggleSymbolPalette,
  178. insertSymbol,
  179. inactiveTutorials,
  180. deactivateTutorial,
  181. currentPopup,
  182. setCurrentPopup,
  183. writefullAdClicked,
  184. setWritefullAdClicked,
  185. outOfSync,
  186. setOutOfSync,
  187. ]
  188. )
  189. return (
  190. <EditorContext.Provider value={value}>{children}</EditorContext.Provider>
  191. )
  192. }
  193. export function useEditorContext() {
  194. const context = useContext(EditorContext)
  195. if (!context) {
  196. throw new Error('useEditorContext is only available inside EditorProvider')
  197. }
  198. return context
  199. }