diff --git a/frontend/src/app/HomeContent.tsx b/frontend/src/app/HomeContent.tsx index 6622517b..2d1a6602 100644 --- a/frontend/src/app/HomeContent.tsx +++ b/frontend/src/app/HomeContent.tsx @@ -55,6 +55,7 @@ export default function HomeContent() { const [selectedModel, setSelectedModel] = useState( models[0] || 'Loading models' ); + const [chatListUpdated, setChatListUpdated] = useState(false); // Welcome dialog state const [open, setOpen] = useState(false); @@ -165,6 +166,7 @@ export default function HomeContent() { onCompleted: async (data) => { const newChatId = data.createChat.id; setChatId(newChatId); + setChatListUpdated(true); await startChatStream(newChatId, input); }, onError: () => { @@ -274,6 +276,8 @@ export default function HomeContent() { formRef={formRef} setMessages={setMessages} setInput={setInput} + chatListUpdated={chatListUpdated} // Pass to ChatLayout + setChatListUpdated={setChatListUpdated} // Pass to ChatLayout /> diff --git a/frontend/src/components/chat/chat-layout.tsx b/frontend/src/components/chat/chat-layout.tsx index 6132934e..ca581606 100644 --- a/frontend/src/components/chat/chat-layout.tsx +++ b/frontend/src/components/chat/chat-layout.tsx @@ -25,6 +25,8 @@ interface ChatLayoutProps { formRef: React.RefObject; setMessages: Dispatch>; setInput: Dispatch>; + chatListUpdated: boolean; + setChatListUpdated: React.Dispatch>; } export function ChatLayout({ @@ -42,6 +44,8 @@ export function ChatLayout({ formRef, setMessages, setInput, + chatListUpdated, + setChatListUpdated, }: ChatLayoutProps) { const [isCollapsed, setIsCollapsed] = useState(defaultCollapsed); const [isMobile, setIsMobile] = useState(false); @@ -91,7 +95,12 @@ export function ChatLayout({ : 'hidden md:block' )} > - + >; } // Define chat type based on the actual GraphQL response @@ -44,6 +46,8 @@ export function Sidebar({ isCollapsed, isMobile, currentChatId, + chatListUpdated, + setChatListUpdated, }: SidebarProps) { const router = useRouter(); const [selectedChatId, setSelectedChatId] = useState( @@ -51,10 +55,21 @@ export function Sidebar({ ); // Query user chats + // const { data, loading, error } = useQuery(GET_USER_CHATS, { + // fetchPolicy: 'network-only', + // }); const { data, loading, error } = useQuery(GET_USER_CHATS, { - fetchPolicy: 'network-only', + fetchPolicy: chatListUpdated ? 'network-only' : 'cache-first', }); + const chats: Chat[] = data?.getUserChats || []; + + useEffect(() => { + if (chatListUpdated) { + setChatListUpdated(false); + } + }, [chatListUpdated, setChatListUpdated]); + // Delete chat mutation const [deleteChat] = useMutation(DELETE_CHAT, { refetchQueries: [{ query: GET_USER_CHATS }], @@ -70,8 +85,6 @@ export function Sidebar({ return null; } - const chats: Chat[] = data?.getUserChats || []; - // Sort chats by creation date const sortedChats = [...chats].sort((a, b) => { return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();