import { useCallback, useEffect, useRef, useState } from "react" import { type SessionSummary, deleteSession, getSessions } from "@/api/sessions" const LIMIT = 20 interface UseSessionHistoryOptions { activeSessionId: string onDeletedActiveSession: () => void } export function useSessionHistory({ activeSessionId, onDeletedActiveSession, }: UseSessionHistoryOptions) { const observerRef = useRef(null) const [sessions, setSessions] = useState([]) const [offset, setOffset] = useState(0) const [hasMore, setHasMore] = useState(true) const [isLoadingMore, setIsLoadingMore] = useState(false) const loadSessions = useCallback( async (reset = true) => { try { const currentOffset = reset ? 0 : offset if (reset) { setHasMore(true) setOffset(0) } const data = await getSessions(currentOffset, LIMIT) if (data.length < LIMIT) { setHasMore(false) } if (reset) { setSessions(data) } else { setSessions((prev) => { const existingIds = new Set(prev.map((s) => s.id)) const newItems = data.filter((s) => !existingIds.has(s.id)) return [...prev, ...newItems] }) } setOffset(currentOffset + data.length) } catch { // silently fail } finally { setIsLoadingMore(false) } }, [offset], ) useEffect(() => { if (!observerRef.current || !hasMore || isLoadingMore) return const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting && hasMore && !isLoadingMore) { setIsLoadingMore(true) void loadSessions(false) } }, { threshold: 0.1 }, ) observer.observe(observerRef.current) return () => observer.disconnect() }, [hasMore, isLoadingMore, loadSessions]) const handleDeleteSession = useCallback( async (id: string) => { try { await deleteSession(id) setSessions((prev) => prev.filter((s) => s.id !== id)) if (id === activeSessionId) { onDeletedActiveSession() } } catch (err) { console.error("Failed to delete session:", err) } }, [activeSessionId, onDeletedActiveSession], ) return { sessions, hasMore, observerRef, loadSessions, handleDeleteSession, } }