gitscope
← marzneshin / v0.7.2 / dashboard/src/modules/nodes/api/backend-stats.query.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/modules/nodes/api/backend-stats.query.ts · TypeScript
Blame Raw
 1import type { NodeType } from "@marzneshin/modules/nodes";
 2import { useQuery } from "@tanstack/react-query";
 3import { fetch } from "@marzneshin/common/utils";
 4
 5type NodeBackendStatsQueryKey = [string, number, string, string];
 6
 7interface NodeBackendStatsQuery {
 8    queryKey: NodeBackendStatsQueryKey;
 9}
10
11export async function fetchNodeBackendStats({
12    queryKey,
13}: NodeBackendStatsQuery): Promise<{
14    running: boolean;
15} | null> {
16    return fetch(`/nodes/${Number(queryKey[1])}/${queryKey[2]}/stats`)
17        .then((stats) => stats)
18        .catch(() => null);
19}
20
21export const useBackendStatsQuery = (node: NodeType, backend: string) => {
22    const queryKey: NodeBackendStatsQueryKey = [
23        "nodes",
24        node.id,
25        backend,
26        "stats",
27    ];
28
29    return useQuery({
30        queryKey,
31        queryFn: fetchNodeBackendStats,
32        refetchInterval: 1000 * 5,
33        initialData: null,
34    });
35};