mirror of
https://github.com/fankes/komari-theme-purcarte.git
synced 2025-10-20 12:29:22 +08:00
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import { useState, useEffect } from "react";
|
|
import { useNodeData } from "@/contexts/NodeDataContext";
|
|
import type { PingHistoryResponse, NodeData } from "@/types/node";
|
|
|
|
export const usePingChart = (node: NodeData | null, hours: number) => {
|
|
const { getPingHistory } = useNodeData();
|
|
const [pingHistory, setPingHistory] = useState<PingHistoryResponse | null>(
|
|
null
|
|
);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!node?.uuid) {
|
|
setPingHistory(null);
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
const fetchHistory = async () => {
|
|
try {
|
|
const data = await getPingHistory(node.uuid, hours);
|
|
setPingHistory(data);
|
|
} catch (err: any) {
|
|
setError(err.message || "Failed to fetch history data");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchHistory();
|
|
}, [node?.uuid, hours, getPingHistory]);
|
|
|
|
return {
|
|
loading,
|
|
error,
|
|
pingHistory,
|
|
};
|
|
};
|