user-settings-context.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. createContext,
  3. useContext,
  4. useMemo,
  5. Dispatch,
  6. SetStateAction,
  7. FC,
  8. useState,
  9. useEffect,
  10. } from 'react'
  11. import { UserSettings } from '../../../../types/user-settings'
  12. import getMeta from '@/utils/meta'
  13. import customLocalStorage from '@/infrastructure/local-storage'
  14. import { getLegacyWriteAndCiteMigration } from '../utils/write-and-cite-settings-migration'
  15. import { saveUserSettings } from '@/features/editor-left-menu/utils/api'
  16. export const defaultSettings: UserSettings = {
  17. pdfViewer: 'pdfjs',
  18. autoComplete: true,
  19. autoPairDelimiters: true,
  20. syntaxValidation: false,
  21. previewTabs: false,
  22. editorTheme: 'textmate',
  23. editorDarkTheme: 'overleaf_dark',
  24. editorLightTheme: 'textmate',
  25. overallTheme: '',
  26. mode: 'default',
  27. fontSize: 12,
  28. fontFamily: 'monaco',
  29. lineHeight: 'normal',
  30. mathPreview: true,
  31. editorTabs: true,
  32. referencesSearchMode: 'advanced',
  33. breadcrumbs: true,
  34. nonBlinkingCursor: false,
  35. darkModePdf: false,
  36. zotero: {
  37. enabled: true,
  38. groups: [],
  39. disablePersonalLibrary: false,
  40. },
  41. mendeley: {
  42. enabled: true,
  43. groups: [],
  44. disablePersonalLibrary: false,
  45. },
  46. papers: {
  47. enabled: true,
  48. groups: [],
  49. disablePersonalLibrary: false,
  50. },
  51. }
  52. type UserSettingsContextValue = {
  53. userSettings: UserSettings
  54. setUserSettings: Dispatch<
  55. SetStateAction<UserSettingsContextValue['userSettings']>
  56. >
  57. }
  58. export const UserSettingsContext = createContext<
  59. UserSettingsContextValue | undefined
  60. >(undefined)
  61. export const UserSettingsProvider: FC<React.PropsWithChildren> = ({
  62. children,
  63. }) => {
  64. const [userSettings, setUserSettings] = useState<UserSettings>(
  65. () => getMeta('ol-userSettings') || defaultSettings
  66. )
  67. useEffect(() => {
  68. const { patch, keysToRemove } = getLegacyWriteAndCiteMigration(userSettings)
  69. if (Object.keys(patch).length === 0) {
  70. keysToRemove.forEach(customLocalStorage.removeItem)
  71. return
  72. }
  73. Promise.all(
  74. Object.entries(patch).map(([key, value]) =>
  75. saveUserSettings(
  76. key as keyof Pick<UserSettings, 'mendeley' | 'zotero' | 'papers'>,
  77. value
  78. )
  79. )
  80. ).then(() => {
  81. setUserSettings(currentSettings => ({
  82. ...currentSettings,
  83. ...patch,
  84. }))
  85. keysToRemove.forEach(customLocalStorage.removeItem)
  86. })
  87. // Only run once when the provider mounts
  88. // eslint-disable-next-line react-hooks/exhaustive-deps
  89. }, [])
  90. const value = useMemo<UserSettingsContextValue>(
  91. () => ({
  92. userSettings,
  93. setUserSettings,
  94. }),
  95. [userSettings, setUserSettings]
  96. )
  97. return (
  98. <UserSettingsContext.Provider value={value}>
  99. {children}
  100. </UserSettingsContext.Provider>
  101. )
  102. }
  103. export function useUserSettingsContext() {
  104. const context = useContext(UserSettingsContext)
  105. if (!context) {
  106. throw new Error(
  107. 'useUserSettingsContext is only available inside UserSettingsProvider'
  108. )
  109. }
  110. return context
  111. }