"use client"; import { Webhook, WebhookCallStatus } from "@prisma/client"; import { formatDistanceToNow } from "date-fns"; import { Copy, Eye, EyeOff } from "lucide-react"; import { useState } from "react"; import { Button } from "@usesend/ui/src/button"; import { toast } from "@usesend/ui/src/toaster"; import { api } from "~/trpc/react"; import { Badge } from "@usesend/ui/src/badge"; import { WebhookStatusBadge } from "../webhook-status-badge"; export function WebhookInfo({ webhook }: { webhook: Webhook }) { const [showSecret, setShowSecret] = useState(false); const sevenDaysAgo = new Date(); sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); const callsQuery = api.webhook.listCalls.useQuery({ webhookId: webhook.id, limit: 50, }); const calls = callsQuery.data?.items ?? []; const last7DaysCalls = calls.filter( (call) => new Date(call.createdAt) >= sevenDaysAgo, ); const deliveredCount = last7DaysCalls.filter( (c) => c.status === WebhookCallStatus.DELIVERED, ).length; const failedCount = last7DaysCalls.filter( (c) => c.status === WebhookCallStatus.FAILED, ).length; const pendingCount = last7DaysCalls.filter( (c) => c.status === WebhookCallStatus.PENDING || c.status === WebhookCallStatus.IN_PROGRESS, ).length; const handleCopySecret = () => { navigator.clipboard.writeText(webhook.secret); toast.success("Secret copied to clipboard"); }; return (
Events
{webhook.eventTypes.length === 0 ? ( All events ) : ( <> {webhook.eventTypes.slice(0, 2).map((event) => ( {event} ))} {webhook.eventTypes.length > 2 && ( +{webhook.eventTypes.length - 2} more )} )}
Status
Created {formatDistanceToNow(webhook.createdAt, { addSuffix: true })}
Signing Secret
{showSecret ? webhook.secret : "whsec_••••••••••••••••••••••••"}
); }