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