-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathuseSettings.ts
More file actions
274 lines (241 loc) · 7.32 KB
/
useSettings.ts
File metadata and controls
274 lines (241 loc) · 7.32 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { ACCENT_COLORS } from '#shared/utils/constants'
import type { LocaleObject } from '@nuxtjs/i18n'
import { BACKGROUND_THEMES } from '#shared/utils/constants'
type BackgroundThemeId = keyof typeof BACKGROUND_THEMES
type AccentColorId = keyof typeof ACCENT_COLORS.light
/** Available search providers */
export type SearchProvider = 'npm' | 'algolia'
/**
* 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
/** Preferred background shade */
preferredBackgroundTheme: BackgroundThemeId | null
/** Hide platform-specific packages (e.g., @scope/pkg-linux-x64) from search results */
hidePlatformPackages: boolean
/** User-selected locale */
selectedLocale: LocaleObject['code'] | null
/** Search provider for package search */
searchProvider: SearchProvider
/** Show search results as you type */
instantSearch: boolean
/** Enable/disable keyboard shortcuts */
keyboardShortcuts: boolean
/** Connector preferences */
connector: {
/** Automatically open the web auth page in the browser */
autoOpenURL: boolean
}
sidebar: {
collapsed: string[]
}
chartFilter: {
averageWindow: number
smoothingTau: number
anomaliesFixed: boolean
}
}
const DEFAULT_SETTINGS: AppSettings = {
relativeDates: false,
includeTypesInInstall: true,
accentColorId: null,
hidePlatformPackages: true,
selectedLocale: null,
preferredBackgroundTheme: null,
searchProvider: import.meta.test ? 'npm' : 'algolia',
instantSearch: true,
keyboardShortcuts: true,
connector: {
autoOpenURL: false,
},
sidebar: {
collapsed: [],
},
chartFilter: {
averageWindow: 0,
smoothingTau: 1,
anomaliesFixed: true,
},
}
const STORAGE_KEY = 'npmx-settings'
/**
* Read settings from localStorage and merge with defaults.
*/
function normaliseSettings(input: AppSettings): AppSettings {
return {
...input,
searchProvider: input.searchProvider === 'npm' ? 'npm' : 'algolia',
sidebar: {
...input.sidebar,
collapsed: Array.isArray(input.sidebar?.collapsed)
? input.sidebar.collapsed.filter((v): v is string => typeof v === 'string')
: [],
},
}
}
function readFromLocalStorage(): AppSettings {
try {
const raw = localStorage.getItem(STORAGE_KEY)
if (raw) {
const stored = JSON.parse(raw)
return normaliseSettings({
...DEFAULT_SETTINGS,
...stored,
connector: { ...DEFAULT_SETTINGS.connector, ...stored.connector },
sidebar: { ...DEFAULT_SETTINGS.sidebar, ...stored.sidebar },
chartFilter: { ...DEFAULT_SETTINGS.chartFilter, ...stored.chartFilter },
})
}
} catch {}
return { ...DEFAULT_SETTINGS }
}
let syncInitialized = false
/**
* Composable for managing application settings.
*
* Uses useState for SSR-safe hydration (server and client agree on initial
* values during hydration) and syncs with localStorage on the client.
* The onPrehydrate script in prehydrate.ts handles DOM-level patches
* (accent color, bg theme, collapsed sections, etc.) to prevent visual
* flash before hydration.
*/
export function useSettings() {
const settings = useState<AppSettings>(STORAGE_KEY, () => ({ ...DEFAULT_SETTINGS }))
if (import.meta.client && !syncInitialized) {
syncInitialized = true
// Read localStorage eagerly but apply after mount to prevent hydration
// mismatch. During hydration, useState provides server-matching defaults.
// After mount, we swap in the user's actual preferences from localStorage.
// Uses nuxtApp.hook('app:mounted') instead of onMounted so it works even
// when useSettings() is first called from a plugin (no component context).
const stored = readFromLocalStorage()
const nuxtApp = useNuxtApp()
if (nuxtApp.isHydrating) {
nuxtApp.hook('app:mounted', () => {
settings.value = stored
})
} else {
settings.value = stored
}
// Persist future changes back to localStorage
watch(
settings,
value => {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(value))
} catch {}
},
{ deep: true },
)
}
return {
settings,
}
}
/**
* 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 accessing just the keyboard shortcuts setting.
* Useful for components that only need to read this specific setting.
*/
export const useKeyboardShortcuts = createSharedComposable(function useKeyboardShortcuts() {
const { settings } = useSettings()
const enabled = computed(() => settings.value.keyboardShortcuts)
if (import.meta.client) {
watch(
enabled,
value => {
if (value) {
delete document.documentElement.dataset.kbdShortcuts
} else {
document.documentElement.dataset.kbdShortcuts = 'false'
}
},
{ immediate: true },
)
}
return enabled
})
/**
* Composable for managing accent color.
*/
export function useAccentColor() {
const { settings } = useSettings()
const colorMode = useColorMode()
const accentColors = computed(() => {
const isDark = colorMode.value === 'dark'
const colors = isDark ? ACCENT_COLORS.dark : ACCENT_COLORS.light
return Object.entries(colors).map(([id, value]) => ({
id: id as AccentColorId,
name: id,
value,
}))
})
function setAccentColor(id: AccentColorId | null) {
if (id) {
document.documentElement.style.setProperty('--accent-color', `var(--swatch-${id})`)
} else {
document.documentElement.style.removeProperty('--accent-color')
}
settings.value.accentColorId = id
}
return {
accentColors,
selectedAccentColor: computed(() => settings.value.accentColorId),
setAccentColor,
}
}
/**
* Composable for managing the search provider setting.
*/
export function useSearchProvider() {
const { settings } = useSettings()
const searchProvider = computed({
get: () => settings.value.searchProvider,
set: (value: SearchProvider) => {
settings.value.searchProvider = value
},
})
const isAlgolia = computed(() => searchProvider.value === 'algolia')
function toggle() {
searchProvider.value = searchProvider.value === 'npm' ? 'algolia' : 'npm'
}
return {
searchProvider,
isAlgolia,
toggle,
}
}
export function useBackgroundTheme() {
const backgroundThemes = Object.entries(BACKGROUND_THEMES).map(([id, value]) => ({
id: id as BackgroundThemeId,
name: id,
value,
}))
const { settings } = useSettings()
function setBackgroundTheme(id: BackgroundThemeId | null) {
if (id) {
document.documentElement.dataset.bgTheme = id
} else {
document.documentElement.removeAttribute('data-bg-theme')
}
settings.value.preferredBackgroundTheme = id
}
return {
backgroundThemes,
selectedBackgroundTheme: computed(() => settings.value.preferredBackgroundTheme),
setBackgroundTheme,
}
}