| 1 | "use client"; |
| 2 | |
| 3 | import { Trash2 } from "lucide-react"; |
| 4 | import { useRouter } from "next/navigation"; |
| 5 | import { useAction } from "next-safe-action/hooks"; |
| 6 | import { toast } from "sonner"; |
| 7 | |
| 8 | import { |
| 9 | AlertDialog, |
| 10 | AlertDialogCancel, |
| 11 | AlertDialogContent, |
| 12 | AlertDialogDescription, |
| 13 | AlertDialogFooter, |
| 14 | AlertDialogHeader, |
| 15 | AlertDialogTitle, |
| 16 | AlertDialogTrigger, |
| 17 | } from "@/components/ui/alert-dialog"; |
| 18 | import { Button } from "@/components/ui/button"; |
| 19 | import { Card } from "@/components/ui/card"; |
| 20 | import { FieldError } from "@/components/ui/field"; |
| 21 | import { Spinner } from "@/components/ui/spinner"; |
| 22 | import { type WebhookOwner, webhooksPath } from "@/lib/webhooks"; |
| 23 | |
| 24 | import { deleteWebhook } from "./actions"; |
| 25 | |
| 26 | export function DeleteWebhookCard({ |
| 27 | owner, |
| 28 | webhook, |
| 29 | }: { |
| 30 | owner: WebhookOwner; |
| 31 | webhook: { id: string; url: string }; |
| 32 | }) { |
| 33 | const router = useRouter(); |
| 34 | const remove = useAction(deleteWebhook, { |
| 35 | onSuccess: () => { |
| 36 | toast.success("Webhook deleted"); |
| 37 | router.push(webhooksPath(owner)); |
| 38 | }, |
| 39 | }); |
| 40 | |
| 41 | return ( |
| 42 | <section> |
| 43 | <h2 className="mb-3 text-sm font-semibold">Delete</h2> |
| 44 | <Card className="flex-row items-center justify-between gap-4 px-6"> |
| 45 | <p className="text-sm text-muted-foreground"> |
| 46 | Stops all deliveries, including retries still waiting. |
| 47 | </p> |
| 48 | <AlertDialog> |
| 49 | <AlertDialogTrigger asChild> |
| 50 | <Button size="sm" variant="destructive"> |
| 51 | <Trash2 /> |
| 52 | Delete |
| 53 | </Button> |
| 54 | </AlertDialogTrigger> |
| 55 | <AlertDialogContent> |
| 56 | <AlertDialogHeader> |
| 57 | <AlertDialogTitle>Delete this webhook?</AlertDialogTitle> |
| 58 | <AlertDialogDescription> |
| 59 | {webhook.url} stops receiving deliveries, and its delivery log |
| 60 | is deleted. |
| 61 | </AlertDialogDescription> |
| 62 | </AlertDialogHeader> |
| 63 | {remove.result.serverError && ( |
| 64 | <FieldError>{remove.result.serverError}</FieldError> |
| 65 | )} |
| 66 | <AlertDialogFooter> |
| 67 | <AlertDialogCancel>Cancel</AlertDialogCancel> |
| 68 | <Button |
| 69 | variant="destructive" |
| 70 | disabled={remove.isExecuting} |
| 71 | onClick={() => remove.execute({ owner, webhookId: webhook.id })} |
| 72 | > |
| 73 | {remove.isExecuting && <Spinner />} |
| 74 | Delete webhook |
| 75 | </Button> |
| 76 | </AlertDialogFooter> |
| 77 | </AlertDialogContent> |
| 78 | </AlertDialog> |
| 79 | </Card> |
| 80 | </section> |
| 81 | ); |
| 82 | } |