import { useState, useEffect, useCallback } from "react"; import type { SessionEntry, SessionDetailResponse } from "../lib/types"; interface SessionState { sessions: SessionEntry[]; sessionsLoading: boolean; sessionsError: string | null; currentSession: SessionDetailResponse | null; sessionLoading: boolean; sessionError: string | null; loadSessions: () => Promise; loadSession: (id: string) => Promise; } export function useSession(): SessionState { const [sessions, setSessions] = useState([]); const [sessionsLoading, setSessionsLoading] = useState(true); const [sessionsError, setSessionsError] = useState(null); const [currentSession, setCurrentSession] = useState(null); const [sessionLoading, setSessionLoading] = useState(false); const [sessionError, setSessionError] = useState(null); const loadSessions = useCallback(async () => { setSessionsLoading(true); setSessionsError(null); try { const res = await fetch("/api/sessions"); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); setSessions(data.sessions); } catch (err) { setSessionsError( err instanceof Error ? err.message : "Failed to load sessions" ); } finally { setSessionsLoading(false); } }, []); const loadSession = useCallback(async (id: string) => { setSessionLoading(true); setSessionError(null); try { const res = await fetch(`/api/sessions/${encodeURIComponent(id)}`); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); setCurrentSession(data); } catch (err) { setSessionError( err instanceof Error ? err.message : "Failed to load session" ); } finally { setSessionLoading(false); } }, []); useEffect(() => { loadSessions(); }, [loadSessions]); return { sessions, sessionsLoading, sessionsError, currentSession, sessionLoading, sessionError, loadSessions, loadSession, }; }