Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf: debounce Zustand store persistence and deduplicate legacy cleanup

Chukwudi Nwobodo7a377fc5e4d6
diff --git a/apps/web/src/store.ts b/apps/web/src/store.ts
index 711e993e0..6e816804c 100644
--- a/apps/web/src/store.ts
+++ b/apps/web/src/store.ts
@@ -63,6 +63,8 @@ function readPersistedState(): AppState {
}
}
+let legacyKeysCleanedUp = false;
+
function persistState(state: AppState): void {
if (typeof window === "undefined") return;
try {
@@ -74,14 +76,29 @@ function persistState(state: AppState): void {
.map((project) => project.cwd),
}),
);
- for (const legacyKey of LEGACY_PERSISTED_STATE_KEYS) {
- window.localStorage.removeItem(legacyKey);
+ if (!legacyKeysCleanedUp) {
+ legacyKeysCleanedUp = true;
+ for (const legacyKey of LEGACY_PERSISTED_STATE_KEYS) {
+ window.localStorage.removeItem(legacyKey);
+ }
}
} catch {
// Ignore quota/storage errors to avoid breaking chat UX.
}
}
+let persistTimer: ReturnType<typeof setTimeout> | null = null;
+
+function debouncedPersistState(state: AppState): void {
+ if (persistTimer !== null) {
+ clearTimeout(persistTimer);
+ }
+ persistTimer = setTimeout(() => {
+ persistTimer = null;
+ persistState(state);
+ }, 500);
+}
+
// ── Pure helpers ──────────────────────────────────────────────────────
function updateThread(
@@ -395,8 +412,19 @@ export const useStore = create<AppStore>((set) => ({
set((state) => setThreadBranch(state, threadId, branch, worktreePath)),
}));
-// Persist on every state change
-useStore.subscribe((state) => persistState(state));
+// Persist state changes with debouncing to avoid localStorage thrashing
+useStore.subscribe((state) => debouncedPersistState(state));
+
+// Flush pending writes synchronously before page unload to prevent data loss.
+if (typeof window !== "undefined") {
+ window.addEventListener("beforeunload", () => {
+ if (persistTimer !== null) {
+ clearTimeout(persistTimer);
+ persistTimer = null;
+ persistState(useStore.getState());
+ }
+ });
+}
export function StoreProvider({ children }: { children: ReactNode }) {
useEffect(() => {