From 24472c0ea6137b3e787a92c584978f265cca2d8e Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Tue, 27 Jan 2026 10:50:37 +0200 Subject: [PATCH] fix: preserve author info in cloned chats and force PGlite resync after clone --- .../[search_space_id]/new-chat/[[...chat_id]]/page.tsx | 10 ++++++++-- surfsense_web/hooks/use-inbox.ts | 9 +++++++++ surfsense_web/lib/electric/client.ts | 10 ---------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx index 9b45d4d62..a56cd84ce 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx @@ -187,6 +187,12 @@ export default function NewChatPage() { ? membersData?.find((m) => m.user_id === msg.author_id) : null; + // Preserve existing author info if member lookup fails (e.g., cloned chats) + const existingMsg = prev.find((m) => m.id === `msg-${msg.id}`); + const existingAuthor = existingMsg?.metadata?.custom?.author as + | { displayName?: string | null; avatarUrl?: string | null } + | undefined; + return convertToThreadMessage({ id: msg.id, thread_id: msg.thread_id, @@ -194,8 +200,8 @@ export default function NewChatPage() { content: msg.content, author_id: msg.author_id, created_at: msg.created_at, - author_display_name: member?.user_display_name ?? null, - author_avatar_url: member?.user_avatar_url ?? null, + author_display_name: member?.user_display_name ?? existingAuthor?.displayName ?? null, + author_avatar_url: member?.user_avatar_url ?? existingAuthor?.avatarUrl ?? null, }); }); }); diff --git a/surfsense_web/hooks/use-inbox.ts b/surfsense_web/hooks/use-inbox.ts index 4c26ddcb9..656de18a8 100644 --- a/surfsense_web/hooks/use-inbox.ts +++ b/surfsense_web/hooks/use-inbox.ts @@ -119,6 +119,15 @@ export function useInbox( async function startSync() { try { + // Check for force resync flag (e.g., after clone from public page) + if (localStorage.getItem("surfsense_force_notif_resync") === "true") { + console.log("[useInbox] Force resync flag detected, clearing notifications"); + await client.db.exec("DELETE FROM notifications"); + localStorage.removeItem("surfsense_force_notif_resync"); + // Reset sync key to force a fresh sync + userSyncKeyRef.current = null; + } + const cutoffDate = getSyncCutoffDate(); const userSyncKey = `inbox_${userId}_${cutoffDate}`; diff --git a/surfsense_web/lib/electric/client.ts b/surfsense_web/lib/electric/client.ts index 4e7ff87e7..148da58ec 100644 --- a/surfsense_web/lib/electric/client.ts +++ b/surfsense_web/lib/electric/client.ts @@ -274,16 +274,6 @@ export async function initElectric(userId: string): Promise { CREATE INDEX IF NOT EXISTS idx_new_chat_messages_created_at ON new_chat_messages(created_at); `); - // Force resync notifications if flagged (e.g., after clone from public page) - if ( - typeof window !== "undefined" && - localStorage.getItem("surfsense_force_notif_resync") === "true" - ) { - console.log("[Electric] Force resync flag detected, clearing notifications table"); - await db.exec("DELETE FROM notifications"); - localStorage.removeItem("surfsense_force_notif_resync"); - } - const electricUrl = getElectricUrl(); // STEP 4: Create the client wrapper