1import { writable } from 'svelte/store';
2import { defaultTheme, type Theme } from './theme.svelte';
3
4export type ApiEndpoints = Record<string, string> & {
5 slingshot: string;
6 spacedust: string;
7 constellation: string;
8};
9export type Settings = {
10 endpoints: ApiEndpoints;
11 theme: Theme;
12};
13
14export const defaultSettings: Settings = {
15 endpoints: {
16 slingshot: 'https://slingshot.microcosm.blue',
17 spacedust: 'https://spacedust.microcosm.blue',
18 constellation: 'https://constellation.microcosm.blue'
19 },
20 theme: defaultTheme
21};
22
23const createSettingsStore = () => {
24 const stored = localStorage.getItem('settings');
25
26 const initial: Partial<Settings> = stored ? JSON.parse(stored) : defaultSettings;
27 initial.endpoints = initial.endpoints ?? defaultSettings.endpoints;
28 initial.theme = initial.theme ?? defaultSettings.theme;
29
30 const { subscribe, set, update } = writable<Settings>(initial as Settings);
31
32 subscribe((settings) => {
33 const theme = settings.theme;
34 document.documentElement.style.setProperty('--nucleus-bg', theme.bg);
35 document.documentElement.style.setProperty('--nucleus-fg', theme.fg);
36 document.documentElement.style.setProperty('--nucleus-accent', theme.accent);
37 document.documentElement.style.setProperty('--nucleus-accent2', theme.accent2);
38 });
39
40 return {
41 subscribe,
42 set: (value: Settings) => {
43 localStorage.setItem('settings', JSON.stringify(value));
44 set(value);
45 },
46 update: (fn: (value: Settings) => Settings) => {
47 update((value) => {
48 const newValue = fn(value);
49 localStorage.setItem('settings', JSON.stringify(newValue));
50 return newValue;
51 });
52 },
53 reset: () => {
54 localStorage.setItem('settings', JSON.stringify(defaultSettings));
55 set(defaultSettings);
56 }
57 };
58};
59
60export const settings = createSettingsStore();
61
62export const needsReload = (current: Settings, other: Settings): boolean => {
63 return (
64 current.endpoints.slingshot !== other.endpoints.slingshot ||
65 current.endpoints.spacedust !== other.endpoints.spacedust ||
66 current.endpoints.constellation !== other.endpoints.constellation
67 );
68};