@devries-cameron20 / t3code Mirror · read-only / commits / 70c73d96e0
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, 08:00 UTC

perf(web): useLocalStorage compiles with the React Compiler (#20472)

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

Julius Marminge committed Oct 16, 2026 at 18:46 UTC 70c73d96e0f395c3cea59266699a6d92045959ed
2 files changed +93 -80
apps/web/reactCompiledFiles.ts
+1
@@ -47,4 +47,5 @@ export const REACT_COMPILED_FILES = [
47 "apps/web/src/components/onboarding/WelcomeWizard.tsx",
48 "apps/web/src/hooks/useThreadActions.ts",
49 "apps/web/src/components/pullRequest/PullRequestCodeTab.tsx",
50 + "apps/web/src/hooks/useLocalStorage.ts",
51 ] as const;
apps/web/src/hooks/useLocalStorage.ts
+92 -80
@@ -1,6 +1,6 @@
1 import * as Schema from "effect/Schema";
2 import * as Record from "effect/Record";
3 -import { useCallback, useMemo, useSyncExternalStore } from "react";
3 +import { useSyncExternalStore } from "react";
4
5 export class LocalStorageOperationError extends Schema.TaggedError<LocalStorageOperationError>()(
6 "LocalStorageOperationError",
@@ -97,93 +97,105 @@ function dispatchLocalStorageChange(key: string) {
97 }
98 }
99
100 +function readSerializedValue(key: string): string | null {
101 + try {
102 + return read(key);
103 + } catch (error) {
104 + console.error("[LOCALSTORAGE] Could not read stored value.", error);
105 + return null;
106 + }
107 +}
108 +
109 +function decodeOrInitial<T, E>(
110 + key: string,
111 + schema: Schema.Codec<T, E>,
112 + serializedValue: string | null,
113 + initialValue: T,
114 +): T {
115 + if (serializedValue === null) {
116 + return initialValue;
117 + }
118 + try {
119 + return decode(key, schema, serializedValue);
120 + } catch (error) {
121 + console.error("[LOCALSTORAGE] Could not decode stored value.", error);
122 + return initialValue;
123 + }
124 +}
125 +
126 +function writeLocalStorageValue<T, E>(
127 + key: string,
128 + value: T | ((val: T) => T),
129 + initialValue: T,
130 + schema: Schema.Codec<T, E>,
131 +): void {
132 + try {
133 + let valueToStore: T;
134 + if (typeof value === "function") {
135 + // Like the read path, treat an unreadable stored value as the initial
136 + // value, so a corrupt blob cannot block every later write.
137 + let currentValue = initialValue;
138 + try {
139 + currentValue = getLocalStorageItem(key, schema) ?? initialValue;
140 + } catch (error) {
141 + console.error("[LOCALSTORAGE] Could not read stored value.", error);
142 + }
143 + try {
144 + valueToStore = (value as (val: T) => T)(currentValue);
145 + } catch (cause) {
146 + throw new LocalStorageOperationError({
147 + operation: "update",
148 + storageKey: key,
149 + cause,
150 + });
151 + }
152 + } else {
153 + valueToStore = value;
154 + }
155 + if (valueToStore === null) {
156 + removeLocalStorageItem(key);
157 + } else {
158 + setLocalStorageItem(key, valueToStore, schema);
159 + }
160 + dispatchLocalStorageChange(key);
161 + } catch (error) {
162 + console.error("[LOCALSTORAGE] Could not update stored value.", error);
163 + }
164 +}
165 +
166 export function useLocalStorage<T, E>(
167 key: string,
168 initialValue: T,
169 schema: Schema.Codec<T, E>,
170 ): [T, (value: T | ((val: T) => T)) => void] {
105 - const getSnapshot = useCallback(() => {
106 - try {
107 - return read(key);
108 - } catch (error) {
109 - console.error("[LOCALSTORAGE] Could not read stored value.", error);
110 - return null;
111 - }
112 - }, [key]);
113 -
114 - const subscribe = useCallback(
115 - (onStoreChange: () => void) => {
116 - const handleStorageChange = (event: StorageEvent) => {
117 - if (event.key === key) {
118 - onStoreChange();
119 - }
120 - };
121 - const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => {
122 - if (event.detail.key === key) {
123 - onStoreChange();
124 - }
125 - };
126 -
127 - window.addEventListener("storage", handleStorageChange);
128 - window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
129 - return () => {
130 - window.removeEventListener("storage", handleStorageChange);
131 - window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
132 - };
133 - },
134 - [key],
135 - );
171 + const getSnapshot = () => readSerializedValue(key);
172 +
173 + const subscribe = (onStoreChange: () => void) => {
174 + const handleStorageChange = (event: StorageEvent) => {
175 + if (event.key === key) {
176 + onStoreChange();
177 + }
178 + };
179 + const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => {
180 + if (event.detail.key === key) {
181 + onStoreChange();
182 + }
183 + };
184 +
185 + window.addEventListener("storage", handleStorageChange);
186 + window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
187 + return () => {
188 + window.removeEventListener("storage", handleStorageChange);
189 + window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
190 + };
191 + };
192
193 const serializedValue = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
138 - const storedValue = useMemo(() => {
139 - if (serializedValue === null) {
140 - return initialValue;
141 - }
142 - try {
143 - return decode(key, schema, serializedValue);
144 - } catch (error) {
145 - console.error("[LOCALSTORAGE] Could not decode stored value.", error);
146 - return initialValue;
147 - }
148 - }, [initialValue, key, schema, serializedValue]);
194 + const storedValue = decodeOrInitial(key, schema, serializedValue, initialValue);
195
150 - const setValue = useCallback(
151 - (value: T | ((val: T) => T)) => {
152 - try {
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) {
166 - throw new LocalStorageOperationError({
167 - operation: "update",
168 - storageKey: key,
169 - cause,
170 - });
171 - }
172 - } else {
173 - valueToStore = value;
174 - }
175 - if (valueToStore === null) {
176 - removeLocalStorageItem(key);
177 - } else {
178 - setLocalStorageItem(key, valueToStore, schema);
179 - }
180 - dispatchLocalStorageChange(key);
181 - } catch (error) {
182 - console.error("[LOCALSTORAGE] Could not update stored value.", error);
183 - }
184 - },
185 - [initialValue, key, schema],
186 - );
196 + const setValue = (value: T | ((val: T) => T)) => {
197 + writeLocalStorageValue(key, value, initialValue, schema);
198 + };
199
200 return [storedValue, setValue];
201 }