|
| 1 | +import { ACCENT_COLORS, BACKGROUND_THEMES } from '#shared/utils/constants' |
| 2 | +import { |
| 3 | + DEFAULT_USER_PREFERENCES, |
| 4 | + type AccentColorId, |
| 5 | + type BackgroundThemeId, |
| 6 | + type ColorModePreference, |
| 7 | +} from '#shared/schemas/userPreferences' |
| 8 | + |
| 9 | +/** |
| 10 | + * Main composable for user preferences. |
| 11 | + * Uses `npmx-user-preferences` localStorage key and syncs to the server |
| 12 | + * for authenticated users via `useUserPreferencesProvider`. |
| 13 | + */ |
| 14 | +export function useUserPreferences() { |
| 15 | + const provider = useUserPreferencesProvider(DEFAULT_USER_PREFERENCES) |
| 16 | + |
| 17 | + return { |
| 18 | + preferences: provider.data, |
| 19 | + isAuthenticated: provider.isAuthenticated, |
| 20 | + isSyncing: provider.isSyncing, |
| 21 | + isSynced: provider.isSynced, |
| 22 | + hasError: provider.hasError, |
| 23 | + syncError: provider.syncError, |
| 24 | + lastSyncedAt: provider.lastSyncedAt, |
| 25 | + initSync: provider.initSync, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export function useRelativeDates() { |
| 30 | + const { preferences } = useUserPreferences() |
| 31 | + return computed(() => preferences.value.relativeDates) |
| 32 | +} |
| 33 | + |
| 34 | +export function useAccentColor() { |
| 35 | + const { preferences } = useUserPreferences() |
| 36 | + const colorMode = useColorMode() |
| 37 | + |
| 38 | + const accentColors = computed(() => { |
| 39 | + const isDark = colorMode.value === 'dark' |
| 40 | + const colors = isDark ? ACCENT_COLORS.dark : ACCENT_COLORS.light |
| 41 | + |
| 42 | + return Object.entries(colors).map(([id, value]) => ({ |
| 43 | + id: id as AccentColorId, |
| 44 | + name: id, |
| 45 | + value, |
| 46 | + })) |
| 47 | + }) |
| 48 | + |
| 49 | + function setAccentColor(id: AccentColorId | null) { |
| 50 | + if (id) { |
| 51 | + document.documentElement.style.setProperty('--accent-color', `var(--swatch-${id})`) |
| 52 | + } else { |
| 53 | + document.documentElement.style.removeProperty('--accent-color') |
| 54 | + } |
| 55 | + preferences.value.accentColorId = id |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + accentColors, |
| 60 | + selectedAccentColor: computed(() => preferences.value.accentColorId), |
| 61 | + setAccentColor, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export function useBackgroundTheme() { |
| 66 | + const backgroundThemes = Object.entries(BACKGROUND_THEMES).map(([id, value]) => ({ |
| 67 | + id: id as BackgroundThemeId, |
| 68 | + name: id, |
| 69 | + value, |
| 70 | + })) |
| 71 | + |
| 72 | + const { preferences } = useUserPreferences() |
| 73 | + |
| 74 | + function setBackgroundTheme(id: BackgroundThemeId | null) { |
| 75 | + if (id) { |
| 76 | + document.documentElement.dataset.bgTheme = id |
| 77 | + } else { |
| 78 | + document.documentElement.removeAttribute('data-bg-theme') |
| 79 | + } |
| 80 | + preferences.value.preferredBackgroundTheme = id |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + backgroundThemes, |
| 85 | + selectedBackgroundTheme: computed(() => preferences.value.preferredBackgroundTheme), |
| 86 | + setBackgroundTheme, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Composable for syncing color mode preference. |
| 92 | + * Keeps the user preference in sync with @nuxtjs/color-mode's own LS key (`npmx-color-mode`) |
| 93 | + * so that the color-mode module picks up the correct value on page load. |
| 94 | + */ |
| 95 | +export function useColorModePreference() { |
| 96 | + const { preferences } = useUserPreferences() |
| 97 | + const colorMode = useColorMode() |
| 98 | + |
| 99 | + /** |
| 100 | + * Set color mode preference and sync to both user preferences and the |
| 101 | + * `npmx-color-mode` LS key used by @nuxtjs/color-mode. |
| 102 | + */ |
| 103 | + function setColorMode(mode: ColorModePreference) { |
| 104 | + preferences.value.colorModePreference = mode |
| 105 | + colorMode.preference = mode |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * On init, if the user has a stored preference, apply it to @nuxtjs/color-mode. |
| 110 | + * This handles the case where preferences were synced from the server. |
| 111 | + */ |
| 112 | + function applyStoredColorMode() { |
| 113 | + const stored = preferences.value.colorModePreference |
| 114 | + if (stored) { |
| 115 | + colorMode.preference = stored |
| 116 | + } else { |
| 117 | + // No user preference stored yet — seed it from the current color-mode LS value |
| 118 | + const currentPreference = colorMode.preference as ColorModePreference |
| 119 | + if (currentPreference && currentPreference !== 'system') { |
| 120 | + preferences.value.colorModePreference = currentPreference |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + colorModePreference: computed( |
| 127 | + () => preferences.value.colorModePreference ?? colorMode.preference, |
| 128 | + ), |
| 129 | + setColorMode, |
| 130 | + applyStoredColorMode, |
| 131 | + } |
| 132 | +} |
0 commit comments