From 83a58f1a581917fa8bdd9867905d5f778dc34ced Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 18 Jun 2026 17:43:04 +0200 Subject: [PATCH 1/3] refac --- .../src/lib/components/Sidebar.svelte | 93 +++++++----- .../src/lib/components/chat/ChatPanel.svelte | 22 +-- cptr/frontend/src/lib/stores/chat.ts | 143 +++++++++--------- cptr/frontend/src/lib/stores/socket.svelte.ts | 40 ++++- cptr/frontend/src/routes/+layout.svelte | 43 ++++-- 5 files changed, 195 insertions(+), 146 deletions(-) diff --git a/cptr/frontend/src/lib/components/Sidebar.svelte b/cptr/frontend/src/lib/components/Sidebar.svelte index 07226343..90aee35b 100644 --- a/cptr/frontend/src/lib/components/Sidebar.svelte +++ b/cptr/frontend/src/lib/components/Sidebar.svelte @@ -10,7 +10,7 @@ sidebarWidth, appVersion, showChangelog, - showSearch, + showSearch } from '$lib/stores'; import Sortable from 'sortablejs'; import Icon from './Icon.svelte'; @@ -41,6 +41,7 @@ let menuButtonEl: HTMLButtonElement | undefined = $state(); let wsListEl: HTMLDivElement | undefined = $state(); let sortable: Sortable | null = null; + let unbindSocketListener: (() => void) | null = null; // ── Per-workspace chat history ────────────────────────────── let expandedWorkspaces = $state>(new Set()); @@ -119,7 +120,13 @@ // Socket listener for chat events — invalidate cache when chats update const seenChatIds = new Set(); - function handleChatEvent(data: { chat_id: string; done?: boolean; title?: string; delta?: string; workspace?: string }) { + function handleChatEvent(data: { + chat_id: string; + done?: boolean; + title?: string; + delta?: string; + workspace?: string; + }) { // Re-fetch on done, title update, or first event of a new chat const isNew = !seenChatIds.has(data.chat_id); seenChatIds.add(data.chat_id); @@ -237,23 +244,13 @@ } // Bind socket listener for chat cache invalidation - const tryBind = () => { - const socket = socketStore.getSocket(); - if (!socket) { - setTimeout(tryBind, 200); - return; - } - socket.on('events:chat', handleChatEvent); - }; - tryBind(); + unbindSocketListener = socketStore.on('events:chat', handleChatEvent); }); onDestroy(() => { sortable?.destroy(); - const socket = socketStore.getSocket(); - if (socket) { - socket.off('events:chat', handleChatEvent); - } + unbindSocketListener?.(); + unbindSocketListener = null; }); @@ -307,26 +304,31 @@ {$t('search.search')} - + {#if $chatEnabled} - + {/if} @@ -349,10 +351,12 @@ {@const chats = wsChatsCache.get(ws.path)} {@const isLoading = wsChatsLoading.has(ws.path)}
- - -