gitscope
← marzneshin / v0.5.2 / dashboard/src/features/nodes/services/nodes.query.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/nodes/services/nodes.query.ts · TypeScript
Blame Raw
 1import type { NodeType } from "@marzneshin/features/nodes";
 2import { useQuery } from "@tanstack/react-query";
 3import { fetch } from "@marzneshin/utils";
 4import type {
 5    EntityQueryKeyType,
 6    UseEntityQueryProps,
 7} from "@marzneshin/features/entity-table";
 8
 9export async function fetchNodes({
10    queryKey,
11}: EntityQueryKeyType): Promise<{ entity: NodeType[]; pageCount: number }> {
12    return fetch(
13        `/nodes?page=${queryKey[1]}&size=${queryKey[2]}&name=${queryKey[3]}`,
14    ).then((result) => {
15        return {
16            entity: result.items,
17            pageCount: result.pages,
18        };
19    });
20}
21
22export const NodesQueryFetchKey = "nodes";
23
24export const useNodesQuery = ({ page, size, search }: UseEntityQueryProps) => {
25    return useQuery({
26        queryKey: [NodesQueryFetchKey, page, size, search ? search : ""],
27        queryFn: fetchNodes,
28        initialData: { entity: [], pageCount: 0 },
29    });
30};