From d22acbebb28b5710284bc0a6e053f7078f08d16c Mon Sep 17 00:00:00 2001 From: im-adithya Date: Wed, 14 Aug 2024 16:10:06 +0530 Subject: [PATCH] chore: use alert dialog in debug tools --- frontend/src/screens/settings/DebugTools.tsx | 376 ++++++++++++++----- 1 file changed, 291 insertions(+), 85 deletions(-) diff --git a/frontend/src/screens/settings/DebugTools.tsx b/frontend/src/screens/settings/DebugTools.tsx index 8458b882..a62e58fe 100644 --- a/frontend/src/screens/settings/DebugTools.tsx +++ b/frontend/src/screens/settings/DebugTools.tsx @@ -1,16 +1,228 @@ -import { useState } from "react"; +import React from "react"; import { ResetRoutingDataDialogContent } from "src/components/ResetRoutingDataDialogContent"; import { AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, AlertDialogTrigger, } from "src/components/ui/alert-dialog"; import { Button } from "src/components/ui/button"; +import { Input } from "src/components/ui/input"; +import { Label } from "src/components/ui/label"; import { Textarea } from "src/components/ui/textarea"; +import { useInfo } from "src/hooks/useInfo"; import { request } from "src/utils/request"; +type Props = { + apiRequest: ( + endpoint: string, + method: string, + requestBody?: object + ) => Promise; + target?: string; +}; + +function ProbeInvoiceDialogContent({ apiRequest }: Props) { + const [invoice, setInvoice] = React.useState(); + + async function onConfirm() { + await apiRequest("/api/send-payment-probes", "POST", { + invoice: invoice, + }); + setInvoice(""); + } + + return ( + + + Probe Invoice + + + { + setInvoice(e.target.value.trim()); + }} + /> + + + + Cancel + + Confirm + + + + ); +} + +function ProbeKeysendDialogContent({ apiRequest }: Props) { + const [amount, setAmount] = React.useState(""); + const [nodeId, setNodeId] = React.useState(""); + + async function onConfirm() { + await apiRequest("/api/send-spontaneous-payment-probes", "POST", { + amount: (parseInt(amount) || 0) * 1000, + nodeId, + }); + setAmount(""); + setNodeId(""); + } + + return ( + + + Probe Keysend + +
+ + { + setAmount(e.target.value.trim()); + }} + /> +
+
+ + { + setNodeId(e.target.value.trim()); + }} + /> +
+
+
+ + Cancel + + Confirm + + +
+ ); +} + +function GetLogsDialogContent({ apiRequest, target }: Props) { + const [maxLen, setMaxLen] = React.useState(""); + + async function onConfirm() { + await apiRequest(`/api/log/${target}?maxLen=${maxLen}`, "GET"); + setMaxLen(""); + } + + return ( + + + + Get {target} Logs + + + + { + setMaxLen(e.target.value.trim()); + }} + /> + + + + Cancel + + Confirm + + + + ); +} + +function GetNetworkGraphDialogContent({ apiRequest }: Props) { + const [nodeIds, setNodeIds] = React.useState(""); + + async function onConfirm() { + await apiRequest(`/api/node/network-graph?nodeIds=${nodeIds}`, "GET"); + setNodeIds(""); + } + + return ( + + + Get Network Graph + + + { + setNodeIds(e.target.value.trim()); + }} + /> + + + + Cancel + + Confirm + + + + ); +} + export default function DebugTools() { - const [apiResponse, setApiResponse] = useState(""); + const [apiResponse, setApiResponse] = React.useState(""); + const [dialog, setDialog] = React.useState< + | "probeInvoice" + | "probeKeysend" + | "getAppLogs" + | "getNodeLogs" + | "getNetworkGraph" + | "resetRoutingData" + >(); + + const { data: info } = useInfo(); async function apiRequest( endpoint: string, @@ -41,98 +253,92 @@ export default function DebugTools() { return (
-
- - - - - - - - - - + {info?.backendType === "LDK" && ( + + + + )} + {info?.backendType === "LDK" && ( + + + + )} + + + + + - + - + + + + + + {info?.backendType === "LDK" && ( + + + + )} + + {dialog === "probeInvoice" && ( + + )} + {dialog === "probeKeysend" && ( + + )} + {(dialog === "getAppLogs" || dialog === "getNodeLogs") && ( + + )} + {dialog === "getNetworkGraph" && ( + + )} + {dialog === "resetRoutingData" && }
{apiResponse && (