settings-modal-context.tsx 12 KB

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