settings-modal-context.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import { createContext, FC, useContext, useMemo, useState } from 'react'
  2. import { useLayoutContext } from '@/shared/context/layout-context'
  3. import AutoCloseBracketsSetting from '@/features/settings/components/editor-settings/auto-close-brackets-setting'
  4. import AutoCompleteSetting from '@/features/settings/components/editor-settings/auto-complete-setting'
  5. import CodeCheckSetting from '@/features/settings/components/editor-settings/code-check-setting'
  6. import PreviewTabsSetting from '@/features/settings/components/editor-settings/preview-tabs-setting'
  7. import KeybindingSetting from '@/features/settings/components/editor-settings/keybinding-setting'
  8. import PDFViewerSetting from '@/features/settings/components/editor-settings/pdf-viewer-setting'
  9. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  10. import SpellCheckSetting from '@/features/settings/components/editor-settings/spell-check-setting'
  11. import DictionarySetting from '@/features/settings/components/editor-settings/dictionary-setting'
  12. import { useTranslation } from 'react-i18next'
  13. import BreadcrumbsSetting from '@/features/settings/components/editor-settings/breadcrumbs-setting'
  14. import NonBlinkingCursorSetting from '@/features/settings/components/editor-settings/non-blinking-cursor-setting'
  15. import MathPreviewSetting from '@/features/settings/components/editor-settings/math-preview-setting'
  16. import RootDocumentSetting from '@/features/settings/components/compiler-settings/root-document-setting'
  17. import CompilerSetting from '@/features/settings/components/compiler-settings/compiler-setting'
  18. import ImageNameSetting from '@/features/settings/components/compiler-settings/image-name-setting'
  19. import DraftSetting from '@/features/settings/components/compiler-settings/draft-setting'
  20. import StopOnFirstErrorSetting from '@/features/settings/components/compiler-settings/stop-on-first-error-setting'
  21. import AutoCompileSetting from '@/features/settings/components/compiler-settings/auto-compile-setting'
  22. import OverallThemeSetting from '@/features/settings/components/appearance-settings/overall-theme-setting'
  23. import EditorThemeSetting from '@/features/settings/components/appearance-settings/editor-theme-setting'
  24. import FontSizeSetting from '@/features/settings/components/appearance-settings/font-size-setting'
  25. import LineHeightSetting from '@/features/settings/components/appearance-settings/line-height-setting'
  26. import FontFamilySetting from '@/features/settings/components/appearance-settings/font-family-setting'
  27. import { EditorLeftMenuProvider } from '@/features/editor-left-menu/components/editor-left-menu-context'
  28. import DarkModePdfSetting from '@/features/settings/components/appearance-settings/dark-mode-pdf-setting'
  29. import { useProjectSettingsContext } from '@/features/editor-left-menu/context/project-settings-context'
  30. import { useFeatureFlag } from '@/shared/context/split-test-context'
  31. import ProjectNotificationsSetting from '@/features/settings/components/editor-settings/project-notifications-setting'
  32. import getMeta from '@/utils/meta'
  33. import type {
  34. SettingsEntry,
  35. SettingsSection,
  36. SettingsSectionHook,
  37. } from '@/features/settings/context/types'
  38. import EditorTabsSetting from '../components/editor-settings/editor-tabs-setting'
  39. import FloatingMenuSetting from '../components/editor-settings/floating-menu-setting'
  40. const [referenceSearchSettingModule] = importOverleafModules(
  41. 'referenceSearchSetting'
  42. )
  43. const ReferenceSearchSetting = referenceSearchSettingModule?.import.default
  44. const editorTabExtraSectionHooks: SettingsSectionHook[] = importOverleafModules(
  45. 'settingsModalEditorTabSections'
  46. )
  47. .map((m: any) => m?.import?.default)
  48. .filter((h: unknown): h is SettingsSectionHook => typeof h === 'function')
  49. const spellcheckExtraSectionHooks: SettingsSectionHook[] =
  50. importOverleafModules('settingsModalSpellcheckSections')
  51. .map((m: any) => m?.import?.default)
  52. .filter((h: unknown): h is SettingsSectionHook => typeof h === 'function')
  53. const useSlotSections = (hooks: SettingsSectionHook[]): SettingsSection[] =>
  54. hooks.map(hook => hook()).filter((s): s is SettingsSection => s != null)
  55. type SettingsModalState = {
  56. show: boolean
  57. setShow: (shown: boolean) => void
  58. activeTab: string | null | undefined
  59. setActiveTab: (tab: string | null | undefined) => void
  60. settingsTabs: SettingsEntry[]
  61. settingToTabMap: Map<string, string>
  62. }
  63. export const SettingsModalContext = createContext<
  64. SettingsModalState | undefined
  65. >(undefined)
  66. export const SettingsModalProvider: FC<React.PropsWithChildren> = ({
  67. children,
  68. }) => {
  69. const { t } = useTranslation()
  70. const { isOverleaf } = getMeta('ol-ExposedSettings')
  71. const { overallTheme, floatingMenu } = useProjectSettingsContext()
  72. // TODO ide-redesign-cleanup: Rename this field and move it directly into this context
  73. const { leftMenuShown, setLeftMenuShown } = useLayoutContext()
  74. const hasEmailNotifications = useFeatureFlag('email-notifications')
  75. const hasEditorTabs = useFeatureFlag('editor-tabs')
  76. const hasToolbarMigration = useFeatureFlag('writefull-toolbar-migration')
  77. const editorTabExtraSections = useSlotSections(editorTabExtraSectionHooks)
  78. const spellcheckExtraSections = useSlotSections(spellcheckExtraSectionHooks)
  79. const allSettingsTabs: SettingsEntry[] = useMemo(
  80. () => [
  81. {
  82. key: 'editor',
  83. title: t('editor'),
  84. icon: 'code',
  85. sections: [
  86. {
  87. key: 'general',
  88. settings: [
  89. {
  90. key: 'autoComplete',
  91. component: <AutoCompleteSetting />,
  92. },
  93. {
  94. key: 'autoPairDelimiters',
  95. component: <AutoCloseBracketsSetting />,
  96. },
  97. {
  98. key: 'nonBlinkingCursor',
  99. component: <NonBlinkingCursorSetting />,
  100. },
  101. {
  102. key: 'syntaxValidation',
  103. component: <CodeCheckSetting />,
  104. },
  105. {
  106. key: 'editorTabs',
  107. component: <EditorTabsSetting />,
  108. hidden: !hasEditorTabs,
  109. },
  110. {
  111. key: 'previewTabs',
  112. component: <PreviewTabsSetting />,
  113. hidden: !hasEditorTabs,
  114. },
  115. {
  116. key: 'mode',
  117. component: <KeybindingSetting />,
  118. },
  119. {
  120. key: 'pdfViewer',
  121. component: <PDFViewerSetting />,
  122. },
  123. {
  124. key: 'write-and-cite-settings',
  125. component: <ReferenceSearchSetting />,
  126. hidden: !ReferenceSearchSetting,
  127. },
  128. {
  129. key: 'floating-menu',
  130. component: <FloatingMenuSetting />,
  131. hidden: !hasToolbarMigration && floatingMenu,
  132. },
  133. ],
  134. },
  135. {
  136. key: 'tools',
  137. title: t('tools'),
  138. settings: [
  139. {
  140. key: 'breadcrumbs-setting',
  141. component: <BreadcrumbsSetting />,
  142. },
  143. {
  144. key: 'mathPreview',
  145. component: <MathPreviewSetting />,
  146. },
  147. ],
  148. },
  149. ...editorTabExtraSections,
  150. ],
  151. },
  152. {
  153. key: 'spelling_and_language',
  154. title: t('spelling_and_language'),
  155. icon: 'spellcheck',
  156. sections: [
  157. {
  158. key: 'spellcheck',
  159. title: t('spellcheck'),
  160. settings: [
  161. {
  162. key: 'spellCheckLanguage',
  163. component: <SpellCheckSetting />,
  164. },
  165. {
  166. key: 'dictionary-settings',
  167. component: <DictionarySetting />,
  168. },
  169. ],
  170. },
  171. ...spellcheckExtraSections,
  172. ],
  173. },
  174. {
  175. key: 'compiler',
  176. title: t('compiler'),
  177. icon: 'picture_as_pdf',
  178. sections: [
  179. {
  180. key: 'general',
  181. settings: [
  182. {
  183. key: 'rootDocId',
  184. component: <RootDocumentSetting />,
  185. },
  186. {
  187. key: 'compiler',
  188. component: <CompilerSetting />,
  189. },
  190. {
  191. key: 'imageName',
  192. component: <ImageNameSetting />,
  193. },
  194. {
  195. key: 'draft',
  196. component: <DraftSetting />,
  197. },
  198. {
  199. key: 'stopOnFirstError',
  200. component: <StopOnFirstErrorSetting />,
  201. },
  202. {
  203. key: 'autoCompile',
  204. component: <AutoCompileSetting />,
  205. },
  206. ],
  207. },
  208. ],
  209. },
  210. {
  211. key: 'appearance',
  212. title: t('appearance'),
  213. icon: 'brush',
  214. sections: [
  215. {
  216. key: 'general',
  217. settings: [
  218. {
  219. key: 'overallTheme',
  220. component: <OverallThemeSetting />,
  221. },
  222. {
  223. key: 'editorTheme',
  224. component: <EditorThemeSetting />,
  225. },
  226. {
  227. key: 'pdfDarkMode',
  228. component: <DarkModePdfSetting />,
  229. hidden: overallTheme === 'light-',
  230. },
  231. {
  232. key: 'fontSize',
  233. component: <FontSizeSetting />,
  234. },
  235. {
  236. key: 'fontFamily',
  237. component: <FontFamilySetting />,
  238. },
  239. {
  240. key: 'lineHeight',
  241. component: <LineHeightSetting />,
  242. },
  243. ],
  244. },
  245. ],
  246. },
  247. {
  248. key: 'project_notifications',
  249. title: t('project_notifications'),
  250. icon: 'notifications' as const,
  251. sections: [
  252. {
  253. key: 'general',
  254. settings: [
  255. {
  256. key: 'projectNotifications',
  257. component: <ProjectNotificationsSetting />,
  258. },
  259. ],
  260. },
  261. ],
  262. hidden: !hasEmailNotifications,
  263. },
  264. {
  265. key: 'account_settings',
  266. title: t('account_settings'),
  267. icon: 'settings',
  268. href: '/user/settings',
  269. },
  270. {
  271. key: 'subscription',
  272. title: t('subscription'),
  273. icon: 'account_balance',
  274. href: '/user/subscription',
  275. hidden: !isOverleaf,
  276. },
  277. ],
  278. [
  279. t,
  280. hasEditorTabs,
  281. overallTheme,
  282. hasEmailNotifications,
  283. isOverleaf,
  284. editorTabExtraSections,
  285. spellcheckExtraSections,
  286. hasToolbarMigration,
  287. floatingMenu,
  288. ]
  289. )
  290. const settingsTabs = useMemo(
  291. () => allSettingsTabs.filter(tab => !tab.hidden),
  292. [allSettingsTabs]
  293. )
  294. const settingToTabMap = useMemo(() => {
  295. const map = new Map<string, string>()
  296. settingsTabs
  297. .filter(t => 'sections' in t)
  298. .forEach(tab => {
  299. tab.sections.forEach(section => {
  300. section.settings.forEach(setting => {
  301. map.set(setting.key, tab.key)
  302. })
  303. })
  304. })
  305. return map
  306. }, [settingsTabs])
  307. const [activeTab, setActiveTab] = useState<string | null | undefined>(
  308. settingsTabs[0]?.key
  309. )
  310. const value = useMemo(
  311. () => ({
  312. show: leftMenuShown,
  313. setShow: setLeftMenuShown,
  314. activeTab,
  315. setActiveTab,
  316. settingsTabs,
  317. settingToTabMap,
  318. }),
  319. [
  320. leftMenuShown,
  321. setLeftMenuShown,
  322. activeTab,
  323. setActiveTab,
  324. settingsTabs,
  325. settingToTabMap,
  326. ]
  327. )
  328. return (
  329. // TODO ide-redesign-cleanup: Merge <EditorLeftMenuProvider> into <SettingsModalProvider>
  330. <EditorLeftMenuProvider>
  331. <SettingsModalContext.Provider value={value}>
  332. {children}
  333. </SettingsModalContext.Provider>
  334. </EditorLeftMenuProvider>
  335. )
  336. }
  337. export const useSettingsModalContext = () => {
  338. const value = useContext(SettingsModalContext)
  339. if (!value) {
  340. throw new Error(
  341. `useSettingsModalContext is only available inside SettingsModalProvider`
  342. )
  343. }
  344. return value
  345. }