Files
16gagent/.benchmark/appointment/frontend/hooks/useStats.ts
T
2026-06-06 10:40:48 +08:00

36 lines
876 B
TypeScript

"use client";
import { useState, useEffect, useCallback } from "react";
// Generic data type for stats
type Stat = Record<string, unknown>;
/**
* Fetch and manage stats data.
*/
export function useStats() {
const [data, setData] = useState<Stat[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchData = useCallback(async () => {
try {
setLoading(true);
setError(null);
const res = await fetch(`/api/stats`);
const json = await res.json();
setData(Array.isArray(json) ? json : json?.data || []);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to load data");
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
fetchData();
}, [fetchData]);
return { data, loading, error, refetch: fetchData };
}