()
| 11 | import { Session } from '@/types'; |
| 12 | |
| 13 | export const SessionList: React.FC = () => { |
| 14 | const { sessions, activeSessionId, setActiveSessionId, removeSession, addSession, setSessions } = useSessionStore(); |
| 15 | const { projects, activeProjectId } = useProjectStore(); |
| 16 | const [expandedProjects, setExpandedProjects] = useState<Set<string>>(() => { |
| 17 | return new Set(activeProjectId ? [activeProjectId] : []); |
| 18 | }); |
| 19 | const [deleteTarget, setDeleteTarget] = useState<string | null>(null); |
| 20 | |
| 21 | useEffect(() => { |
| 22 | if (!activeProjectId) return; |
| 23 | setExpandedProjects((prev) => new Set(prev).add(activeProjectId)); |
| 24 | }, [activeProjectId]); |
| 25 | |
| 26 | const toggleProject = (projectId: string) => { |
| 27 | setExpandedProjects((prev) => { |
| 28 | const next = new Set(prev); |
| 29 | if (next.has(projectId)) { |
| 30 | next.delete(projectId); |
| 31 | } else { |
| 32 | next.add(projectId); |
| 33 | } |
| 34 | return next; |
| 35 | }); |
| 36 | }; |
| 37 | |
| 38 | const handleNewSession = async (e: React.MouseEvent, projectId: string) => { |
| 39 | e.stopPropagation(); |
| 40 | const now = new Date().toISOString(); |
| 41 | const session = { id: generateId(), projectId, title: 'New Chat', createdAt: now, updatedAt: now }; |
| 42 | addSession(session); |
| 43 | setActiveSessionId(session.id); |
| 44 | setExpandedProjects((prev) => new Set(prev).add(projectId)); |
| 45 | |
| 46 | try { |
| 47 | const createdSession = await invoke<Session>('create_session', { projectId, title: 'New Chat' }); |
| 48 | const currentSessions = useSessionStore.getState().sessions; |
| 49 | setSessions(currentSessions.map((existing) => (existing.id === session.id ? createdSession : existing))); |
| 50 | setActiveSessionId(createdSession.id); |
| 51 | } catch (error) { |
| 52 | console.error('Failed to persist session creation:', error); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | const handleDeleteSession = (e: React.MouseEvent, sessionId: string) => { |
| 57 | e.stopPropagation(); |
| 58 | setDeleteTarget(sessionId); |
| 59 | }; |
| 60 | |
| 61 | const confirmDeleteSession = async () => { |
| 62 | if (!deleteTarget) return; |
| 63 | const sessionId = deleteTarget; |
| 64 | setDeleteTarget(null); |
| 65 | |
| 66 | const wasActive = activeSessionId === sessionId; |
| 67 | removeSession(sessionId); |
| 68 | if (wasActive) { |
| 69 | useChatStore.getState().setMessages([]); |
| 70 | useAgentStore.getState().setAgentRuns([]); |
nothing calls this directly
no test coverage detected