Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix(desktop): a saved setting of the wrong kind no longer breaks the chat window

leepokai1e83320e3fe4
diff --git a/apps/desktop/src/renderer/lib/prefs.test.ts b/apps/desktop/src/renderer/lib/prefs.test.ts
new file mode 100644
index 00000000..e0c65f21
--- /dev/null
+++ b/apps/desktop/src/renderer/lib/prefs.test.ts
@@ -0,0 +1,26 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { setMaxListeners } from 'node:events'
+
+test('A stored preference of another kind reads as the fallback', async () => {
+ const stored = new Map<string, string>([
+ ['hiddenComputers', 'null'],
+ ['computerOrder', '{"a":1}'],
+ ['sidebarCompact', '"yes"'],
+ ['desktopSidebarWidth', '300'],
+ ['computerAccessSetup', '"ready"'],
+ ])
+ const events = new EventTarget()
+ setMaxListeners(0, events)
+ Object.defineProperty(globalThis, 'window', { configurable: true, value: events })
+ Object.defineProperty(globalThis, 'localStorage', {
+ configurable: true,
+ value: { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => void stored.set(key, value) },
+ })
+ const { prefs } = await import('./prefs.ts')
+ assert.equal(prefs.hiddenComputers.get(), '')
+ assert.deepEqual(prefs.computerOrder.get(), [])
+ assert.equal(prefs.sidebarCompact.get(), false)
+ assert.equal(prefs.sidebarWidth.get(), 300)
+ assert.equal(prefs.computerAccessSetup.get(), 'ready')
+})
diff --git a/apps/desktop/src/renderer/lib/prefs.ts b/apps/desktop/src/renderer/lib/prefs.ts
index 9a32661e..a52156c2 100644
--- a/apps/desktop/src/renderer/lib/prefs.ts
+++ b/apps/desktop/src/renderer/lib/prefs.ts
@@ -2,24 +2,32 @@ import type { AppLanguage } from './l10n'
import { useSyncExternalStore } from 'react'
import type { StarAsk } from './star-ask'
+/** A stored value, or the fallback when it's missing, unreadable or of another kind (a `null` where a string
+ * belongs crashed the chat window). A `null` fallback takes any value. */
+function stored<T>(raw: string | null, fallback: T): T {
+ if (raw === null) return fallback
+ try {
+ const value: unknown = JSON.parse(raw)
+ const sameKind = value !== null && typeof value === typeof fallback && Array.isArray(value) === Array.isArray(fallback)
+ return fallback === null || sameKind ? (value as T) : fallback
+ } catch {
+ return fallback
+ }
+}
+
/** A per-window preference in localStorage (kit's `@AppStorage`), observable by views. */
export function pref<T>(key: string, fallback: T) {
const listeners = new Set<() => void>()
let value: T = (() => {
try {
- const raw = localStorage.getItem(key)
- return raw === null ? fallback : (JSON.parse(raw) as T)
+ return stored(localStorage.getItem(key), fallback)
} catch {
return fallback
}
})()
window.addEventListener('storage', (e) => {
if (e.key !== key) return
- try {
- value = e.newValue === null ? fallback : (JSON.parse(e.newValue) as T)
- } catch {
- value = fallback
- }
+ value = stored(e.newValue, fallback)
for (const l of listeners) l()
})
return {