import {useCallback, useEffect, useState} from 'react'; import {apiFetch} from '../utils/api.js'; import {statsUrl} from '../utils/stats.js'; /** * GET /api/library/stats for the given groups (none = all). stats is null while loading and after an error. */ export default function useUserStats(groups, enabled = true) { const [stats, setStats] = useState(null); const [failed, setFailed] = useState(false); const key = groups.join(','); const load = useCallback(async () => { setFailed(false); try { const response = await apiFetch(statsUrl(key)); if (response.status === 401) return; if (!response.ok) { setFailed(true); return; } setStats(await response.json()); } catch (err) { console.error('Stats load error:', err); setFailed(true); } }, [key]); useEffect(() => { if (enabled) load(); }, [enabled, load]); return {stats, failed, reload: load}; }