From 75fd39c249ba8eba832e342ea8da9f7cc48c9a29 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Sun, 29 Mar 2026 16:11:00 +0530 Subject: [PATCH] refactor: simplify author metadata handling in NewChatPage and UserMessage components --- .../new-chat/[[...chat_id]]/page.tsx | 30 ++++++++----------- .../components/assistant-ui/user-message.tsx | 5 +++- 2 files changed, 17 insertions(+), 18 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 1f9074793..5b35c0284 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 @@ -218,15 +218,13 @@ export default function NewChatPage() { return; } - const isSharedChat = currentThread?.visibility === "SEARCH_SPACE"; - setMessages((prev) => { if (syncedMessages.length < prev.length) { return prev; } return syncedMessages.map((msg) => { - const member = isSharedChat && msg.author_id + const member = msg.author_id ? membersData?.find((m) => m.user_id === msg.author_id) : null; @@ -241,7 +239,7 @@ export default function NewChatPage() { thread_id: msg.thread_id, role: msg.role.toLowerCase() as "user" | "assistant" | "system", content: msg.content, - author_id: isSharedChat ? msg.author_id : null, + author_id: msg.author_id, created_at: msg.created_at, author_display_name: member?.user_display_name ?? existingAuthor?.displayName ?? null, author_avatar_url: member?.user_avatar_url ?? existingAuthor?.avatarUrl ?? null, @@ -249,7 +247,7 @@ export default function NewChatPage() { }); }); }, - [isRunning, membersData, currentThread?.visibility] + [isRunning, membersData] ); useMessagesSync(threadId, handleSyncedMessagesUpdate); @@ -485,18 +483,17 @@ export default function NewChatPage() { // Add user message to state const userMsgId = `msg-user-${Date.now()}`; - // Include author metadata for shared chats - const authorMetadata = - currentThread?.visibility === "SEARCH_SPACE" && currentUser - ? { - custom: { - author: { - displayName: currentUser.display_name ?? null, - avatarUrl: currentUser.avatar_url ?? null, - }, + // Always include author metadata so the UI layer can decide visibility + const authorMetadata = currentUser + ? { + custom: { + author: { + displayName: currentUser.display_name ?? null, + avatarUrl: currentUser.avatar_url ?? null, }, - } - : undefined; + }, + } + : undefined; const userMessage: ThreadMessageLike = { id: userMsgId, @@ -884,7 +881,6 @@ export default function NewChatPage() { setMessageDocumentsMap, setAgentCreatedDocuments, queryClient, - currentThread, currentUser, disabledTools, updateChatTabTitle, diff --git a/surfsense_web/components/assistant-ui/user-message.tsx b/surfsense_web/components/assistant-ui/user-message.tsx index 74461e760..34945c472 100644 --- a/surfsense_web/components/assistant-ui/user-message.tsx +++ b/surfsense_web/components/assistant-ui/user-message.tsx @@ -3,6 +3,7 @@ import { useAtomValue } from "jotai"; import { CheckIcon, CopyIcon, FileText, Pen } from "lucide-react"; import Image from "next/image"; import { type FC, useState } from "react"; +import { currentThreadAtom } from "@/atoms/chat/current-thread.atom"; import { messageDocumentsMapAtom } from "@/atoms/chat/mentioned-documents.atom"; import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button"; @@ -51,6 +52,8 @@ export const UserMessage: FC = () => { const mentionedDocs = messageId ? messageDocumentsMap[messageId] : undefined; const metadata = useAuiState(({ message }) => message?.metadata); const author = metadata?.custom?.author as AuthorMetadata | undefined; + const isSharedChat = useAtomValue(currentThreadAtom).visibility === "SEARCH_SPACE"; + const showAvatar = isSharedChat && !!author; return ( { - {author && ( + {showAvatar && (