import React from "react"; import { toast } from "sonner"; import { AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "src/components/ui/alert-dialog"; import { Textarea } from "src/components/ui/textarea"; import { useInfo } from "src/hooks/useInfo"; import { request } from "src/utils/request"; type ExecuteCustomNodeCommandDialogContentProps = { availableCommands: string; setCommandResponse: (response: string) => void; }; export function ExecuteCustomNodeCommandDialogContent({ setCommandResponse, availableCommands, }: ExecuteCustomNodeCommandDialogContentProps) { const { mutate: reloadInfo } = useInfo(); const [command, setCommand] = React.useState(); let parsedAvailableCommands = availableCommands; try { parsedAvailableCommands = JSON.stringify( JSON.parse(availableCommands).commands, null, 2 ); } catch { // ignore unexpected json } async function executeCommand() { try { if (!command) { throw new Error("No command set"); } const result = await request("/api/command", { method: "POST", body: JSON.stringify({ command }), headers: { "Content-Type": "application/json", }, }); await reloadInfo(); const parsedResponse = JSON.stringify(result); setCommandResponse(parsedResponse); toast("Command executed", { description: parsedResponse }); } catch (error) { console.error(error); toast.error("Something went wrong: " + error); } } const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); executeCommand(); }; return ( Execute Custom Node Command