Skip to content

Commit af77969

Browse files
levochkaaclaude
andcommitted
fix(web): resizing stays usable when storage rejects the panel width
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 463a966 commit af77969

3 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎apps/web/src/hooks/useLocalStorage.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function writeLocalStorageValue<T, E>(
128128
value: T | ((val: T) => T),
129129
initialValue: T,
130130
schema: Schema.Codec<T, E>,
131-
): void {
131+
): boolean {
132132
try {
133133
let valueToStore: T;
134134
if (typeof value === "function") {
@@ -157,8 +157,10 @@ export function writeLocalStorageValue<T, E>(
157157
setLocalStorageItem(key, valueToStore, schema);
158158
}
159159
dispatchLocalStorageChange(key);
160+
return true;
160161
} catch (error) {
161162
console.error("[LOCALSTORAGE] Could not update stored value.", error);
163+
return false;
162164
}
163165
}
164166

‎apps/web/src/hooks/useResizableWidth.browser.test.tsx‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import { describe, expect, it, vi } from "vite-plus/test";
22

33
import { act, render } from "../../test/render";
44
import { useResizableWidth } from "./useResizableWidth";
55

66
const STORAGE_KEY = "t3code:test:resizable-width";
77

88
function Probe() {
9-
const { width } = useResizableWidth({
9+
const { width, handlers } = useResizableWidth({
1010
storageKey: STORAGE_KEY,
1111
defaultWidth: 400,
1212
minWidth: 300,
1313
maxWidth: 800,
1414
edge: "left",
1515
});
16-
return <output>{width}</output>;
16+
return (
17+
<div>
18+
<output>{width}</output>
19+
<div role="separator" {...handlers} />
20+
</div>
21+
);
22+
}
23+
24+
function dragLeftBy(handle: Element, distance: number) {
25+
const init = { bubbles: true, button: 0, pointerId: 1 };
26+
handle.dispatchEvent(new PointerEvent("pointerdown", { ...init, clientX: 500 }));
27+
handle.dispatchEvent(new PointerEvent("pointermove", { ...init, clientX: 500 - distance }));
28+
handle.dispatchEvent(new PointerEvent("pointerup", { ...init, clientX: 500 - distance }));
1729
}
1830

1931
describe("useResizableWidth", () => {
@@ -32,4 +44,27 @@ describe("useResizableWidth", () => {
3244
});
3345
await expect.element(screen.getByText("640")).toBeInTheDocument();
3446
});
47+
48+
it("starts the next drag from a width storage rejected", async () => {
49+
using _storage = { [Symbol.dispose]: () => localStorage.removeItem(STORAGE_KEY) };
50+
await using screen = await render(<Probe />, {
51+
// A bare hook probe; nothing here navigates.
52+
router: false,
53+
});
54+
// Synthetic pointer ids are not active pointers, so the browser would reject capture.
55+
using _capture = vi
56+
.spyOn(HTMLElement.prototype, "setPointerCapture")
57+
.mockImplementation(() => {});
58+
using _write = vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {
59+
throw new DOMException("Storage is full.", "QuotaExceededError");
60+
});
61+
using _log = vi.spyOn(console, "error").mockImplementation(() => {});
62+
const handle = screen.getByRole("separator").element();
63+
64+
await act(() => dragLeftBy(handle, 100));
65+
await expect.element(screen.getByText("500")).toBeInTheDocument();
66+
67+
await act(() => dragLeftBy(handle, 50));
68+
await expect.element(screen.getByText("550")).toBeInTheDocument();
69+
});
3570
});

‎apps/web/src/hooks/useResizableWidth.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Schema from "effect/Schema";
2-
import { type PointerEvent as ReactPointerEvent, useLayoutEffect } from "react";
2+
import { type PointerEvent as ReactPointerEvent, useLayoutEffect, useState } from "react";
33

44
import { useLocalStorage, writeLocalStorageValue } from "./useLocalStorage";
55
import { useResizeDrag } from "./useResizeDrag";
@@ -41,10 +41,21 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
4141

4242
// Panels that share a key follow each other's drags, in this tab and in others.
4343
const [storedWidth] = useLocalStorage(storageKey, defaultWidth, WidthSchema);
44-
const clampedWidth = clamp(storedWidth);
44+
// Resizing remains usable for this session when browser storage is unavailable.
45+
const [unsaved, setUnsaved] = useState<{
46+
readonly storageKey: string;
47+
readonly storedWidth: number;
48+
readonly width: number;
49+
} | null>(null);
50+
const clampedWidth = clamp(
51+
unsaved?.storageKey === storageKey && unsaved.storedWidth === storedWidth
52+
? unsaved.width
53+
: storedWidth,
54+
);
4555

4656
const commitWidth = (nextStorageKey: string, nextWidth: number) => {
47-
writeLocalStorageValue(nextStorageKey, nextWidth, defaultWidth, WidthSchema);
57+
const saved = writeLocalStorageValue(nextStorageKey, nextWidth, defaultWidth, WidthSchema);
58+
setUnsaved(saved ? null : { storageKey: nextStorageKey, storedWidth, width: nextWidth });
4859
};
4960

5061
const drag = useResizeDrag<HTMLElement>((event) => {

0 commit comments

Comments
 (0)