import { useState, useEffect, useRef } from "preact/hooks"; import type { SessionEntry } from "./types"; import { fetchSessionLog } from "./api"; export function useSessionLog(sessionFile: string | null, isOpen: boolean, shouldPoll: boolean) { const [entries, setEntries] = useState([]); const [loading, setLoading] = useState(false); const loadedRef = useRef(false); useEffect(() => { if (!sessionFile || !isOpen) return; let cancelled = false; const load = async () => { if (!loadedRef.current) setLoading(true); try { const data = await fetchSessionLog(sessionFile); if (!cancelled) { setEntries(data); setLoading(false); loadedRef.current = true; } } catch { if (!cancelled) setLoading(false); } }; load(); if (shouldPoll) { const id = setInterval(load, 3000); return () => { cancelled = true; clearInterval(id); }; } return () => { cancelled = true; }; }, [sessionFile, isOpen, shouldPoll]); return { entries, loading }; }