'use client' import { useState, useMemo } from 'react' import { Button } from '@/components/ui/button' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { ScrollArea } from '@/components/ui/scroll-area' import { Badge } from '@/components/ui/badge' import { MessageSquare, Plus, Trash2, Edit2, Check, X, Clock } from 'lucide-react' import { formatDistanceToNow } from 'date-fns' import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog' import { BaseChatSession } from '@/lib/types/api' import { useModels } from '@/lib/hooks/use-models' interface SessionManagerProps { sessions: BaseChatSession[] currentSessionId: string | null onCreateSession: (title: string) => void onSelectSession: (sessionId: string) => void onUpdateSession: (sessionId: string, title: string) => void onDeleteSession: (sessionId: string) => void loadingSessions: boolean } export function SessionManager({ sessions, currentSessionId, onCreateSession, onSelectSession, onUpdateSession, onDeleteSession, loadingSessions }: SessionManagerProps) { const [isCreating, setIsCreating] = useState(false) const [newSessionTitle, setNewSessionTitle] = useState('') const [editingId, setEditingId] = useState(null) const [editTitle, setEditTitle] = useState('') const [deleteConfirmId, setDeleteConfirmId] = useState(null) const { data: models } = useModels() // Helper to get model name from ID const getModelName = useMemo(() => { return (modelId: string) => { const model = models?.find(m => m.id === modelId) return model?.name || 'Custom Model' } }, [models]) const handleCreateSession = () => { if (newSessionTitle.trim()) { onCreateSession(newSessionTitle.trim()) setNewSessionTitle('') setIsCreating(false) } } const handleStartEdit = (session: BaseChatSession) => { setEditingId(session.id) setEditTitle(session.title) } const handleSaveEdit = () => { if (editingId && editTitle.trim()) { onUpdateSession(editingId, editTitle.trim()) setEditingId(null) setEditTitle('') } } const handleCancelEdit = () => { setEditingId(null) setEditTitle('') } const handleDeleteConfirm = () => { if (deleteConfirmId) { onDeleteSession(deleteConfirmId) setDeleteConfirmId(null) } } return ( <> Chat Sessions {isCreating && (
setNewSessionTitle(e.target.value)} placeholder="Session title..." className="mb-2" autoFocus onKeyPress={(e) => { if (e.key === 'Enter') handleCreateSession() }} />
)} {loadingSessions ? (
Loading sessions...
) : sessions.length === 0 ? (

No chat sessions yet

Create a session to start chatting

) : (
{sessions.map((session) => (
onSelectSession(session.id)} > {editingId === session.id ? (
e.stopPropagation()}> setEditTitle(e.target.value)} onKeyPress={(e) => { if (e.key === 'Enter') handleSaveEdit() if (e.key === 'Escape') handleCancelEdit() }} autoFocus />
) : ( <>

{session.title}

e.stopPropagation()}>
{formatDistanceToNow(new Date(session.created), { addSuffix: true })}
{session.message_count != null && session.message_count > 0 && ( {session.message_count} messages )} {session.model_override && ( {getModelName(session.model_override)} )} )}
))}
)}
setDeleteConfirmId(null)}> Delete Chat Session? This action cannot be undone. All messages in this session will be permanently deleted. Cancel Delete ) }