60 lines
1.2 KiB
TypeScript
60 lines
1.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
interface DailyToken {
|
|
name: string;
|
|
value: number;
|
|
}
|
|
|
|
interface DailyConnection {
|
|
name: string;
|
|
value: number;
|
|
}
|
|
|
|
interface ModelDistribution {
|
|
name: string;
|
|
value: number;
|
|
}
|
|
|
|
interface StatsData {
|
|
dailyTokens: DailyToken[];
|
|
dailyConnections: DailyConnection[];
|
|
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
|
|
};
|
|
} |