DisconnectPeerDialogContent.tsx raw

   1  import { toast } from "sonner";
   2  import { useNodeDetails } from "src/hooks/useNodeDetails";
   3  import { usePeers } from "src/hooks/usePeers";
   4  import { Peer } from "src/types";
   5  import { request } from "src/utils/request";
   6  import {
   7    AlertDialogAction,
   8    AlertDialogCancel,
   9    AlertDialogContent,
  10    AlertDialogDescription,
  11    AlertDialogFooter,
  12    AlertDialogHeader,
  13    AlertDialogTitle,
  14  } from "./ui/alert-dialog";
  15  
  16  type Props = {
  17    peer: Peer;
  18  };
  19  
  20  export function DisconnectPeerDialogContent({ peer }: Props) {
  21    const { mutate: reloadPeers } = usePeers();
  22    const { data: peerDetails } = useNodeDetails(peer.nodeId);
  23  
  24    async function disconnectPeer() {
  25      try {
  26        console.info(`Disconnecting from ${peer.nodeId}`);
  27  
  28        await request(`/api/peers/${peer.nodeId}`, {
  29          method: "DELETE",
  30          headers: {
  31            "Content-Type": "application/json",
  32          },
  33        });
  34        toast("Successfully disconnected from peer", {
  35          description: peer.nodeId,
  36        });
  37        await reloadPeers();
  38      } catch (e) {
  39        console.error(e);
  40        toast.error("Failed to disconnect peer", {
  41          description: "" + e,
  42        });
  43      }
  44    }
  45  
  46    return (
  47      <AlertDialogContent>
  48        <AlertDialogHeader>
  49          <AlertDialogTitle>Disconnect Peer</AlertDialogTitle>
  50          <AlertDialogDescription>
  51            <div>
  52              <p>
  53                Are you sure you wish to disconnect from{" "}
  54                {peerDetails?.alias || "this peer"}?
  55              </p>
  56              <p className="font-medium text-foreground mt-4">Peer Pubkey</p>
  57              <p className="break-all">{peer.nodeId}</p>
  58            </div>
  59          </AlertDialogDescription>
  60        </AlertDialogHeader>
  61        <AlertDialogFooter>
  62          <AlertDialogCancel>Cancel</AlertDialogCancel>
  63          <AlertDialogAction onClick={disconnectPeer}>Confirm</AlertDialogAction>
  64        </AlertDialogFooter>
  65      </AlertDialogContent>
  66    );
  67  }
  68