settings-modal-context.tsx 11 KB

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