@stacksjs/defaults 0.75.672 → 0.75.683

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/ai/skills/stacks-ai/SKILL.md +7 -2
  2. package/ai/skills/stacks-api/SKILL.md +1 -1
  3. package/ai/skills/stacks-dashboard/SKILL.md +26 -5
  4. package/ai/skills/stacks-deploy/SKILL.md +8 -0
  5. package/ai/skills/stacks-desktop/SKILL.md +31 -0
  6. package/ai/skills/stacks-development/SKILL.md +5 -7
  7. package/ai/skills/stacks-health/SKILL.md +19 -9
  8. package/ai/skills/stacks-native/CATALOG.md +24 -24
  9. package/ai/skills/stacks-newsletter/SKILL.md +10 -4
  10. package/ai/skills/stacks-router/SKILL.md +2 -2
  11. package/ai/skills/stacks-routes/SKILL.md +2 -2
  12. package/ai/skills/stacks-scheduler/SKILL.md +16 -2
  13. package/ai/skills/stacks-security/SKILL.md +24 -0
  14. package/ai/skills/stacks-socials/SKILL.md +17 -1
  15. package/ai/skills/stacks-storage/SKILL.md +34 -15
  16. package/app/Actions/SubscriberEmailAction.ts +26 -10
  17. package/app/Jobs/Inspire.ts +33 -0
  18. package/functions/dashboard-sidebar-order.ts +3 -0
  19. package/ide/vscode/package.json +1 -1
  20. package/package.json +8 -7
  21. package/project/storage/framework/types/request-context.d.ts +3 -0
  22. package/resources/components/Dashboard/Billing/BillingPaymentMethodsPanel.stx +3 -10
  23. package/resources/components/Dashboard/Billing/BillingPlanSummary.stx +3 -10
  24. package/resources/components/Dashboard/Billing/BillingSettings.stx +13 -15
  25. package/resources/components/Dashboard/Billing/BillingTransactionHistory.stx +2 -9
  26. package/resources/components/Dashboard/DashboardSectionLanding.stx +72 -0
  27. package/resources/components/Dashboard/DashboardSectionSidebar.stx +78 -0
  28. package/resources/components/Dashboard/GlobalSearch.stx +3 -1
  29. package/resources/components/Dashboard/Home/DashboardHome.stx +22 -9
  30. package/resources/components/Dashboard/MobileSidebar.stx +1 -1
  31. package/resources/components/Dashboard/Settings/AppearanceSettingsDashboard.stx +37 -92
  32. package/resources/components/Dashboard/Settings/DashboardSettingsFrame.stx +98 -0
  33. package/resources/components/Dashboard/Settings/DashboardThemePicker.stx +213 -0
  34. package/resources/components/Dashboard/Settings/MailSettingsDashboard.stx +162 -167
  35. package/resources/components/Dashboard/Settings/SettingsDashboard.stx +34 -30
  36. package/resources/components/Dashboard/Settings/SoftwareUpdateDashboard.stx +229 -0
  37. package/resources/components/Dashboard/UI/ActivityTable.stx +7 -7
  38. package/resources/components/Dashboard/UI/Button.stx +6 -3
  39. package/resources/components/Dashboard/UI/Card.stx +1 -1
  40. package/resources/components/Dashboard/UI/DashboardNavigationCards.stx +44 -0
  41. package/resources/components/Dashboard/UI/Drawer.stx +1 -1
  42. package/resources/components/Dashboard/UI/Input.stx +1 -0
  43. package/resources/components/Dashboard/UI/Modal.stx +1 -0
  44. package/resources/components/Dashboard/UI/PageHeader.stx +1 -1
  45. package/resources/components/Dashboard/UI/QuickLinks.stx +7 -5
  46. package/resources/components/Dashboard/UI/Select.stx +1 -0
  47. package/resources/components/Dashboard/UI/ServiceHealth.stx +6 -6
  48. package/resources/components/Dashboard/UI/StatsCard.stx +5 -5
  49. package/resources/components/Dashboard/UI/Table.stx +2 -1
  50. package/resources/components/Dashboard/UI/Textarea.stx +1 -0
  51. package/resources/components/Marketing/SubscribeForm.stx +29 -8
  52. package/resources/functions/dashboard/section-landing.ts +201 -0
  53. package/resources/functions/dashboard/section-navigation.ts +95 -0
  54. package/styles/dashboard-theme.css +1017 -0
  55. package/views/dashboard/composables/dashboard-theme-bootstrap.ts +11 -0
  56. package/views/dashboard/composables/dashboard-themes.ts +180 -0
  57. package/views/dashboard/composables/settings-window.ts +116 -0
  58. package/views/dashboard/composables/useAppearance.ts +34 -13
  59. package/views/dashboard/composables/useDashboardThemes.ts +80 -0
  60. package/views/dashboard/composables/useSettingsWindow.ts +23 -0
  61. package/views/dashboard/layouts/default.stx +74 -59
  62. package/views/dashboard/layouts/guest.stx +11 -1
  63. package/views/dashboard/layouts/settings.stx +48 -0
  64. package/views/dashboard/sections/[section].stx +19 -0
  65. package/views/dashboard/settings/appearance.stx +4 -2
  66. package/views/dashboard/settings/billing.stx +3 -4
  67. package/views/dashboard/settings/index.stx +4 -2
  68. package/views/dashboard/settings/mail.stx +3 -3
  69. package/views/dashboard/settings/software-update.stx +9 -0
@@ -0,0 +1,11 @@
1
+ import { applyDashboardTheme, readDashboardThemes, resolveDashboardTheme } from './dashboard-themes'
2
+
3
+ const root = document.documentElement
4
+ try {
5
+ const { active, installed } = readDashboardThemes(window.localStorage)
6
+ applyDashboardTheme(root, resolveDashboardTheme(active, installed))
7
+ }
8
+ catch {
9
+ // The browser can refuse access to localStorage itself.
10
+ applyDashboardTheme(root)
11
+ }
@@ -0,0 +1,180 @@
1
+ export const DASHBOARD_THEME_STORAGE_KEY = 'stacks-dashboard-themes'
2
+ export const ORIGINAL_DASHBOARD_THEME = 'original'
3
+ export const MAX_THEME_FILE_SIZE = 16_384
4
+ const MAX_INSTALLED_THEMES = 24
5
+
6
+ const paletteKeys = [
7
+ 'window', 'canvas', 'surface', 'surfaceMuted', 'text', 'muted', 'border',
8
+ 'accent', 'accentFill', 'accentText', 'heroStart', 'heroEnd', 'heroText',
9
+ ] as const
10
+
11
+ type ThemePalette = Record<typeof paletteKeys[number], string>
12
+
13
+ export interface DashboardTheme {
14
+ version: 1
15
+ id: string
16
+ name: string
17
+ description?: string
18
+ layout: 'flush' | 'inset'
19
+ navigation: 'plain' | 'tiles' | 'sections'
20
+ overview: 'panel' | 'immersive'
21
+ radius: number
22
+ light: ThemePalette
23
+ dark: ThemePalette
24
+ }
25
+
26
+ export interface DashboardThemePreferences {
27
+ active: string
28
+ installed: DashboardTheme[]
29
+ }
30
+
31
+ export const SYSTEM_CLEANER_THEME: DashboardTheme = {
32
+ version: 1,
33
+ id: 'system-cleaner',
34
+ name: 'System Cleaner',
35
+ description: 'A violet overview, a flat sidebar, and color-coded section pages.',
36
+ layout: 'inset',
37
+ navigation: 'sections',
38
+ overview: 'immersive',
39
+ radius: 14,
40
+ light: {
41
+ window: '#e7e7ec',
42
+ canvas: '#f5f5f7',
43
+ surface: '#ffffff',
44
+ surfaceMuted: '#eeeef1',
45
+ text: '#1d1d1f',
46
+ muted: '#55555b',
47
+ border: '#d9d9e0',
48
+ accent: '#692bc7',
49
+ accentFill: '#6d28d9',
50
+ accentText: '#ffffff',
51
+ heroStart: '#6a2fb8',
52
+ heroEnd: '#27114e',
53
+ heroText: '#ffffff',
54
+ },
55
+ dark: {
56
+ window: '#111113',
57
+ canvas: '#1a1a1c',
58
+ surface: '#242426',
59
+ surfaceMuted: '#2c2c2e',
60
+ text: '#f5f5f7',
61
+ muted: '#b8b8bf',
62
+ border: '#3b3b42',
63
+ accent: '#c48bff',
64
+ accentFill: '#7a2fe0',
65
+ accentText: '#ffffff',
66
+ heroStart: '#542593',
67
+ heroEnd: '#200d3b',
68
+ heroText: '#ffffff',
69
+ },
70
+ }
71
+
72
+ export const BUNDLED_DASHBOARD_THEMES: readonly DashboardTheme[] = [SYSTEM_CLEANER_THEME]
73
+
74
+ function record(value: unknown): Record<string, unknown> {
75
+ return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {}
76
+ }
77
+
78
+ function readPalette(value: unknown, mode: string): ThemePalette {
79
+ const input = record(value)
80
+ const palette = {} as ThemePalette
81
+ for (const key of paletteKeys) {
82
+ const color = input[key]
83
+ if (typeof color !== 'string' || color.length !== 7 || !/^#[\da-f]{6}$/i.test(color))
84
+ throw new Error(`Use a six-digit hex color for ${mode}.${key}.`)
85
+ palette[key] = color.toLowerCase()
86
+ }
87
+ return palette
88
+ }
89
+
90
+ export function parseDashboardTheme(value: unknown): DashboardTheme {
91
+ const { version, id, name, description, layout, navigation, overview = 'panel', radius, light, dark } = record(value)
92
+ if (version !== 1)
93
+ throw new Error('Set the theme version to 1.')
94
+ if (typeof id !== 'string' || id !== id.trim() || !/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(id) || id.length > 48)
95
+ throw new Error('Use a theme ID of up to 48 lowercase letters, digits, and hyphens. Start with a letter.')
96
+ if (id === ORIGINAL_DASHBOARD_THEME || BUNDLED_DASHBOARD_THEMES.some(theme => theme.id === id))
97
+ throw new Error('Choose a new ID. That ID belongs to a bundled theme.')
98
+ if (typeof name !== 'string' || !name.trim() || name.length > 64)
99
+ throw new Error('Enter a theme name of up to 64 characters.')
100
+ if (description !== undefined && (typeof description !== 'string' || description.length > 180))
101
+ throw new Error('Keep the description to 180 characters or fewer.')
102
+ if (layout !== 'flush' && layout !== 'inset')
103
+ throw new Error('Choose either flush or inset for layout.')
104
+ if (navigation !== 'plain' && navigation !== 'tiles' && navigation !== 'sections')
105
+ throw new Error('Choose plain, tiles, or sections for navigation.')
106
+ if (overview !== 'panel' && overview !== 'immersive')
107
+ throw new Error('Choose either panel or immersive for overview.')
108
+ if (typeof radius !== 'number' || !Number.isInteger(radius) || radius < 0 || radius > 24)
109
+ throw new Error('Set the radius to a whole number from 0 to 24.')
110
+ return { version, id, name: name.trim(), description: description?.trim(), layout, navigation, overview, radius, light: readPalette(light, 'light'), dark: readPalette(dark, 'dark') }
111
+ }
112
+
113
+ export function resolveDashboardTheme(id: unknown, installed: readonly DashboardTheme[]): DashboardTheme | undefined {
114
+ return BUNDLED_DASHBOARD_THEMES.find(theme => theme.id === id) ?? installed.find(theme => theme.id === id)
115
+ }
116
+
117
+ export function readDashboardThemes(storage: Pick<Storage, 'getItem'>): DashboardThemePreferences {
118
+ const installed = new Map<string, DashboardTheme>()
119
+ try {
120
+ const saved = record(JSON.parse(storage.getItem(DASHBOARD_THEME_STORAGE_KEY) || '{}'))
121
+ if (Array.isArray(saved.installed)) {
122
+ for (const input of saved.installed.slice(0, MAX_INSTALLED_THEMES)) {
123
+ try {
124
+ const theme = parseDashboardTheme(input)
125
+ installed.set(theme.id, theme)
126
+ }
127
+ catch {
128
+ // An outdated or damaged installation should not hide valid themes.
129
+ }
130
+ }
131
+ }
132
+ const themes = [...installed.values()]
133
+ return { active: resolveDashboardTheme(saved.active, themes)?.id ?? ORIGINAL_DASHBOARD_THEME, installed: themes }
134
+ }
135
+ catch {
136
+ return { active: ORIGINAL_DASHBOARD_THEME, installed: [] }
137
+ }
138
+ }
139
+
140
+ export function installDashboardTheme(prefs: DashboardThemePreferences, theme: DashboardTheme): DashboardThemePreferences {
141
+ const index = prefs.installed.findIndex(installed => installed.id === theme.id)
142
+ const installed = [...prefs.installed]
143
+ if (index === -1) {
144
+ if (installed.length >= MAX_INSTALLED_THEMES)
145
+ throw new Error(`Remove an installed theme first. This browser can store ${MAX_INSTALLED_THEMES} themes.`)
146
+ installed.push(theme)
147
+ }
148
+ else {
149
+ installed[index] = theme
150
+ }
151
+ return { active: theme.id, installed }
152
+ }
153
+
154
+ function colorProperty(key: string): string {
155
+ return `--dashboard-${key.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`)}`
156
+ }
157
+
158
+ export function dashboardThemeColor(theme: DashboardTheme, key: keyof ThemePalette): string {
159
+ return `light-dark(${theme.light[key]}, ${theme.dark[key]})`
160
+ }
161
+
162
+ export function applyDashboardTheme(root: HTMLElement, theme?: DashboardTheme): void {
163
+ root.dataset.dashboardTheme = theme?.id ?? ORIGINAL_DASHBOARD_THEME
164
+ if (theme) {
165
+ root.dataset.dashboardLayout = theme.layout
166
+ root.dataset.dashboardNavigation = theme.navigation
167
+ root.dataset.dashboardOverview = theme.overview
168
+ root.style.setProperty('--dashboard-radius', `${theme.radius}px`)
169
+ for (const key of paletteKeys)
170
+ root.style.setProperty(colorProperty(key), dashboardThemeColor(theme, key))
171
+ }
172
+ else {
173
+ delete root.dataset.dashboardLayout
174
+ delete root.dataset.dashboardNavigation
175
+ delete root.dataset.dashboardOverview
176
+ root.style.removeProperty('--dashboard-radius')
177
+ for (const key of paletteKeys)
178
+ root.style.removeProperty(colorProperty(key))
179
+ }
180
+ }
@@ -0,0 +1,116 @@
1
+ import { nativeWindow } from '@stacksjs/desktop/browser'
2
+
3
+ const windowName = 'stacks-dashboard-settings'
4
+ const paneChannel = 'stacks.dashboard.settings-pane'
5
+ const settingsPaths = ['/settings', '/settings/appearance', '/settings/mail', '/settings/billing', '/settings/software-update']
6
+ let browserWindow: Window | null = null
7
+
8
+ export function isDashboardSettingsPath(path: string): boolean {
9
+ return settingsPaths.includes(path.replace(/\/+$/, ''))
10
+ }
11
+
12
+ /** Return false when the link should continue through ordinary navigation. */
13
+ export function openDashboardSettings(href = '/settings'): boolean {
14
+ let url: URL
15
+ try {
16
+ url = new URL(href, location.href)
17
+ }
18
+ catch {
19
+ return false
20
+ }
21
+ if (url.origin !== location.origin || !isDashboardSettingsPath(url.pathname) || isDashboardSettingsPath(location.pathname))
22
+ return false
23
+
24
+ if (nativeWindow.available) {
25
+ void nativeWindow.open({
26
+ name: windowName,
27
+ url: url.href,
28
+ title: 'Stacks Settings',
29
+ width: 746,
30
+ height: 680,
31
+ minWidth: 668,
32
+ minHeight: 480,
33
+ titlebarHidden: true,
34
+ webWindowMaterial: true,
35
+ chromeControls: false,
36
+ persistentStorage: true,
37
+ }).then(() => {
38
+ // Craft focuses an existing named window and leaves its webview in place.
39
+ // A pane-specific link can request a change from that existing webview.
40
+ if (url.pathname !== '/settings' && typeof BroadcastChannel !== 'undefined') {
41
+ const channel = new BroadcastChannel(paneChannel)
42
+ channel.postMessage(url.pathname + url.search + url.hash)
43
+ channel.close()
44
+ }
45
+ })
46
+ return true
47
+ }
48
+
49
+ if (!browserWindow || browserWindow.closed) {
50
+ browserWindow = window.open(url.href, windowName, 'popup,width=746,height=680')
51
+ if (!browserWindow)
52
+ return false
53
+ }
54
+ else if (url.pathname !== '/settings') {
55
+ // An authentication redirect can temporarily take the popup off-origin.
56
+ // Its URL is writeable even when its current value cannot be read.
57
+ let current: string | undefined
58
+ try { current = browserWindow.location.href }
59
+ catch {}
60
+ if (current !== url.href)
61
+ browserWindow.location.href = url.href
62
+ }
63
+ browserWindow.focus()
64
+ return true
65
+ }
66
+
67
+ export function handleDashboardSettingsClick(event: MouseEvent): void {
68
+ if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
69
+ return
70
+ const link = event.target instanceof Element ? event.target.closest<HTMLAnchorElement>('a[href]') : null
71
+ if (!link || link.hasAttribute('download') || (link.target && link.target !== '_self'))
72
+ return
73
+ if (openDashboardSettings(link.href)) {
74
+ event.preventDefault()
75
+ event.stopPropagation()
76
+ }
77
+ }
78
+
79
+ export function listenForSettingsPane(navigate: (href: string) => void): () => void {
80
+ if (typeof BroadcastChannel === 'undefined' || !nativeWindow.available)
81
+ return () => {}
82
+ const channel = new BroadcastChannel(paneChannel)
83
+ channel.onmessage = ({ data }) => {
84
+ if (typeof data !== 'string' || !data.startsWith('/') || data.startsWith('//'))
85
+ return
86
+ const url = new URL(data, location.href)
87
+ if (url.origin === location.origin && isDashboardSettingsPath(url.pathname) && url.href !== location.href)
88
+ navigate(url.pathname + url.search + url.hash)
89
+ }
90
+ return () => channel.close()
91
+ }
92
+
93
+ export function listenForSettingsCommand(open: () => void): (() => void) | undefined {
94
+ return (window as Window & { craft?: { settings?: { onOpen?: (handler: () => void) => () => void } } }).craft?.settings?.onOpen?.(open)
95
+ }
96
+
97
+ export function isNativeSettingsWindow(): boolean {
98
+ return nativeWindow.available
99
+ }
100
+
101
+ export function dragSettingsWindow(event: MouseEvent): void {
102
+ if (event.button === 0 && event.target === event.currentTarget)
103
+ void nativeWindow.startDrag()
104
+ }
105
+
106
+ export function setSettingsWindowAppearance(mode: 'system' | 'light' | 'dark'): void {
107
+ void nativeWindow.setAppearance(mode)
108
+ }
109
+
110
+ export function returnToDashboard(event: MouseEvent): void {
111
+ if (window.opener && !window.opener.closed) {
112
+ event.preventDefault()
113
+ window.opener.focus()
114
+ window.close()
115
+ }
116
+ }
@@ -189,9 +189,10 @@ export function sidebarThemeFor(style: SidebarStyle, knownThemes: Record<string,
189
189
  * under `APPEARANCE_STORAGE_KEY`.
190
190
  */
191
191
  export const appearanceStore = defineStore('appearance', () => {
192
- const sidebarStyle = state<SidebarStyle>(DEFAULT_APPEARANCE.sidebarStyle)
193
- const colorMode = state<ColorMode>(DEFAULT_APPEARANCE.colorMode)
194
- const hiddenSections = state<string[]>([...DEFAULT_APPEARANCE.hiddenSections])
192
+ const saved = readStoredAppearance()
193
+ const sidebarStyle = state(saved.sidebarStyle)
194
+ const colorMode = state(saved.colorMode)
195
+ const hiddenSections = state(saved.hiddenSections)
195
196
 
196
197
  function snapshot(): AppearancePreferences {
197
198
  return {
@@ -201,6 +202,19 @@ export const appearanceStore = defineStore('appearance', () => {
201
202
  }
202
203
  }
203
204
 
205
+ function persist(): void {
206
+ if (typeof window === 'undefined')
207
+ return
208
+ try {
209
+ // Save during the interaction so an immediate navigation sees the new
210
+ // value in the synchronous appearance bootstrap.
211
+ window.localStorage.setItem(APPEARANCE_STORAGE_KEY, JSON.stringify(snapshot()))
212
+ }
213
+ catch {
214
+ // The appearance still works in this document if storage is disabled.
215
+ }
216
+ }
217
+
204
218
  /**
205
219
  * Write the preferences onto `<html>`, where the shell CSS reads them.
206
220
  *
@@ -238,6 +252,7 @@ export const appearanceStore = defineStore('appearance', () => {
238
252
  if (!isSidebarStyle(style))
239
253
  return
240
254
  sidebarStyle.set(style)
255
+ persist()
241
256
  apply()
242
257
  }
243
258
 
@@ -245,6 +260,7 @@ export const appearanceStore = defineStore('appearance', () => {
245
260
  if (!isColorMode(mode))
246
261
  return
247
262
  colorMode.set(mode)
263
+ persist()
248
264
  apply()
249
265
  }
250
266
 
@@ -253,6 +269,7 @@ export const appearanceStore = defineStore('appearance', () => {
253
269
  if (!visible)
254
270
  next.push(id)
255
271
  hiddenSections.set(next)
272
+ persist()
256
273
  }
257
274
 
258
275
  function isSectionHidden(id: string): boolean {
@@ -263,6 +280,7 @@ export const appearanceStore = defineStore('appearance', () => {
263
280
  sidebarStyle.set(DEFAULT_APPEARANCE.sidebarStyle)
264
281
  colorMode.set(DEFAULT_APPEARANCE.colorMode)
265
282
  hiddenSections.set([])
283
+ persist()
266
284
  apply()
267
285
  }
268
286
 
@@ -277,12 +295,6 @@ export const appearanceStore = defineStore('appearance', () => {
277
295
  reset,
278
296
  apply,
279
297
  }
280
- }, {
281
- persist: {
282
- storage: 'localStorage',
283
- key: APPEARANCE_STORAGE_KEY,
284
- pick: ['sidebarStyle', 'colorMode', 'hiddenSections'],
285
- },
286
298
  })
287
299
 
288
300
  /*
@@ -300,17 +312,26 @@ export type AppearanceSnapshot = typeof appearanceStore
300
312
  * mirroring happens here instead — otherwise `system` would be resolved once,
301
313
  * at load, and changing the OS theme would do nothing until a reload.
302
314
  */
303
- function installSystemThemeListener(): void {
304
- if (typeof window === 'undefined' || typeof window.matchMedia !== 'function')
315
+ function installAppearanceListeners(): void {
316
+ if (typeof window === 'undefined')
305
317
  return
306
- window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
318
+ window.addEventListener('storage', (event) => {
319
+ if (event.key !== APPEARANCE_STORAGE_KEY && event.key !== null)
320
+ return
321
+ const next = readStoredAppearance()
322
+ appearanceStore.sidebarStyle.set(next.sidebarStyle)
323
+ appearanceStore.colorMode.set(next.colorMode)
324
+ appearanceStore.hiddenSections.set(next.hiddenSections)
325
+ appearanceStore.apply()
326
+ })
327
+ window.matchMedia?.('(prefers-color-scheme: dark)').addEventListener('change', () => {
307
328
  const store = appearanceStore
308
329
  if (store.colorMode() === 'system')
309
330
  store.apply()
310
331
  })
311
332
  }
312
333
 
313
- installSystemThemeListener()
334
+ installAppearanceListeners()
314
335
 
315
336
  export function useAppearance(): AppearanceSnapshot {
316
337
  return appearanceStore
@@ -0,0 +1,80 @@
1
+ import type { DashboardThemePreferences } from './dashboard-themes'
2
+ import { defineStore, derived, state } from '@stacksjs/stx'
3
+ import {
4
+ applyDashboardTheme,
5
+ DASHBOARD_THEME_STORAGE_KEY,
6
+ installDashboardTheme,
7
+ ORIGINAL_DASHBOARD_THEME,
8
+ parseDashboardTheme,
9
+ readDashboardThemes,
10
+ resolveDashboardTheme,
11
+ } from './dashboard-themes'
12
+
13
+ function read(): DashboardThemePreferences {
14
+ try {
15
+ if (typeof window !== 'undefined')
16
+ return readDashboardThemes(window.localStorage)
17
+ }
18
+ catch {
19
+ // Storage access can be disabled by the browser.
20
+ }
21
+ return { active: ORIGINAL_DASHBOARD_THEME, installed: [] }
22
+ }
23
+
24
+ const dashboardThemeStore = defineStore('dashboard-themes', () => {
25
+ const preferences = state(read())
26
+ const active = derived(() => preferences().active)
27
+ const installed = derived(() => preferences().installed)
28
+ const sectionNavigation = derived(() => resolveDashboardTheme(active(), installed())?.navigation === 'sections')
29
+
30
+ function apply(next: DashboardThemePreferences): void {
31
+ preferences.set(next)
32
+ if (typeof document !== 'undefined')
33
+ applyDashboardTheme(document.documentElement, resolveDashboardTheme(next.active, next.installed))
34
+ }
35
+
36
+ if (typeof window !== 'undefined') {
37
+ window.addEventListener('storage', (event) => {
38
+ if (event.key === DASHBOARD_THEME_STORAGE_KEY || event.key === null)
39
+ apply(read())
40
+ })
41
+ }
42
+
43
+ function save(next: DashboardThemePreferences): void {
44
+ try {
45
+ window.localStorage.setItem(DASHBOARD_THEME_STORAGE_KEY, JSON.stringify(next))
46
+ }
47
+ catch {
48
+ throw new Error('The browser could not save your dashboard themes.')
49
+ }
50
+ apply(next)
51
+ }
52
+
53
+ function select(id: string): void {
54
+ const current = read()
55
+ if (id === ORIGINAL_DASHBOARD_THEME || resolveDashboardTheme(id, current.installed))
56
+ save({ ...current, active: id })
57
+ }
58
+
59
+ function install(value: unknown): string {
60
+ const theme = parseDashboardTheme(value)
61
+ save(installDashboardTheme(read(), theme))
62
+ return theme.name
63
+ }
64
+
65
+ function remove(id: string): void {
66
+ const current = read()
67
+ if (!current.installed.some(theme => theme.id === id))
68
+ return
69
+ save({
70
+ active: current.active === id ? ORIGINAL_DASHBOARD_THEME : current.active,
71
+ installed: current.installed.filter(theme => theme.id !== id),
72
+ })
73
+ }
74
+
75
+ return { active, installed, sectionNavigation, select, install, remove, sync: () => apply(read()) }
76
+ })
77
+
78
+ export function useDashboardThemes(): typeof dashboardThemeStore {
79
+ return dashboardThemeStore
80
+ }
@@ -0,0 +1,23 @@
1
+ import { defineStore, state } from '@stacksjs/stx'
2
+
3
+ const historyKey = 'stacksSettingsIndex'
4
+
5
+ export const useSettingsWindow = defineStore('dashboard-settings-window', () => {
6
+ const query = state('')
7
+ const index = state(0)
8
+ const lastIndex = state(0)
9
+ let initialized = false
10
+
11
+ function syncHistory(): void {
12
+ const saved = history.state?.[historyKey]
13
+ const isEntry = typeof saved === 'number'
14
+ const next = isEntry ? saved : initialized ? index() + 1 : 0
15
+ if (!isEntry)
16
+ history.replaceState({ ...history.state, [historyKey]: next }, '')
17
+ index.set(next)
18
+ lastIndex.set(isEntry ? Math.max(lastIndex(), next) : next)
19
+ initialized = true
20
+ }
21
+
22
+ return { query, index, lastIndex, syncHistory }
23
+ })