2026-07-17 00:52:17 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
import clsx from 'clsx';
|
2026-07-17 09:21:40 +02:00
|
|
|
import {
|
|
|
|
|
fetchSessions, createSession, deleteSession, updateSession,
|
|
|
|
|
fetchFolders, createFolder, deleteFolder, updateFolder,
|
|
|
|
|
type ChatSession, type ChatFolder,
|
|
|
|
|
} from '@/api/ai';
|
2026-07-17 00:52:17 +02:00
|
|
|
|
|
|
|
|
interface SessionListProps {
|
|
|
|
|
activeSessionId: string | null;
|
|
|
|
|
onSelectSession: (id: string) => void;
|
|
|
|
|
isSidebar?: boolean;
|
|
|
|
|
className?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
interface TreeNode {
|
|
|
|
|
folder: ChatFolder | null;
|
|
|
|
|
sessions: ChatSession[];
|
|
|
|
|
children: TreeNode[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildTree(folders: ChatFolder[], sessions: ChatSession[]): TreeNode[] {
|
|
|
|
|
const rootSessions = sessions.filter((s) => !s.folder_id);
|
|
|
|
|
const root: TreeNode = { folder: null, sessions: rootSessions, children: [] };
|
|
|
|
|
const folderMap = new Map<string, TreeNode>();
|
2026-07-17 17:09:26 +02:00
|
|
|
for (const f of folders) folderMap.set(f.id, { folder: f, sessions: [], children: [] });
|
2026-07-17 09:21:40 +02:00
|
|
|
for (const f of folders) {
|
|
|
|
|
const node = folderMap.get(f.id)!;
|
2026-07-17 17:09:26 +02:00
|
|
|
if (f.parent_id && folderMap.has(f.parent_id)) folderMap.get(f.parent_id)!.children.push(node);
|
|
|
|
|
else root.children.push(node);
|
2026-07-17 09:21:40 +02:00
|
|
|
}
|
|
|
|
|
for (const s of sessions) {
|
2026-07-17 17:09:26 +02:00
|
|
|
if (s.folder_id && folderMap.has(s.folder_id)) folderMap.get(s.folder_id)!.sessions.push(s);
|
2026-07-17 09:21:40 +02:00
|
|
|
}
|
|
|
|
|
return [root];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function TreeItem({
|
2026-07-17 17:09:26 +02:00
|
|
|
node, activeSessionId, onSelectSession, depth, onMoveSession, onDeleteFolder, onAddFolder, onDragSession, onDropToFolder,
|
2026-07-17 09:21:40 +02:00
|
|
|
}: {
|
|
|
|
|
node: TreeNode;
|
|
|
|
|
activeSessionId: string | null;
|
|
|
|
|
onSelectSession: (id: string) => void;
|
|
|
|
|
depth: number;
|
|
|
|
|
onMoveSession: (sessionId: string, folderId: string | null) => void;
|
|
|
|
|
onDeleteFolder: (folderId: string) => void;
|
|
|
|
|
onAddFolder: (parentId: string | null) => void;
|
2026-07-17 17:09:26 +02:00
|
|
|
onDragSession: (sessionId: string) => void;
|
|
|
|
|
onDropToFolder: (folderId: string | null) => void;
|
2026-07-17 09:21:40 +02:00
|
|
|
}) {
|
|
|
|
|
const [expanded, setExpanded] = useState(true);
|
2026-07-17 17:09:26 +02:00
|
|
|
const [isDragOver, setIsDragOver] = useState(false);
|
2026-07-17 09:21:40 +02:00
|
|
|
const isRoot = node.folder === null;
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-17 17:09:26 +02:00
|
|
|
<div
|
|
|
|
|
onDragOver={(e) => { e.preventDefault(); setIsDragOver(true); }}
|
|
|
|
|
onDragLeave={() => setIsDragOver(false)}
|
|
|
|
|
onDrop={(e) => { e.preventDefault(); setIsDragOver(false); onDropToFolder(node.folder?.id || null); }}
|
|
|
|
|
>
|
2026-07-17 09:21:40 +02:00
|
|
|
{!isRoot && (
|
|
|
|
|
<div
|
2026-07-17 17:09:26 +02:00
|
|
|
className={clsx(
|
|
|
|
|
'group flex items-center gap-1 px-2 py-1.5 text-sm font-medium rounded-md cursor-pointer',
|
|
|
|
|
isDragOver ? 'bg-primary-100 ring-1 ring-primary-300' : 'text-secondary-700 hover:bg-secondary-100'
|
|
|
|
|
)}
|
2026-07-17 09:21:40 +02:00
|
|
|
style={{ paddingLeft: depth * 12 + 8 }}
|
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
|
>
|
|
|
|
|
<svg className={clsx('w-4 h-4 transition-transform', expanded && 'rotate-90')} fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
|
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
|
|
|
|
|
</svg>
|
|
|
|
|
<svg className="w-4 h-4 text-secondary-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
|
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
|
|
|
|
|
</svg>
|
|
|
|
|
<span className="flex-1 truncate">{node.folder!.name}</span>
|
2026-07-17 17:09:26 +02:00
|
|
|
<button onClick={(e) => { e.stopPropagation(); onAddFolder(node.folder!.id); }} className="opacity-0 group-hover:opacity-100 text-secondary-400 hover:text-primary-600" title="Unterordner">+</button>
|
|
|
|
|
<button onClick={(e) => { e.stopPropagation(); onDeleteFolder(node.folder!.id); }} className="opacity-0 group-hover:opacity-100 text-secondary-400 hover:text-red-600" title="Löschen">✕</button>
|
2026-07-17 09:21:40 +02:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{(isRoot || expanded) && (
|
|
|
|
|
<div>
|
|
|
|
|
{node.sessions.map((session) => (
|
|
|
|
|
<div
|
|
|
|
|
key={session.id}
|
2026-07-17 17:09:26 +02:00
|
|
|
draggable
|
|
|
|
|
onDragStart={() => onDragSession(session.id)}
|
2026-07-17 09:21:40 +02:00
|
|
|
onClick={() => onSelectSession(session.id)}
|
|
|
|
|
className={clsx(
|
|
|
|
|
'group flex items-center gap-2 px-3 py-1.5 text-sm cursor-pointer rounded-md',
|
2026-07-17 17:09:26 +02:00
|
|
|
'hover:bg-secondary-100 active:cursor-grabbing',
|
2026-07-17 09:21:40 +02:00
|
|
|
activeSessionId === session.id && 'bg-primary-100 text-primary-700'
|
|
|
|
|
)}
|
|
|
|
|
style={{ paddingLeft: depth * 12 + 24 }}
|
|
|
|
|
>
|
|
|
|
|
<svg className="w-3.5 h-3.5 text-secondary-400 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
|
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 12h8M8 8h8m-8 4h8m-8 4h8" />
|
|
|
|
|
</svg>
|
|
|
|
|
<span className="flex-1 truncate">{session.title}</span>
|
2026-07-17 17:09:26 +02:00
|
|
|
<button onClick={(e) => { e.stopPropagation(); deleteSession(session.id).then(() => window.location.reload()); }} className="opacity-0 group-hover:opacity-100 text-secondary-400 hover:text-red-600" title="Löschen">✕</button>
|
2026-07-17 09:21:40 +02:00
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{(isRoot || expanded) && node.children.map((child) => (
|
|
|
|
|
<TreeItem
|
|
|
|
|
key={child.folder!.id}
|
|
|
|
|
node={child}
|
|
|
|
|
activeSessionId={activeSessionId}
|
|
|
|
|
onSelectSession={onSelectSession}
|
|
|
|
|
depth={depth + 1}
|
|
|
|
|
onMoveSession={onMoveSession}
|
|
|
|
|
onDeleteFolder={onDeleteFolder}
|
|
|
|
|
onAddFolder={onAddFolder}
|
2026-07-17 17:09:26 +02:00
|
|
|
onDragSession={onDragSession}
|
|
|
|
|
onDropToFolder={onDropToFolder}
|
2026-07-17 09:21:40 +02:00
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 00:52:17 +02:00
|
|
|
export function SessionList({ activeSessionId, onSelectSession, isSidebar, className }: SessionListProps) {
|
|
|
|
|
const [sessions, setSessions] = useState<ChatSession[]>([]);
|
2026-07-17 09:21:40 +02:00
|
|
|
const [folders, setFolders] = useState<ChatFolder[]>([]);
|
2026-07-17 00:52:17 +02:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-07-17 17:09:26 +02:00
|
|
|
const [dragSessionId, setDragSessionId] = useState<string | null>(null);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
const loadData = async () => {
|
2026-07-17 00:52:17 +02:00
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
2026-07-17 17:09:26 +02:00
|
|
|
const [s, f] = await Promise.all([fetchSessions(isSidebar ? true : undefined), fetchFolders()]);
|
|
|
|
|
setSessions(s); setFolders(f); setError(null);
|
|
|
|
|
} catch (e) { setError(e instanceof Error ? e.message : 'Failed to load'); }
|
|
|
|
|
finally { setLoading(false); }
|
2026-07-17 00:52:17 +02:00
|
|
|
};
|
|
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
useEffect(() => { loadData(); }, [isSidebar]);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
|
|
|
|
const handleNewChat = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const session = await createSession({ is_sidebar: isSidebar || false });
|
2026-07-17 17:09:26 +02:00
|
|
|
setSessions((prev) => [session, ...prev]); onSelectSession(session.id);
|
|
|
|
|
} catch (e) { setError(e instanceof Error ? e.message : 'Failed'); }
|
2026-07-17 00:52:17 +02:00
|
|
|
};
|
|
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
const handleAddFolder = (parentId: string | null) => {
|
2026-07-17 17:09:26 +02:00
|
|
|
const name = prompt('Ordnername:');;
|
2026-07-17 09:21:40 +02:00
|
|
|
if (!name) return;
|
2026-07-17 17:09:26 +02:00
|
|
|
createFolder({ name, parent_id: parentId || undefined }).then(() => loadData()).catch((e) => setError(e?.message));
|
2026-07-17 09:21:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDeleteFolder = (folderId: string) => {
|
|
|
|
|
if (!confirm('Ordner löschen? Chats bleiben erhalten.')) return;
|
|
|
|
|
deleteFolder(folderId).then(() => loadData()).catch((e) => setError(e?.message));
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-17 17:09:26 +02:00
|
|
|
const handleDropToFolder = async (folderId: string | null) => {
|
|
|
|
|
if (!dragSessionId) return;
|
2026-07-17 00:52:17 +02:00
|
|
|
try {
|
2026-07-17 17:09:26 +02:00
|
|
|
await updateSession(dragSessionId, { folder_id: folderId });
|
|
|
|
|
setDragSessionId(null);
|
2026-07-17 09:21:40 +02:00
|
|
|
loadData();
|
2026-07-17 17:09:26 +02:00
|
|
|
} catch (e) { setError(e instanceof Error ? e.message : 'Failed to move'); }
|
2026-07-17 00:52:17 +02:00
|
|
|
};
|
|
|
|
|
|
2026-07-17 09:21:40 +02:00
|
|
|
if (loading) return <div className="p-4 text-sm text-secondary-400">Laden...</div>;
|
|
|
|
|
|
|
|
|
|
const tree = buildTree(folders, sessions);
|
2026-07-17 00:52:17 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={clsx('flex flex-col h-full', className)}>
|
2026-07-17 09:21:40 +02:00
|
|
|
<div className="p-3 border-b border-secondary-200 flex gap-2">
|
2026-07-17 17:09:26 +02:00
|
|
|
<button onClick={handleNewChat} className="flex-1 rounded-lg bg-primary-600 text-white px-3 py-2 text-sm font-medium hover:bg-primary-700">+ Neuer Chat</button>
|
|
|
|
|
<button onClick={() => handleAddFolder(null)} className="rounded-lg border border-secondary-300 px-3 py-2 text-sm hover:bg-secondary-100" title="Neuer Ordner">📁+</button>
|
2026-07-17 00:52:17 +02:00
|
|
|
</div>
|
|
|
|
|
{error && <div className="p-2 text-xs text-red-600">{error}</div>}
|
2026-07-17 09:21:40 +02:00
|
|
|
<div className="flex-1 overflow-y-auto p-2">
|
|
|
|
|
{tree.map((node) => (
|
|
|
|
|
<TreeItem
|
|
|
|
|
key={node.folder?.id || 'root'}
|
|
|
|
|
node={node}
|
|
|
|
|
activeSessionId={activeSessionId}
|
|
|
|
|
onSelectSession={onSelectSession}
|
|
|
|
|
depth={0}
|
2026-07-17 17:09:26 +02:00
|
|
|
onMoveSession={() => {}}
|
2026-07-17 09:21:40 +02:00
|
|
|
onDeleteFolder={handleDeleteFolder}
|
|
|
|
|
onAddFolder={handleAddFolder}
|
2026-07-17 17:09:26 +02:00
|
|
|
onDragSession={setDragSessionId}
|
|
|
|
|
onDropToFolder={handleDropToFolder}
|
2026-07-17 09:21:40 +02:00
|
|
|
/>
|
|
|
|
|
))}
|
2026-07-17 00:52:17 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|