Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sync problem #35

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions frontend/src/app/HomeContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default function HomeContent() {
const [selectedModel, setSelectedModel] = useState<string>(
models[0] || 'Loading models'
);
const [chatListUpdated, setChatListUpdated] = useState(false);

// Welcome dialog state
const [open, setOpen] = useState(false);
Expand Down Expand Up @@ -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: () => {
Expand Down Expand Up @@ -274,6 +276,8 @@ export default function HomeContent() {
formRef={formRef}
setMessages={setMessages}
setInput={setInput}
chatListUpdated={chatListUpdated} // Pass to ChatLayout
setChatListUpdated={setChatListUpdated} // Pass to ChatLayout
/>
<DialogContent className="flex flex-col space-y-4">
<DialogHeader className="space-y-2">
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/components/chat/chat-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface ChatLayoutProps {
formRef: React.RefObject<HTMLFormElement>;
setMessages: Dispatch<SetStateAction<Message[]>>;
setInput: Dispatch<SetStateAction<string>>;
chatListUpdated: boolean;
setChatListUpdated: React.Dispatch<React.SetStateAction<boolean>>;
}

export function ChatLayout({
Expand All @@ -42,6 +44,8 @@ export function ChatLayout({
formRef,
setMessages,
setInput,
chatListUpdated,
setChatListUpdated,
}: ChatLayoutProps) {
const [isCollapsed, setIsCollapsed] = useState(defaultCollapsed);
const [isMobile, setIsMobile] = useState(false);
Expand Down Expand Up @@ -91,7 +95,12 @@ export function ChatLayout({
: 'hidden md:block'
)}
>
<Sidebar isCollapsed={isCollapsed || isMobile} isMobile={isMobile} />
<Sidebar
isCollapsed={isCollapsed || isMobile}
isMobile={isMobile}
chatListUpdated={chatListUpdated}
setChatListUpdated={setChatListUpdated}
/>
</ResizablePanel>
<ResizableHandle className={cn('hidden md:flex')} withHandle />
<ResizablePanel
Expand Down
21 changes: 17 additions & 4 deletions frontend/src/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MoreHorizontal, SquarePen, Trash2 } from 'lucide-react';
import { cn } from '@/lib/utils';
import { Button, buttonVariants } from '@/components/ui/button';
import Image from 'next/image';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import SidebarSkeleton from './sidebar-skeleton';
import UserSettings from './user-settings';
import { useQuery, useMutation } from '@apollo/client';
Expand All @@ -30,6 +30,8 @@ interface SidebarProps {
isCollapsed: boolean;
isMobile: boolean;
currentChatId?: string;
chatListUpdated: boolean;
setChatListUpdated: React.Dispatch<React.SetStateAction<boolean>>;
}

// Define chat type based on the actual GraphQL response
Expand All @@ -44,17 +46,30 @@ export function Sidebar({
isCollapsed,
isMobile,
currentChatId,
chatListUpdated,
setChatListUpdated,
}: SidebarProps) {
const router = useRouter();
const [selectedChatId, setSelectedChatId] = useState<string | null>(
currentChatId || null
);

// 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 }],
Expand All @@ -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();
Expand Down
Loading