YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(chat-store): stop sidebar render loop — selector returned fresh array each call
Louis Beaumontf4ccfe6bf73a
diff --git a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx b/apps/screenpipe-app-tauri/components/chat-sidebar.tsxindex ec38b4c1f..4b9f8206f 100644--- a/apps/screenpipe-app-tauri/components/chat-sidebar.tsx+++ b/apps/screenpipe-app-tauri/components/chat-sidebar.tsx@@ -38,7 +38,7 @@ import { cn } from "@/lib/utils";import {useChatStore,useChatActions,- selectOrderedSessions,+ useOrderedSessions,type SessionRecord,} from "@/lib/stores/chat-store";import { updateConversationFlags } from "@/lib/chat-storage";@@ -48,7 +48,11 @@ interface ChatSidebarProps {}export function ChatSidebar({ className }: ChatSidebarProps) {- const sessions = useChatStore(selectOrderedSessions);+ // useOrderedSessions subscribes to the raw sessions map (stable identity+ // across no-op updates) and memoizes the sort. Avoids the+ // useSyncExternalStore infinite-loop trap of returning a fresh array+ // from the selector — see comment on selectOrderedSessions.+ const sessions = useOrderedSessions();const currentId = useChatStore((s) => s.currentId);const actions = useChatActions();diff --git a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts b/apps/screenpipe-app-tauri/lib/stores/chat-store.tsindex 2720dc66c..2c42f3499 100644--- a/apps/screenpipe-app-tauri/lib/stores/chat-store.ts+++ b/apps/screenpipe-app-tauri/lib/stores/chat-store.ts@@ -140,10 +140,31 @@ export const useChatStore = create<ChatStore>((set) => ({},}));-/** Stable selector for actions — avoids re-renders when state changes. */+/** Stable selector for actions — avoids re-renders when state changes.+ * `actions` is set once in the create() call and never replaced, so this+ * selector returns the same reference forever. Safe to use in deps. */export const useChatActions = () => useChatStore((s) => s.actions);-/** Sorted view: pinned first (most recent), then unpinned by updatedAt desc. */+/**+ * Sorted view: pinned first (most recent), then unpinned by updatedAt desc.+ *+ * IMPORTANT: this returns a fresh array on every call. Do NOT pass it+ * directly to `useChatStore(selectOrderedSessions)` — useSyncExternalStore+ * compares snapshots by Object.is, would see a new reference every render,+ * and re-render in an infinite loop.+ *+ * Two safe consumption patterns:+ *+ * // 1. via the dedicated hook below (memoized internally)+ * const sessions = useOrderedSessions();+ *+ * // 2. raw: subscribe to the underlying map and memoize per-component+ * const sessionsMap = useChatStore((s) => s.sessions);+ * const sessions = useMemo(+ * () => selectOrderedSessions({ sessions: sessionsMap } as ChatStore),+ * [sessionsMap]+ * );+ */export function selectOrderedSessions(state: ChatStore): SessionRecord[] {const all = Object.values(state.sessions);const pinned = all@@ -154,3 +175,24 @@ export function selectOrderedSessions(state: ChatStore): SessionRecord[] {.sort((a, b) => b.updatedAt - a.updatedAt);return [...pinned, ...recents];}++/**+ * Stable hook returning the ordered session list. Subscribes to the raw+ * `sessions` map (referentially stable across no-op updates) and memoizes+ * the sort. Component only re-renders when the map's identity changes —+ * which only happens when an entry is added / removed / mutated.+ */+import { useMemo } from "react";+export function useOrderedSessions(): SessionRecord[] {+ const sessionsMap = useChatStore((s) => s.sessions);+ return useMemo(() => {+ const all = Object.values(sessionsMap);+ const pinned = all+ .filter((s) => s.pinned)+ .sort((a, b) => b.updatedAt - a.updatedAt);+ const recents = all+ .filter((s) => !s.pinned)+ .sort((a, b) => b.updatedAt - a.updatedAt);+ return [...pinned, ...recents];+ }, [sessionsMap]);+}