forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSettings.ts
More file actions
114 lines (100 loc) · 3.34 KB
/
useSettings.ts
File metadata and controls
114 lines (100 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import type { RemovableRef } from '@vueuse/core'
import { useLocalStorage } from '@vueuse/core'
import { ACCENT_COLORS } from '#shared/utils/constants'
type AccentColorId = keyof typeof ACCENT_COLORS
/**
* Application settings stored in localStorage
*/
export interface AppSettings {
/** Display dates as relative (e.g., "3 days ago") instead of absolute */
relativeDates: boolean
/** Include @types/* package in install command for packages without built-in types */
includeTypesInInstall: boolean
/** Accent color theme */
accentColorId: AccentColorId | null
/** Language code (e.g., "en-US") */
language: string
}
const DEFAULT_SETTINGS: AppSettings = {
relativeDates: false,
includeTypesInInstall: true,
accentColorId: null,
language: 'en-US',
}
const STORAGE_KEY = 'npmx-settings'
// Shared settings instance (singleton per app)
let settingsRef: RemovableRef<AppSettings> | null = null
export function getDefaultLanguage(languages: string[]) {
if (import.meta.server) return 'en-US'
return matchLanguages(languages, navigator.languages) || 'en-US'
}
/**
* Composable for managing application settings with localStorage persistence.
* Settings are shared across all components that use this composable.
*/
export function useSettings() {
if (!settingsRef) {
settingsRef = useLocalStorage<AppSettings>(STORAGE_KEY, DEFAULT_SETTINGS, {
mergeDefaults: true,
})
}
return {
settings: settingsRef,
}
}
/**
* Composable for accessing just the relative dates setting.
* Useful for components that only need to read this specific setting.
*/
export function useRelativeDates() {
const { settings } = useSettings()
return computed(() => settings.value.relativeDates)
}
/**
* Composable for managing accent color.
*/
export function useAccentColor() {
const { settings } = useSettings()
const accentColors = Object.entries(ACCENT_COLORS).map(([id, value]) => ({
id: id as AccentColorId,
name: id,
value,
}))
function setAccentColor(id: AccentColorId | null) {
const color = id ? ACCENT_COLORS[id] : null
if (color) {
document.documentElement.style.setProperty('--accent-color', color)
} else {
document.documentElement.style.removeProperty('--accent-color')
}
settings.value.accentColorId = id
}
return {
accentColors,
selectedAccentColor: computed(() => settings.value.accentColorId),
setAccentColor,
}
}
/**
* Applies accent color before hydration to prevent flash of default color.
* Call this from app.vue to ensure accent color is applied on every page.
*/
export function initAccentOnPrehydrate() {
// Callback is stringified by Nuxt - external variables won't be available.
// Colors must be hardcoded since ACCENT_COLORS can't be referenced.
onPrehydrate(() => {
const colors: Record<AccentColorId, string> = {
rose: 'oklch(0.797 0.084 11.056)',
amber: 'oklch(0.828 0.165 84.429)',
emerald: 'oklch(0.792 0.153 166.95)',
sky: 'oklch(0.787 0.128 230.318)',
violet: 'oklch(0.714 0.148 286.067)',
coral: 'oklch(0.704 0.177 14.75)',
}
const settings = JSON.parse(localStorage.getItem('npmx-settings') || '{}')
const color = settings.accentColorId ? colors[settings.accentColorId as AccentColorId] : null
if (color) {
document.documentElement.style.setProperty('--accent-color', color)
}
})
}