@devries-cameron20 / t3code Mirror · read-only / commits / 4f909be2c1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 14:15 UTC

fix(web): settings save again after a stored value fails decode (#19312)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 14, 2026 at 01:37 UTC 4f909be2c1a2eb4a88e6a51c8d0ef25e47a027d2
2 files changed +61 -1
apps/web/src/hooks/useLocalStorage.hook.test.tsx new
+53
@@ -0,0 +1,53 @@
1 +// @vitest-environment jsdom
2 +import * as Schema from "effect/Schema";
3 +import { act, createElement, useEffect } from "react";
4 +import { createRoot, type Root } from "react-dom/client";
5 +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6 +
7 +import { useLocalStorage } from "./useLocalStorage";
8 +
9 +const KEY = "corrupt-key";
10 +let root: Root;
11 +let current: ReturnType<typeof useLocalStorage<number, number>>;
12 +
13 +function Probe() {
14 + const result = useLocalStorage(KEY, 0, Schema.Number);
15 + useEffect(() => {
16 + current = result;
17 + });
18 + return null;
19 +}
20 +
21 +beforeEach(async () => {
22 + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
23 + vi.spyOn(console, "error").mockImplementation(() => {});
24 + // Valid JSON that no longer matches the schema, as after a schema change.
25 + localStorage.setItem(KEY, JSON.stringify("not a number"));
26 + root = createRoot(document.createElement("div"));
27 + await act(() => root.render(createElement(Probe)));
28 +});
29 +
30 +afterEach(async () => {
31 + await act(() => root.unmount());
32 + localStorage.clear();
33 + vi.restoreAllMocks();
34 + vi.unstubAllGlobals();
35 +});
36 +
37 +describe("useLocalStorage with a stored value that fails decode", () => {
38 + it("replaces it with a plain write", async () => {
39 + expect(current[0]).toBe(0);
40 +
41 + await act(() => current[1](42));
42 +
43 + expect(localStorage.getItem(KEY)).toBe("42");
44 + expect(current[0]).toBe(42);
45 + });
46 +
47 + it("runs an updater from the initial value", async () => {
48 + await act(() => current[1]((value) => value + 1));
49 +
50 + expect(localStorage.getItem(KEY)).toBe("1");
51 + expect(current[0]).toBe(1);
52 + });
53 +});
apps/web/src/hooks/useLocalStorage.ts
+8 -1
@@ -150,9 +150,16 @@ export function useLocalStorage<T, E>(
150 const setValue = useCallback(
151 (value: T | ((val: T) => T)) => {
152 try {
153 - const currentValue = getLocalStorageItem(key, schema) ?? initialValue;
153 let valueToStore: T;
154 if (typeof value === "function") {
155 + // Like the read path, treat an unreadable stored value as the initial
156 + // value, so a corrupt blob cannot block every later write.
157 + let currentValue = initialValue;
158 + try {
159 + currentValue = getLocalStorageItem(key, schema) ?? initialValue;
160 + } catch (error) {
161 + console.error("[LOCALSTORAGE] Could not read stored value.", error);
162 + }
163 try {
164 valueToStore = (value as (val: T) => T)(currentValue);
165 } catch (cause) {