|
| 1 | +import { type FC } from 'react'; |
| 2 | +import { useState, useCallback, useEffect } from 'react'; |
| 3 | +import { setDocumentTitle } from '@/utils/title'; |
| 4 | +import { useQuery, useQueryClient } from '@tanstack/react-query'; |
| 5 | +import { LeftSidebar } from '@/components/LeftSidebar'; |
| 6 | +import { RightSidebar } from '@/components/RightSidebar'; |
| 7 | +import { ConversationContent } from '@/components/ConversationContent'; |
| 8 | +import { useApi } from '@/contexts/ApiContext'; |
| 9 | +import type { ConversationItem } from '@/components/ConversationList'; |
| 10 | +import { toConversationItems } from '@/utils/conversation'; |
| 11 | +import { demoConversations, type DemoConversation } from '@/democonversations'; |
| 12 | +import { useSearchParams, useNavigate } from 'react-router-dom'; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + className?: string; |
| 16 | + route: string; |
| 17 | +} |
| 18 | + |
| 19 | +const Conversations: FC<Props> = ({ route }) => { |
| 20 | + const [leftSidebarOpen, setLeftSidebarOpen] = useState(true); |
| 21 | + const [rightSidebarOpen, setRightSidebarOpen] = useState(false); |
| 22 | + const [searchParams] = useSearchParams(); |
| 23 | + const navigate = useNavigate(); |
| 24 | + const conversationParam = searchParams.get('conversation'); |
| 25 | + const [selectedConversation, setSelectedConversation] = useState<string>( |
| 26 | + conversationParam || demoConversations[0].name |
| 27 | + ); |
| 28 | + const { api, isConnected, connectionConfig } = useApi(); |
| 29 | + const queryClient = useQueryClient(); |
| 30 | + |
| 31 | + // Update selected conversation when URL param changes |
| 32 | + useEffect(() => { |
| 33 | + if (conversationParam) { |
| 34 | + setSelectedConversation(conversationParam); |
| 35 | + } |
| 36 | + }, [conversationParam]); |
| 37 | + |
| 38 | + // Fetch conversations from API with proper caching |
| 39 | + const { |
| 40 | + data: apiConversations = [], |
| 41 | + isError, |
| 42 | + error, |
| 43 | + isLoading, |
| 44 | + refetch, |
| 45 | + } = useQuery({ |
| 46 | + queryKey: ['conversations', connectionConfig.baseUrl, isConnected], |
| 47 | + queryFn: async () => { |
| 48 | + console.log('Fetching conversations, connection state:', isConnected); |
| 49 | + if (!isConnected) { |
| 50 | + console.warn('Attempting to fetch conversations while disconnected'); |
| 51 | + return []; |
| 52 | + } |
| 53 | + try { |
| 54 | + const conversations = await api.getConversations(); |
| 55 | + console.log('Fetched conversations:', conversations); |
| 56 | + return conversations; |
| 57 | + } catch (err) { |
| 58 | + console.error('Failed to fetch conversations:', err); |
| 59 | + throw err; |
| 60 | + } |
| 61 | + }, |
| 62 | + enabled: isConnected, |
| 63 | + staleTime: 0, // Always refetch when query is invalidated |
| 64 | + gcTime: 5 * 60 * 1000, |
| 65 | + }); |
| 66 | + |
| 67 | + // Log any query errors |
| 68 | + if (isError) { |
| 69 | + console.error('Conversation query error:', error); |
| 70 | + } |
| 71 | + |
| 72 | + // Combine demo and API conversations |
| 73 | + const allConversations: ConversationItem[] = [ |
| 74 | + // Convert demo conversations to ConversationItems |
| 75 | + ...demoConversations.map((conv: DemoConversation) => ({ |
| 76 | + name: conv.name, |
| 77 | + lastUpdated: conv.lastUpdated, |
| 78 | + messageCount: conv.messages.length, |
| 79 | + readonly: true, |
| 80 | + })), |
| 81 | + // Convert API conversations to ConversationItems |
| 82 | + ...toConversationItems(apiConversations), |
| 83 | + ]; |
| 84 | + |
| 85 | + const handleSelectConversation = useCallback( |
| 86 | + (id: string) => { |
| 87 | + if (id === selectedConversation) { |
| 88 | + return; |
| 89 | + } |
| 90 | + // Cancel any pending queries for the previous conversation |
| 91 | + queryClient.cancelQueries({ |
| 92 | + queryKey: ['conversation', selectedConversation], |
| 93 | + }); |
| 94 | + setSelectedConversation(id); |
| 95 | + // Update URL with the new conversation ID |
| 96 | + console.log(`[Conversations] [handleSelectConversation] id: ${id}`); |
| 97 | + navigate(`${route}?conversation=${id}`); |
| 98 | + }, |
| 99 | + [selectedConversation, queryClient, navigate, route] |
| 100 | + ); |
| 101 | + |
| 102 | + const conversation = allConversations.find((conv) => conv.name === selectedConversation); |
| 103 | + |
| 104 | + // Update document title when selected conversation changes |
| 105 | + useEffect(() => { |
| 106 | + if (conversation) { |
| 107 | + setDocumentTitle(conversation.name); |
| 108 | + } else { |
| 109 | + setDocumentTitle(); |
| 110 | + } |
| 111 | + return () => setDocumentTitle(); // Reset title on unmount |
| 112 | + }, [conversation]); |
| 113 | + |
| 114 | + return ( |
| 115 | + <div className="flex flex-1 overflow-hidden"> |
| 116 | + <LeftSidebar |
| 117 | + isOpen={leftSidebarOpen} |
| 118 | + onToggle={() => setLeftSidebarOpen(!leftSidebarOpen)} |
| 119 | + conversations={allConversations} |
| 120 | + selectedConversationId={selectedConversation} |
| 121 | + onSelectConversation={handleSelectConversation} |
| 122 | + isLoading={isLoading} |
| 123 | + isError={isError} |
| 124 | + error={error as Error} |
| 125 | + onRetry={() => refetch()} |
| 126 | + route={route} |
| 127 | + /> |
| 128 | + {conversation ? <ConversationContent conversation={conversation} /> : null} |
| 129 | + <RightSidebar |
| 130 | + isOpen={rightSidebarOpen} |
| 131 | + onToggle={() => setRightSidebarOpen(!rightSidebarOpen)} |
| 132 | + /> |
| 133 | + </div> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +export default Conversations; |
0 commit comments