import { useCallback, useEffect, useState } from "react"; import type { ApiResult } from "@/lib/client/fetch-json"; import { groupAuditList, type GroupAuditEvent } from "@/lib/client/group-audit"; function isError(result: ApiResult): result is { error: { code: string; message: string } } { return "error" in result; } export default function useGroupAudit(activeGroupId?: number | null) { const [events, setEvents] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const load = useCallback(async () => { if (!activeGroupId) { setEvents([]); setError(""); return; } setLoading(true); setError(""); const result = await groupAuditList(); if (isError(result)) setError(result.error.message || ""); else setEvents(result.data.events || []); setLoading(false); }, [activeGroupId]); useEffect(() => { load(); }, [load]); return { events, loading, error, reload: load }; }