"use client"; import { useEffect } from "react"; import { useFormState } from "react-dom"; import { useRouter } from "next/navigation"; import { redeliverWebhookDeliveryAction, redeliverWebhookDeliveryTourAction, type WebhookRedeliverState, } from "../app/(app)/dashboard/actions"; type WebhookDeliverySummary = { id: string; webhook_id: string | null; event: string; url: string; invoice_id: string | null; invoice_address: string | null; invoice_subaddress_index: number | null; invoice_amount_xmr: string | null; invoice_status: | "pending" | "payment_detected" | "confirmed" | "expired" | "invalid" | null; http_status: number | null; error_message: string | null; created_at: string; }; const initialState: WebhookRedeliverState = { error: null, success: null }; export default function WebhookHistoryPanel({ mode = "live", deliveries, }: { mode?: "live" | "tour"; deliveries: WebhookDeliverySummary[]; }) { const router = useRouter(); const [state, formAction] = useFormState( mode === "tour" ? redeliverWebhookDeliveryTourAction : redeliverWebhookDeliveryAction, initialState ); useEffect(() => { if (state.success && mode !== "tour") { router.refresh(); } }, [mode, router, state.success]); const formatTimestamp = (value: string) => new Date(value).toLocaleString(); return (
{state.error ? (

{state.error}

) : null} {state.success ? (

{state.success}

) : null} {deliveries.length === 0 ? (

No webhook calls recorded yet.

) : (
{deliveries.map((delivery) => { const statusLabel = delivery.http_status !== null ? delivery.http_status.toString() : "No response"; const isFailed = delivery.http_status === null || (typeof delivery.http_status === "number" && delivery.http_status >= 400); const hasInvoiceDetails = Boolean(delivery.invoice_id) || Boolean(delivery.invoice_address) || Boolean(delivery.invoice_amount_xmr) || delivery.invoice_subaddress_index !== null; return (

{delivery.event}

{delivery.url}

{formatTimestamp(delivery.created_at)}

{statusLabel} {isFailed ? (
) : null}
{hasInvoiceDetails ? (
View invoice details
{delivery.invoice_id ? (

Invoice id

{delivery.invoice_id}

) : null} {delivery.invoice_address ? (

Subaddress

{delivery.invoice_address}

) : null} {delivery.invoice_subaddress_index !== null ? (

Subaddress index

{delivery.invoice_subaddress_index}

) : null} {delivery.invoice_amount_xmr ? (

Invoice amount (XMR)

{delivery.invoice_amount_xmr}

) : null} {delivery.invoice_status ? (

Invoice status

{delivery.invoice_status}

) : null}
) : null} {delivery.error_message ? (

Delivery error: {delivery.error_message}

) : null}
); })}
)}
); }