use-editor-theme-option-groups.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import getMeta from '@/utils/meta'
  2. import { useMemo } from 'react'
  3. import { Option } from '../components/settings/settings-menu-select'
  4. import { useTranslation } from 'react-i18next'
  5. const overrides = new Map([['overleaf', 'overleaf light']])
  6. function getThemeName(theme: string): string {
  7. return (overrides.get(theme) ?? theme).replace(/_/g, ' ')
  8. }
  9. export function useEditorThemesOptionGroups() {
  10. const editorThemes = getMeta('ol-editorThemes')
  11. const legacyEditorThemes = getMeta('ol-legacyEditorThemes')
  12. const { t } = useTranslation()
  13. const optgroups = useMemo(() => {
  14. const lightThemes: Array<Option> = []
  15. const darkThemes: Array<Option> = []
  16. editorThemes?.forEach(({ name: theme, dark }) => {
  17. const target = dark ? darkThemes : lightThemes
  18. target.push({
  19. value: theme,
  20. label: getThemeName(theme),
  21. })
  22. })
  23. const legacyEditorThemeOptions: Array<Option> =
  24. legacyEditorThemes?.map(({ name: theme }) => ({
  25. value: theme,
  26. label: getThemeName(theme) + ' (Legacy)',
  27. })) ?? []
  28. const groups = []
  29. if (lightThemes.length > 0) {
  30. groups.push({ label: t('light_themes'), options: lightThemes })
  31. }
  32. if (darkThemes.length > 0) {
  33. groups.push({ label: t('dark_themes'), options: darkThemes })
  34. }
  35. if (legacyEditorThemeOptions.length > 0) {
  36. groups.push({
  37. label: t('legacy_themes'),
  38. options: legacyEditorThemeOptions,
  39. })
  40. }
  41. return groups
  42. }, [editorThemes, legacyEditorThemes, t])
  43. return optgroups
  44. }