'use client' import { useMemo } from 'react' import { useNotebookChat } from '@/lib/hooks/useNotebookChat' import { useNotes } from '@/lib/hooks/use-notes' import { ChatPanel } from '@/components/source/ChatPanel' import { LoadingSpinner } from '@/components/common/LoadingSpinner' import { Card, CardContent } from '@/components/ui/card' import { AlertCircle } from 'lucide-react' import { ContextSelections } from '../[id]/page' import { useTranslation } from '@/lib/hooks/use-translation' import { SourceListResponse } from '@/lib/types/api' interface ChatColumnProps { notebookId: string contextSelections: ContextSelections sources: SourceListResponse[] sourcesLoading: boolean } export function ChatColumn({ notebookId, contextSelections, sources, sourcesLoading }: ChatColumnProps) { const { t } = useTranslation() // Fetch notes for this notebook const { data: notes = [], isLoading: notesLoading } = useNotes(notebookId) // Initialize notebook chat hook const chat = useNotebookChat({ notebookId, sources, notes, contextSelections }) // Calculate context stats for indicator const contextStats = useMemo(() => { let sourcesInsights = 0 let sourcesFull = 0 let notesCount = 0 // Count sources by mode sources.forEach(source => { const mode = contextSelections.sources[source.id] if (mode === 'insights') { sourcesInsights++ } else if (mode === 'full') { sourcesFull++ } }) // Count notes that are included (not 'off') notes.forEach(note => { const mode = contextSelections.notes[note.id] if (mode === 'full') { notesCount++ } }) return { sourcesInsights, sourcesFull, notesCount, tokenCount: chat.tokenCount, charCount: chat.charCount } }, [sources, notes, contextSelections, chat.tokenCount, chat.charCount]) // Show loading state while sources/notes are being fetched if (sourcesLoading || notesLoading) { return ( ) } // Show error state if data fetch failed (unlikely but good to handle) if (!sources && !notes) { return ( {t('chat.unableToLoadChat')} {t('common.refreshPage') || 'Please try refreshing the page'} ) } return ( chat.sendMessage(message, modelOverride)} modelOverride={chat.currentSession?.model_override ?? chat.pendingModelOverride ?? undefined} onModelChange={(model) => chat.setModelOverride(model ?? null)} sessions={chat.sessions} currentSessionId={chat.currentSessionId} onCreateSession={(title) => chat.createSession(title)} onSelectSession={chat.switchSession} onUpdateSession={(sessionId, title) => chat.updateSession(sessionId, { title })} onDeleteSession={chat.deleteSession} loadingSessions={chat.loadingSessions} notebookContextStats={contextStats} notebookId={notebookId} /> ) }
{t('chat.unableToLoadChat')}
{t('common.refreshPage') || 'Please try refreshing the page'}