import { useFastMutation } from './useFastMutation';
import { useSetAtom } from 'jotai';
import { invokeCmd } from '../lib/tauri';
import { grpcConnectionsAtom } from './useGrpcConnections';

export function useDeleteGrpcConnections(requestId?: string) {
  const setGrpcConnections = useSetAtom(grpcConnectionsAtom);
  return useFastMutation({
    mutationKey: ['delete_grpc_connections', requestId],
    mutationFn: async () => {
      if (requestId === undefined) return;
      await invokeCmd('cmd_delete_all_grpc_connections', { requestId });
    },
    onSuccess: () => {
      setGrpcConnections((all) => all.filter((r) => r.requestId !== requestId));
    },
  });
}
