first commit
This commit is contained in:
54
hooks/useStats.ts
Normal file
54
hooks/useStats.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
interface DailyToken {
|
||||
name: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
interface ModelDistribution {
|
||||
name: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
interface StatsData {
|
||||
dailyTokens: DailyToken[];
|
||||
modelDistribution: ModelDistribution[];
|
||||
}
|
||||
|
||||
export function useStats() {
|
||||
const [stats, setStats] = useState<StatsData | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchStats = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
const response = await fetch("/api/stats");
|
||||
if (!response.ok) {
|
||||
throw new Error("Erreur lors du chargement des statistiques");
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
setStats(data);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Erreur inconnue");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchStats();
|
||||
}, []);
|
||||
|
||||
return {
|
||||
stats,
|
||||
loading,
|
||||
error,
|
||||
refetch: fetchStats
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user