gitscope
← marzneshin / v0.3.0-alpha4 / 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 { NodeType } from "@marzneshin/features/nodes";
 2import { useQuery } from "@tanstack/react-query";
 3import { fetch } from "@marzneshin/utils";
 4import {
 5    EntityQueryKeyType,
 6    UseEntityQueryProps
 7} from "@marzneshin/components";
 8
 9export async function fetchNodes({ queryKey }: EntityQueryKeyType): Promise<{ entity: NodeType[], pageCount: number }> {
10    return fetch(`/nodes?page=${queryKey[1]}&size=${queryKey[2]}&name=${queryKey[3]}`).then((result) => {
11        return {
12            entity: result.items,
13            pageCount: result.pages,
14        };
15    });
16}
17
18export const NodesQueryFetchKey = "nodes";
19
20
21export const useNodesQuery = ({ page, size, search }: UseEntityQueryProps) => {
22    return useQuery({
23        queryKey: [NodesQueryFetchKey, page, size, search ? search : ""],
24        queryFn: fetchNodes,
25        initialData: { entity: [], pageCount: 0 },
26    })
27}