import { useMutation, useQueryClient } from '@tanstack/react-query'; import { HttpUtil } from '@/utils'; import { keys } from '@/api/queryKeys'; import type { NodeRecord } from '@/api/queries/useNodesQuery'; interface ApiMsg { success?: boolean; msg?: string; obj?: T; } export interface ProbeResult { status: string; latencyMs?: number; xrayVersion?: string; error?: string; } export function useNodeMutations() { const queryClient = useQueryClient(); const invalidate = () => queryClient.invalidateQueries({ queryKey: keys.nodes.root() }); const createMut = useMutation({ mutationFn: (payload: Partial) => HttpUtil.post('/panel/api/nodes/add', payload) as Promise, onSuccess: (msg) => { if (msg?.success) invalidate(); }, }); const updateMut = useMutation({ mutationFn: ({ id, payload }: { id: number; payload: Partial }) => HttpUtil.post(`/panel/api/nodes/update/${id}`, payload) as Promise, onSuccess: (msg) => { if (msg?.success) invalidate(); }, }); const removeMut = useMutation({ mutationFn: (id: number) => HttpUtil.post(`/panel/api/nodes/del/${id}`) as Promise, onSuccess: (msg) => { if (msg?.success) invalidate(); }, }); const setEnableMut = useMutation({ mutationFn: ({ id, enable }: { id: number; enable: boolean }) => HttpUtil.post(`/panel/api/nodes/setEnable/${id}`, { enable }) as Promise, onSuccess: (msg) => { if (msg?.success) invalidate(); }, }); const probeMut = useMutation({ mutationFn: (id: number) => HttpUtil.post(`/panel/api/nodes/probe/${id}`) as Promise>, onSuccess: (msg) => { if (msg?.success) invalidate(); }, }); return { create: (payload: Partial) => createMut.mutateAsync(payload), update: (id: number, payload: Partial) => updateMut.mutateAsync({ id, payload }), remove: (id: number) => removeMut.mutateAsync(id), setEnable: (id: number, enable: boolean) => setEnableMut.mutateAsync({ id, enable }), probe: (id: number) => probeMut.mutateAsync(id), testConnection: (payload: Partial) => HttpUtil.post('/panel/api/nodes/test', payload) as Promise>, }; }