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

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