import React, { useEffect, useState } from 'react'; import clsx from 'clsx'; import { fetchSessions, createSession, deleteSession, updateSession, type ChatSession } from '@/api/ai'; interface SessionListProps { activeSessionId: string | null; onSelectSession: (id: string) => void; isSidebar?: boolean; className?: string; } export function SessionList({ activeSessionId, onSelectSession, isSidebar, className }: SessionListProps) { const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const loadSessions = async () => { try { setLoading(true); const data = await fetchSessions(isSidebar ? true : undefined); setSessions(data); setError(null); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to load sessions'); } finally { setLoading(false); } }; useEffect(() => { loadSessions(); }, [isSidebar]); const handleNewChat = async () => { try { const session = await createSession({ is_sidebar: isSidebar || false }); setSessions((prev) => [session, ...prev]); onSelectSession(session.id); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to create session'); } }; const handleDelete = async (id: string, e: React.MouseEvent) => { e.stopPropagation(); try { await deleteSession(id); setSessions((prev) => prev.filter((s) => s.id !== id)); if (activeSessionId === id) { onSelectSession(''); } } catch (e) { setError(e instanceof Error ? e.message : 'Failed to delete session'); } }; if (loading) { return
Laden...
; } return (
{error &&
{error}
}
{sessions.length === 0 ? (
Keine Chats vorhanden
) : (
    {sessions.map((session) => (
  • onSelectSession(session.id)} className={clsx( 'group flex items-center gap-2 rounded-lg px-3 py-2 text-sm cursor-pointer', 'hover:bg-secondary-100', activeSessionId === session.id && 'bg-primary-100 text-primary-700' )} > {session.title}
  • ))}
)}
); }