settings-modal-context.tsx 9.8 KB

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