editor-context.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. import { WritefullAPI } from './types/writefull-instance'
  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. renameProject: (newName: string) => void
  38. setPermissionsLevel: (permissionsLevel: PermissionsLevel) => void
  39. showSymbolPalette?: boolean
  40. toggleSymbolPalette?: () => void
  41. insertSymbol?: (symbol: string) => void
  42. isProjectOwner: boolean
  43. isRestrictedTokenMember?: boolean
  44. isPendingEditor: boolean
  45. permissionsLevel: PermissionsLevel
  46. deactivateTutorial: (tutorial: string) => void
  47. inactiveTutorials: string[]
  48. currentPopup: string | null
  49. setCurrentPopup: Dispatch<SetStateAction<string | null>>
  50. setOutOfSync: (value: boolean) => void
  51. assistantUpgraded: boolean
  52. setAssistantUpgraded: (value: boolean) => void
  53. hasPremiumSuggestion: boolean
  54. setHasPremiumSuggestion: (value: boolean) => void
  55. setPremiumSuggestionResetDate: (date: Date) => void
  56. premiumSuggestionResetDate: Date
  57. writefullInstance: WritefullAPI | null
  58. setWritefullInstance: (instance: WritefullAPI) => void
  59. }
  60. | undefined
  61. >(undefined)
  62. export const EditorProvider: FC = ({ children }) => {
  63. const { socket } = useIdeContext()
  64. const { id: userId, featureUsage } = useUserContext()
  65. const { role } = useDetachContext()
  66. const { showGenericMessageModal } = useModalsContext()
  67. const { owner, features, _id: projectId, members } = useProjectContext()
  68. const cobranding = useMemo(() => {
  69. const brandVariation = getMeta('ol-brandVariation')
  70. return (
  71. brandVariation && {
  72. logoImgUrl: brandVariation.logo_url,
  73. brandVariationName: brandVariation.name,
  74. brandVariationId: brandVariation.id,
  75. brandId: brandVariation.brand_id,
  76. brandVariationHomeUrl: brandVariation.home_url,
  77. publishGuideHtml: brandVariation.publish_guide_html,
  78. partner: brandVariation.partner,
  79. brandedMenu: brandVariation.branded_menu,
  80. submitBtnHtml: brandVariation.submit_button_html,
  81. }
  82. )
  83. }, [])
  84. const [projectName, setProjectName] = useScopeValue('project.name')
  85. const [permissionsLevel, setPermissionsLevel] =
  86. useScopeValue('permissionsLevel')
  87. const [outOfSync, setOutOfSync] = useState(false)
  88. const [showSymbolPalette] = useScopeValue('editor.showSymbolPalette')
  89. const [toggleSymbolPalette] = useScopeValue('editor.toggleSymbolPalette')
  90. const [inactiveTutorials, setInactiveTutorials] = useState(
  91. () => getMeta('ol-inactiveTutorials') || []
  92. )
  93. const [currentPopup, setCurrentPopup] = useState<string | null>(null)
  94. const [assistantUpgraded, setAssistantUpgraded] = useState(false)
  95. const [hasPremiumSuggestion, setHasPremiumSuggestion] = useState<boolean>(
  96. () => {
  97. return Boolean(
  98. featureUsage?.aiErrorAssistant &&
  99. featureUsage?.aiErrorAssistant.remainingUsage > 0
  100. )
  101. }
  102. )
  103. const [premiumSuggestionResetDate, setPremiumSuggestionResetDate] =
  104. useState<Date>(() => {
  105. return featureUsage?.aiErrorAssistant?.resetDate
  106. ? new Date(featureUsage.aiErrorAssistant.resetDate)
  107. : new Date()
  108. })
  109. const isPendingEditor = useMemo(
  110. () =>
  111. members?.some(member => member._id === userId && member.pendingEditor),
  112. [members, userId]
  113. )
  114. const deactivateTutorial = useCallback(
  115. tutorialKey => {
  116. setInactiveTutorials([...inactiveTutorials, tutorialKey])
  117. },
  118. [inactiveTutorials]
  119. )
  120. useEffect(() => {
  121. if (socket) {
  122. socket.on('projectNameUpdated', setProjectName)
  123. return () => socket.removeListener('projectNameUpdated', setProjectName)
  124. }
  125. }, [socket, setProjectName])
  126. const renameProject = useCallback(
  127. (newName: string) => {
  128. setProjectName((oldName: string) => {
  129. if (oldName !== newName) {
  130. saveProjectSettings(projectId, { name: newName }).catch(
  131. (response: any) => {
  132. setProjectName(oldName)
  133. const { data, status } = response
  134. showGenericMessageModal(
  135. 'Error renaming project',
  136. status === 400 ? data : 'Please try again in a moment'
  137. )
  138. }
  139. )
  140. }
  141. return newName
  142. })
  143. },
  144. [setProjectName, projectId, showGenericMessageModal]
  145. )
  146. const { setTitle } = useBrowserWindow()
  147. useEffect(() => {
  148. const parts = []
  149. if (role === 'detached') {
  150. parts.push('[PDF]')
  151. }
  152. if (projectName) {
  153. parts.push(projectName)
  154. parts.push('-')
  155. }
  156. parts.push('Online LaTeX Editor')
  157. parts.push(getMeta('ol-ExposedSettings').appName)
  158. const title = parts.join(' ')
  159. setTitle(title)
  160. }, [projectName, setTitle, role])
  161. const insertSymbol = useCallback((symbol: string) => {
  162. window.dispatchEvent(
  163. new CustomEvent('editor:insert-symbol', {
  164. detail: symbol,
  165. })
  166. )
  167. }, [])
  168. const [writefullInstance, setWritefullInstance] =
  169. useState<WritefullAPI | null>(null)
  170. const value = useMemo(
  171. () => ({
  172. cobranding,
  173. hasPremiumCompile: features?.compileGroup === 'priority',
  174. renameProject,
  175. permissionsLevel: outOfSync ? 'readOnly' : permissionsLevel,
  176. setPermissionsLevel,
  177. isProjectOwner: owner?._id === userId,
  178. isRestrictedTokenMember: getMeta('ol-isRestrictedTokenMember'),
  179. isPendingEditor,
  180. showSymbolPalette,
  181. toggleSymbolPalette,
  182. insertSymbol,
  183. inactiveTutorials,
  184. deactivateTutorial,
  185. currentPopup,
  186. setCurrentPopup,
  187. setOutOfSync,
  188. hasPremiumSuggestion,
  189. setHasPremiumSuggestion,
  190. premiumSuggestionResetDate,
  191. setPremiumSuggestionResetDate,
  192. assistantUpgraded,
  193. setAssistantUpgraded,
  194. writefullInstance,
  195. setWritefullInstance,
  196. }),
  197. [
  198. cobranding,
  199. features?.compileGroup,
  200. owner,
  201. userId,
  202. renameProject,
  203. permissionsLevel,
  204. setPermissionsLevel,
  205. isPendingEditor,
  206. showSymbolPalette,
  207. toggleSymbolPalette,
  208. insertSymbol,
  209. inactiveTutorials,
  210. deactivateTutorial,
  211. currentPopup,
  212. setCurrentPopup,
  213. outOfSync,
  214. setOutOfSync,
  215. hasPremiumSuggestion,
  216. setHasPremiumSuggestion,
  217. premiumSuggestionResetDate,
  218. setPremiumSuggestionResetDate,
  219. assistantUpgraded,
  220. setAssistantUpgraded,
  221. writefullInstance,
  222. setWritefullInstance,
  223. ]
  224. )
  225. return (
  226. <EditorContext.Provider value={value}>{children}</EditorContext.Provider>
  227. )
  228. }
  229. export function useEditorContext() {
  230. const context = useContext(EditorContext)
  231. if (!context) {
  232. throw new Error('useEditorContext is only available inside EditorProvider')
  233. }
  234. return context
  235. }