ResetRoutingDataDialogContent.tsx raw

   1  import React from "react";
   2  import { toast } from "sonner";
   3  import {
   4    AlertDialogAction,
   5    AlertDialogCancel,
   6    AlertDialogContent,
   7    AlertDialogDescription,
   8    AlertDialogFooter,
   9    AlertDialogHeader,
  10    AlertDialogTitle,
  11  } from "src/components/ui/alert-dialog";
  12  import { Label } from "src/components/ui/label";
  13  import {
  14    Select,
  15    SelectContent,
  16    SelectItem,
  17    SelectTrigger,
  18    SelectValue,
  19  } from "src/components/ui/select";
  20  import { useInfo } from "src/hooks/useInfo";
  21  import { request } from "src/utils/request";
  22  
  23  const RESET_KEY_OPTIONS = [
  24    {
  25      value: "ALL",
  26      label: "All",
  27      description: "Clears both the scorer, network graph data, and node metrics",
  28    },
  29    {
  30      value: "Scorer",
  31      label: "Scorer",
  32      description:
  33        "Clears the scores/penalties applied to nodes from past payment attempts.",
  34    },
  35    {
  36      value: "NetworkGraph",
  37      label: "Network Graph",
  38      description: "Clears the cache of nodes on the network",
  39    },
  40    {
  41      value: "NodeMetrics",
  42      label: "Node Metrics",
  43      description:
  44        "Clears last sync timestamps to do a full wallet or network graph re-scan when RGS is enabled",
  45    },
  46  ];
  47  
  48  export function ResetRoutingDataDialogContent() {
  49    const { mutate: reloadInfo } = useInfo();
  50    const [resetKey, setResetKey] = React.useState<string>();
  51  
  52    async function resetRouter() {
  53      try {
  54        await request("/api/reset-router", {
  55          method: "POST",
  56          body: JSON.stringify({ key: resetKey }),
  57          headers: {
  58            "Content-Type": "application/json",
  59          },
  60        });
  61        await reloadInfo();
  62        toast("🎉 Router reset");
  63      } catch (error) {
  64        console.error(error);
  65        toast.error("Something went wrong", {
  66          description: "" + error,
  67        });
  68      }
  69    }
  70  
  71    const handleSubmit = async (e: React.FormEvent) => {
  72      e.preventDefault();
  73      await resetRouter();
  74    };
  75  
  76    return (
  77      <AlertDialogContent>
  78        <AlertDialogHeader>
  79          <AlertDialogTitle>Clear Routing Data</AlertDialogTitle>
  80          <AlertDialogDescription className="text-left">
  81            <div>
  82              <p>Are you sure you want to clear your routing data?</p>
  83              <form id="reset-routing-form" onSubmit={handleSubmit}>
  84                <div className="grid gap-2 mt-4">
  85                  <Label className="text-foreground">Routing Data to Clear</Label>
  86                  <Select
  87                    name="resetKey"
  88                    value={resetKey}
  89                    onValueChange={(value) => setResetKey(value)}
  90                  >
  91                    <SelectTrigger>
  92                      <SelectValue placeholder="Select Data" />
  93                    </SelectTrigger>
  94                    <SelectContent>
  95                      {RESET_KEY_OPTIONS.map((resetKey) => (
  96                        <SelectItem key={resetKey.value} value={resetKey.value}>
  97                          {resetKey.label}
  98                        </SelectItem>
  99                      ))}
 100                    </SelectContent>
 101                  </Select>
 102                </div>
 103              </form>
 104              <div className="grid gap-2 mt-4 border rounded-md p-3">
 105                <h3 className="font-semibold text-foreground">
 106                  Clear Data Options
 107                </h3>
 108                {RESET_KEY_OPTIONS.map((resetKey) => (
 109                  <p key={resetKey.value}>
 110                    <span className="font-medium text-foreground">
 111                      {resetKey.label}
 112                    </span>
 113                    {" - "}
 114                    {resetKey.description}
 115                  </p>
 116                ))}
 117              </div>
 118              <p className="font-medium text-foreground mt-4">
 119                After clearing, you'll need to login again to restart your node.
 120              </p>
 121            </div>
 122          </AlertDialogDescription>
 123        </AlertDialogHeader>
 124        <AlertDialogFooter>
 125          <AlertDialogCancel onClick={() => setResetKey(undefined)}>
 126            Cancel
 127          </AlertDialogCancel>
 128          <AlertDialogAction
 129            disabled={!resetKey}
 130            type="submit"
 131            form="reset-routing-form"
 132          >
 133            Confirm
 134          </AlertDialogAction>
 135        </AlertDialogFooter>
 136      </AlertDialogContent>
 137    );
 138  }
 139