"use client"; import { Loader2Icon, PencilIcon, PlugZapIcon, Trash2Icon } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { toast } from "sonner"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Button, buttonVariants } from "@/components/ui/button"; import { PageHero } from "@/components/hero/page-hero"; import type { EndpointView } from "@/lib/endpoints"; import { ProbeResult } from "../endpoint-form"; import { EndpointStatus } from "../endpoint-status"; /** The page banner: name, URL, connection test, edit and delete; the page's content below it. */ export function EndpointHeader({ endpoint, runCount, children, }: { endpoint: EndpointView; runCount: number; children: React.ReactNode; }) { const router = useRouter(); const [probe, setProbe] = useState(endpoint.probe); const [testing, setTesting] = useState(false); async function test() { setTesting(true); try { const res = await fetch(`/api/endpoints/${endpoint.id}/test`, { method: "POST", }); const json = await res.json().catch(() => ({})); if (!res.ok) return toast.error( json.error?.message ?? `Test failed (${res.status})`, ); setProbe(json); router.refresh(); } finally { setTesting(false); } } async function remove() { const res = await fetch(`/api/endpoints/${endpoint.id}`, { method: "DELETE", }); if (!res.ok) { const json = await res.json().catch(() => ({})); return toast.error( json.error?.message ?? `Could not delete (${res.status})`, ); } router.push("/endpoints"); router.refresh(); } return ( {endpoint.baseUrl} {endpoint.hasApiKey ? " ยท API key set" : ""} } painting={{ view: 0.55, focus: [0.75, 0.35], anchor: [0.65, 0.35] }} actions={ <> Edit } > Delete Delete {endpoint.name}? The endpoint and its stored API key are removed. {runCount > 0 ? ` Its ${runCount} run${runCount === 1 ? "" : "s"} stay, with what they recorded.` : ""} Cancel Delete } > {probe && } {children} ); }