fix(web): settings save again after a stored value fails decode (#19312)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
2d14185f32
commit
4f909be2c1
2 files changed
+61
-1
No files matched your search
@@ -0,0 +1,53 @@
|
||||
// @vitest-environment jsdom
|
||||
import * as Schema from "effect/Schema";
|
||||
import { act, createElement, useEffect } from "react";
|
||||
import { createRoot, type Root } from "react-dom/client";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import { useLocalStorage } from "./useLocalStorage";
|
||||
|
||||
const KEY = "corrupt-key";
|
||||
let root: Root;
|
||||
let current: ReturnType<typeof useLocalStorage<number, number>>;
|
||||
|
||||
function Probe() {
|
||||
const result = useLocalStorage(KEY, 0, Schema.Number);
|
||||
useEffect(() => {
|
||||
current = result;
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
||||
vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
// Valid JSON that no longer matches the schema, as after a schema change.
|
||||
localStorage.setItem(KEY, JSON.stringify("not a number"));
|
||||
root = createRoot(document.createElement("div"));
|
||||
await act(() => root.render(createElement(Probe)));
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await act(() => root.unmount());
|
||||
localStorage.clear();
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("useLocalStorage with a stored value that fails decode", () => {
|
||||
it("replaces it with a plain write", async () => {
|
||||
expect(current[0]).toBe(0);
|
||||
|
||||
await act(() => current[1](42));
|
||||
|
||||
expect(localStorage.getItem(KEY)).toBe("42");
|
||||
expect(current[0]).toBe(42);
|
||||
});
|
||||
|
||||
it("runs an updater from the initial value", async () => {
|
||||
await act(() => current[1]((value) => value + 1));
|
||||
|
||||
expect(localStorage.getItem(KEY)).toBe("1");
|
||||
expect(current[0]).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -150,9 +150,16 @@ export function useLocalStorage<T, E>(
|
||||
const setValue = useCallback(
|
||||
(value: T | ((val: T) => T)) => {
|
||||
try {
|
||||
const currentValue = getLocalStorageItem(key, schema) ?? initialValue;
|
||||
let valueToStore: T;
|
||||
if (typeof value === "function") {
|
||||
// Like the read path, treat an unreadable stored value as the initial
|
||||
// value, so a corrupt blob cannot block every later write.
|
||||
let currentValue = initialValue;
|
||||
try {
|
||||
currentValue = getLocalStorageItem(key, schema) ?? initialValue;
|
||||
} catch (error) {
|
||||
console.error("[LOCALSTORAGE] Could not read stored value.", error);
|
||||
}
|
||||
try {
|
||||
valueToStore = (value as (val: T) => T)(currentValue);
|
||||
} catch (cause) {
|
||||
|
||||
Reference in new issue
Block a user