gitscope
← marzneshin / v0.6.3 / dashboard/src/features/total-traffic-widget/api/total-traffic.query.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/total-traffic-widget/api/total-traffic.query.ts · TypeScript
Blame Raw
 1import { useQuery } from "@tanstack/react-query";
 2import { fetch } from "@marzneshin/common/utils";
 3
 4export type UsageMetric = number[];
 5
 6export interface TotalTrafficsResponse {
 7    usages: Array<UsageMetric>;
 8    total: number;
 9}
10
11export interface TotalTrafficsQueryOptions {
12    start: string;
13    end: string
14}
15
16export type TotalTrafficsQueryKey = [string, string, string, { start?: string, end?: string }]
17
18export const TotalTrafficsDefault = {
19    total: 0,
20    usages: []
21}
22
23export async function fetchTotalTraffics({ queryKey }: { queryKey: TotalTrafficsQueryKey }): Promise<TotalTrafficsResponse> {
24    return await fetch(`/system/stats/traffic`, {
25        query: {
26            start: queryKey[3].start,
27            end: queryKey[3].end
28        }
29    }).then((result) => {
30        return result;
31    });
32}
33export const useTotalTrafficQuery = ({ start, end }: TotalTrafficsQueryOptions) => {
34    return useQuery({
35        queryKey: ["system", "stats", "traffic", { start, end }],
36        queryFn: fetchTotalTraffics,
37        refetchInterval: 1000 * 60 * 5, // 60min refresh
38        initialData: TotalTrafficsDefault
39    })
40}