char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Settings navigation cleanup (#27241)5b6ddc967

char/aliceai-t5-moe
2.3 KiB76 linesraw
1import { browser } from '$app/environment';
2import { CONFIG_LOCALSTORAGE_KEY, USER_OVERRIDES_LOCALSTORAGE_KEY } from '$lib/constants';
3
4/**
5 * SettingsService - localStorage persistence layer for settings
6 *
7 * Stateless read/write of the settings config and user-override keys. Business
8 * logic (default merging, mobile defaults, theme migration) stays in the store.
9 *
10 * **Architecture & Relationships:**
11 * - **settingsStore**: Primary consumer - loads config on init and persists on change
12 *
13 * @see settingsStore in stores/settings/index.svelte.ts - reactive state + business logic
14 */
15export class SettingsService {
16	/**
17	 * Read the raw config and user overrides from localStorage.
18	 * @returns Parsed values, or empty defaults when nothing is stored or parsing fails.
19	 */
20	static loadConfig(): {
21		config: Record<string, unknown>;
22		userOverrides: string[];
23		isFirstVisit: boolean;
24	} {
25		if (!browser) {
26			return { config: {}, isFirstVisit: false, userOverrides: [] };
27		}
28
29		try {
30			const storedConfigRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY);
31			const isFirstVisit = storedConfigRaw === null;
32			const config = JSON.parse(storedConfigRaw || '{}') as Record<string, unknown>;
33			const userOverrides = JSON.parse(
34				localStorage.getItem(USER_OVERRIDES_LOCALSTORAGE_KEY) || '[]'
35			) as string[];
36
37			return { config, isFirstVisit, userOverrides };
38		} catch (error) {
39			console.warn('Failed to parse config from localStorage, using defaults:', error);
40
41			return { config: {}, isFirstVisit: false, userOverrides: [] };
42		}
43	}
44
45	/**
46	 * Migrate the legacy un-namespaced "theme" localStorage key.
47	 * Returns the legacy theme value (and removes the key) when present, else null.
48	 */
49	static migrateLegacyTheme(): string | null {
50		if (!browser) return null;
51
52		const legacyTheme = localStorage.getItem('theme');
53
54		if (legacyTheme) {
55			localStorage.removeItem('theme');
56
57			return legacyTheme;
58		}
59
60		return null;
61	}
62
63	/**
64	 * Persist the config and user overrides to localStorage.
65	 */
66	static saveConfig(config: Record<string, unknown>, userOverrides: string[]): void {
67		if (!browser) return;
68
69		try {
70			localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config));
71			localStorage.setItem(USER_OVERRIDES_LOCALSTORAGE_KEY, JSON.stringify(userOverrides));
72		} catch (error) {
73			console.error('Failed to save config to localStorage:', error);
74		}
75	}
76}