Files
doona/src/shell/preferences.ts
T
Zakk 29327acf9d refactor(api): name the demo backend and the storage keys once
DEMO_API and isDemoApi replace the 'mock' spellings, src/api/storage.ts holds every storage key with its existing string (a test pins them), and the native-API probe the sign-in screen falls back to moves into api/auth.ts. The discovery timeouts stay 3 s for hosted detection and 5 s for the connection test.
2026-09-26 04:47:54 +10:00

71 lines
2.2 KiB
TypeScript

import {createContext} from 'react';
import {readLang, type Lang} from '../i18n';
import {readProfiles, type Profile, type StoragePort} from '../api/profiles';
import {storageKeys} from '../api/storage';
import {DEFAULT_PALETTE, isPaletteId, type PaletteId} from './palettes';
export type Scheme = 'system' | 'light' | 'dark';
export type Wordmark = 'gradient' | 'plain';
export type {PaletteId};
export type Settings = {
api: string | null;
token: string;
profiles: Profile[];
activeId: string;
lang: Lang;
scheme: Scheme;
palette: PaletteId;
wordmark: Wordmark;
};
// A storage that throws (private mode, quota) costs the persistence, not the change.
export function writeSetting(key: 'lang' | 'scheme' | 'palette' | 'wordmark', value: string, storage?: StoragePort) {
try {
(storage ?? localStorage).setItem(storageKeys[key], value);
} catch {}
}
export function readSettings(storage?: StoragePort): Settings {
const read = (key: string): string | null => {
try {
return (storage ?? localStorage).getItem(key);
} catch {
return null;
}
};
const scheme = read(storageKeys.scheme);
const palette = read(storageKeys.palette);
const profiles = readProfiles(storage);
const active = profiles.profiles.find(profile => profile.id === profiles.activeId);
return {
...profiles,
api: active?.api ?? null,
token: active?.token ?? '',
lang: readLang(storage),
scheme: scheme === 'light' || scheme === 'dark' ? scheme : 'system',
palette: isPaletteId(palette) ? palette : DEFAULT_PALETTE,
wordmark: read(storageKeys.wordmark) === 'plain' ? 'plain' : 'gradient'
};
}
export function shouldOpenSettings(api: string | null, hash: string): boolean {
return api === null && (hash === '' || hash === '#' || hash === '#/');
}
type Appearance = {
scheme: Scheme;
dark: boolean;
toggle: () => void;
pickScheme: (value: Scheme) => void;
palette: PaletteId;
pickPalette: (value: PaletteId) => void;
wordmark: Wordmark;
pickWordmark: (value: Wordmark) => void;
};
export const SettingsContext = createContext<{
lang: Lang;
pickLang: (value: Lang) => void;
ap: Appearance;
paletteSections: Array<{title: string; items: Array<{id: PaletteId; label: string; desc?: string}>}>;
} | null>(null);