Files
nanobot/webui/src/lib/local-preferences.ts
T
Li Fengmin 7c2bf8949c feat(webui): add completion notification sound preference
When a turn finishes while the user is watching the page there is no
audible cue; the existing browser notifications only cover the
background case. Add a `notificationSound` local preference (off by
default, next to `browserNotifications`) and play a short bundled
chime from the `turn_end` handler only while the page is visible, so
background tabs stay silent.

- webui/src/lib/notification-sound.ts: visibility- and preference-gated
  playback of the bundled /notification.wav, one reused Audio element
- Overview settings toggle with localized copy in all 10 locales
- Tests cover the default-off, visible, hidden, reuse and
  playback-rejection paths
2026-09-24 17:34:33 +08:00

68 lines
2.2 KiB
TypeScript

export type LocalActivityMode = "auto" | "expanded";
export type FileEditDisplayMode = "summary" | "diff" | "collapsed_diff";
export interface LocalPreferences {
activityMode: LocalActivityMode;
codeWrap: boolean;
brandLogos: boolean;
browserNotifications: boolean;
notificationSound: boolean;
fileEditDisplayMode: FileEditDisplayMode;
}
export const LOCAL_PREFS_STORAGE_KEY = "nanobot-webui.settings-preferences";
export const LOCAL_PREFS_CHANGED_EVENT = "nanobot-webui.local-preferences-changed";
const LOCAL_PREFS_SCHEMA_VERSION = 1;
type PersistedLocalPreferences = Partial<LocalPreferences> & {
schemaVersion?: number;
};
export const DEFAULT_LOCAL_PREFS: LocalPreferences = {
activityMode: "auto",
codeWrap: true,
brandLogos: true,
browserNotifications: false,
notificationSound: false,
fileEditDisplayMode: "summary",
};
export function normalizeFileEditDisplayMode(value: unknown): FileEditDisplayMode {
return value === "diff" || value === "collapsed_diff" ? value : "summary";
}
export function readLocalPreferences(): LocalPreferences {
try {
const raw = window.localStorage.getItem(LOCAL_PREFS_STORAGE_KEY);
if (!raw) return DEFAULT_LOCAL_PREFS;
const parsed = JSON.parse(raw) as PersistedLocalPreferences;
return {
activityMode: parsed.activityMode === "expanded" ? "expanded" : "auto",
codeWrap: parsed.codeWrap !== false,
brandLogos: parsed.schemaVersion === LOCAL_PREFS_SCHEMA_VERSION
? parsed.brandLogos !== false
: true,
browserNotifications: parsed.browserNotifications === true,
notificationSound: parsed.notificationSound === true,
fileEditDisplayMode: normalizeFileEditDisplayMode(parsed.fileEditDisplayMode),
};
} catch {
return DEFAULT_LOCAL_PREFS;
}
}
export function writeLocalPreferences(preferences: LocalPreferences): void {
try {
window.localStorage.setItem(LOCAL_PREFS_STORAGE_KEY, JSON.stringify({
schemaVersion: LOCAL_PREFS_SCHEMA_VERSION,
...preferences,
}));
} catch {
// Browser-only preferences should never block settings.
}
window.dispatchEvent(new CustomEvent<LocalPreferences>(
LOCAL_PREFS_CHANGED_EVENT,
{ detail: preferences },
));
}