Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/shared/src/prefs.ts

116 lines
4.5 KiB
TypeScript

// Tiny browser-local (localStorage) preference helper, in `@hive/shared`
// so a future second consumer (e.g. swarm-ui's own terminal) gets the
// exact same key name for free via the browser's shared per-origin
// storage, rather than an independent copy that could drift.
// `ExpandDetailsSetting` / `HideDebugSetting` / `ShowThinkingSetting` (the
// settings popover rows of the per-agent page and of the swarm UI) are the only
// writers today; `Row.tsx` is the only reader of any of them.
const EXPAND_DETAILS_KEY = "hive-agent-expand-details";
// Whether a per-agent terminal's otherwise-collapsed `<details>` panels
// (long tool-results, Write/Edit diffs, …) should default open. Pure
// client-side — no backend field, nothing round-trips through
// `/api/*`. Read live (not cached) by the shared terminal factory's
// `expandDetails` option — see docs/4-implementation/web-ui/shape.md::Shared terminal
// pane — so a preference change applies to the next rendered row in
// any already-open agent tab without a reload.
export function getExpandDetailsPref(): boolean {
try {
return localStorage.getItem(EXPAND_DETAILS_KEY) === "1";
} catch {
return false;
}
}
export function setExpandDetailsPref(v: boolean): void {
try {
if (v) localStorage.setItem(EXPAND_DETAILS_KEY, "1");
else localStorage.removeItem(EXPAND_DETAILS_KEY);
} catch {
/* localStorage unavailable — preference is session-only */
}
}
const HIDE_DEBUG_KEY = "hive-agent-hide-debug";
// Whether a per-agent terminal's `debug`-level rows (thinking, coalesced
// ticks, ambient harness chatter — see docs/4-implementation/web-ui/terminal-rendering.md's
// Levels table) should be skipped entirely rather than rendered muted.
// Same shape as `getExpandDetailsPref` above: pure client-side, no
// backend field, read live (not cached) so a change applies to the next
// rendered row without a reload.
export function getHideDebugPref(): boolean {
try {
return localStorage.getItem(HIDE_DEBUG_KEY) === "1";
} catch {
return false;
}
}
export function setHideDebugPref(v: boolean): void {
try {
if (v) localStorage.setItem(HIDE_DEBUG_KEY, "1");
else localStorage.removeItem(HIDE_DEBUG_KEY);
} catch {
/* localStorage unavailable — preference is session-only */
}
for (const listener of hideDebugListeners) listener();
}
const hideDebugListeners = new Set<() => void>();
// Calls `listener` whenever the preference changes: in this tab through
// `setHideDebugPref`, in another tab of the same origin through the
// browser's `storage` event. Returns the unsubscribe. This is what lets a
// terminal that is already showing rows follow the toggle, instead of only
// the rows that arrive after it.
export function subscribeHideDebugPref(listener: () => void): () => void {
hideDebugListeners.add(listener);
const onStorage = (e: StorageEvent) => {
if (e.key === HIDE_DEBUG_KEY || e.key === null) listener();
};
window.addEventListener("storage", onStorage);
return () => {
hideDebugListeners.delete(listener);
window.removeEventListener("storage", onStorage);
};
}
const HIDE_THINKING_KEY = "hive-agent-hide-thinking";
// Whether a terminal shows claude's thinking (its thinking text and the live
// count of thinking tokens). On unless turned off, and apart from the "hide
// developer details" preference above. The key records the off state, so a
// browser that never touched the setting shows thinking. Same shape as the
// others: client-side only, read live.
export function getShowThinkingPref(): boolean {
try {
return localStorage.getItem(HIDE_THINKING_KEY) !== "1";
} catch {
return true;
}
}
export function setShowThinkingPref(v: boolean): void {
try {
if (v) localStorage.removeItem(HIDE_THINKING_KEY);
else localStorage.setItem(HIDE_THINKING_KEY, "1");
} catch {
/* localStorage unavailable — preference is session-only */
}
for (const listener of showThinkingListeners) listener();
}
const showThinkingListeners = new Set<() => void>();
// Calls `listener` whenever the preference changes, here or in another tab of
// the same origin, as `subscribeHideDebugPref` does. Returns the unsubscribe.
export function subscribeShowThinkingPref(listener: () => void): () => void {
showThinkingListeners.add(listener);
const onStorage = (e: StorageEvent) => {
if (e.key === HIDE_THINKING_KEY || e.key === null) listener();
};
window.addEventListener("storage", onStorage);
return () => {
showThinkingListeners.delete(listener);
window.removeEventListener("storage", onStorage);
};
}