-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathuseSettings.ts
More file actions
241 lines (212 loc) · 6.66 KB
/
useSettings.ts
File metadata and controls
241 lines (212 loc) · 6.66 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
import type { RemovableRef } from '@vueuse/core'
import { useLocalStorage } from '@vueuse/core'
import { ACCENT_COLORS, type AccentColorId } from '#shared/utils/constants'
import type { LocaleObject } from '@nuxtjs/i18n'
import { BACKGROUND_THEMES } from '#shared/utils/constants'
type BackgroundThemeId = keyof typeof BACKGROUND_THEMES
/** 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
/** Enable weekly download graph pulse looping animation */
enableGraphPulseLooping: 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
/** Enable/disable auto scrolling to requested version at package changelog */
changelogAutoScroll: 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
predictionPoints: number
}
}
const DEFAULT_SETTINGS: AppSettings = {
relativeDates: false,
includeTypesInInstall: true,
accentColorId: null,
hidePlatformPackages: true,
enableGraphPulseLooping: false,
selectedLocale: null,
preferredBackgroundTheme: null,
searchProvider: import.meta.test ? 'npm' : 'algolia',
instantSearch: true,
keyboardShortcuts: true,
changelogAutoScroll: true,
connector: {
autoOpenURL: false,
},
sidebar: {
collapsed: [],
},
chartFilter: {
averageWindow: 0,
smoothingTau: 1,
anomaliesFixed: true,
predictionPoints: 4,
},
}
const STORAGE_KEY = 'npmx-settings'
// Shared settings instance (singleton per app)
let settingsRef: RemovableRef<AppSettings> | null = null
/**
* 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 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 { t } = useI18n()
const accentColorLabels = computed<Record<AccentColorId, string>>(() => ({
sky: t('settings.accent_colors.sky'),
coral: t('settings.accent_colors.coral'),
amber: t('settings.accent_colors.amber'),
emerald: t('settings.accent_colors.emerald'),
violet: t('settings.accent_colors.violet'),
magenta: t('settings.accent_colors.magenta'),
neutral: t('settings.clear_accent'),
}))
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,
label: accentColorLabels.value[id as AccentColorId],
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 { t } = useI18n()
const bgThemeLabels = computed<Record<BackgroundThemeId, string>>(() => ({
neutral: t('settings.background_themes.neutral'),
stone: t('settings.background_themes.stone'),
zinc: t('settings.background_themes.zinc'),
slate: t('settings.background_themes.slate'),
black: t('settings.background_themes.black'),
}))
const backgroundThemes = computed(() =>
Object.entries(BACKGROUND_THEMES).map(([id, value]) => ({
id: id as BackgroundThemeId,
label: bgThemeLabels.value[id as BackgroundThemeId],
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,
}
}