gitscope
← marzneshin / v0.4.0 / dashboard/src/features/nodes/services/settings.query.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/nodes/services/settings.query.ts · TypeScript
Blame Raw
 1import { NodeType } from "@marzneshin/features/nodes";
 2import { useQuery } from "@tanstack/react-query";
 3import { fetch } from "@marzneshin/utils";
 4
 5type NodesSettingsQueryKey = [string, number | null, string]
 6
 7export async function fetchNodesSettings({ queryKey }: { queryKey: NodesSettingsQueryKey }): Promise<NodeType[]> {
 8    return fetch(`/nodes/${Number(queryKey[1])}/xray_config`).then((config) => {
 9        return config;
10    });
11}
12
13export const NodesSettingsQueryFetchKey = "nodes";
14
15export const useNodesSettingsQuery = (node: NodeType | null) => {
16    const nodeId: number | null = node?.id ?? null
17    const queryKey: NodesSettingsQueryKey =
18        [NodesSettingsQueryFetchKey, nodeId, "xray_config"];
19
20    const queryFn = nodeId !== null
21        ? ({ queryKey }: { queryKey: [string, number | null, string] }) => fetchNodesSettings({ queryKey })
22        : () => Promise.resolve<NodeType[]>([]);
23
24    return useQuery({
25        queryKey,
26        queryFn,
27        initialData: [],
28    });
29};