34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
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<T>(result: ApiResult<T>): result is { error: { code: string; message: string } } {
|
|
return "error" in result;
|
|
}
|
|
|
|
export default function useGroupAudit(activeGroupId?: number | null) {
|
|
const [events, setEvents] = useState<GroupAuditEvent[]>([]);
|
|
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 };
|
|
}
|