| 1 | "use client"; |
| 2 | |
| 3 | import { ChevronRight, RotateCw |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { toast } |
| 7 | |
| 8 | import { FromNowHoverCard } |
| 9 | import { Badge } |
| 10 | import { Button } |
| 11 | import { Card } |
| 12 | import { Skeleton } |
| 13 | import { Spinner } |
| 14 | import type { components |
| 15 | import { cn } |
| 16 | import type { WebhookOwner |
| 17 | |
| 18 | import { pingWebhook, redeliverWebhook |
| 19 | |
| 20 | type Delivery |
| 21 | |
| 22 | type DeliveriesProps |
| 23 | owner: WebhookOwner |
| 24 | webhookId: string |
| 25 | }; |
| 26 | |
| 27 | export function WebhookDeliveries({ |
| 28 | owner, |
| 29 | webhookId, |
| 30 | deliveries, |
| 31 | }: DeliveriesProps |
| 32 | const router = |
| 33 | const ping = |
| 34 | onSuccess: () => |
| 35 | toast.success("Test delivery queued |
| 36 | router.refresh(); |
| 37 | }, |
| 38 | onError: ({ error |
| 39 | toast.error(error |
| 40 | }); |
| 41 | |
| 42 | return ( |
| 43 | <section> |
| 44 | <div className="mb-3 flex items-center justify-between gap-3 |
| 45 | <h2 className="text-sm font-semibold |
| 46 | <div className="flex gap-2 |
| 47 | <Button size=" |
| 48 | <RotateCw /> |
| 49 | Refresh |
| 50 | </Button> |
| 51 | <Button |
| 52 | size="sm" |
| 53 | variant="outline" |
| 54 | disabled={ping.isExecuting |
| 55 | onClick={() => |
| 56 | > |
| 57 | {ping.isExecuting ? |
| 58 | Send test |
| 59 | </Button> |
| 60 | </div> |
| 61 | </div> |
| 62 | <Card className=" |
| 63 | {deliveries.length === |
| 64 | <p className="px-6 py-10 text-center text-sm text-muted-foreground |
| 65 | Nothing sent yet. |
| 66 | </p> |
| 67 | ) : ( |
| 68 | <ul className="divide-y |
| 69 | {deliveries.map(( |
| 70 | <DeliveryRow |
| 71 | key={delivery.id |
| 72 | owner={owner} |
| 73 | webhookId={webhookId} |
| 74 | delivery={delivery} |
| 75 | /> |
| 76 | ))} |
| 77 | </ul> |
| 78 | )} |
| 79 | </Card> |
| 80 | </section> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | export function WebhookDeliveriesSkeleton() |
| 85 | return ( |
| 86 | <section> |
| 87 | <div className="mb-3 flex items-center justify-between |
| 88 | <Skeleton className=" |
| 89 | <Skeleton className=" |
| 90 | </div> |
| 91 | <ul className="divide-y rounded-lg border |
| 92 | {Array.from({ |
| 93 | <li key={index |
| 94 | <Skeleton className=" |
| 95 | <Skeleton className=" |
| 96 | <Skeleton className=" |
| 97 | <Skeleton className=" |
| 98 | </li> |
| 99 | ))} |
| 100 | </ul> |
| 101 | </section> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | const statusBadge = { |
| 106 | succeeded: { label: " |
| 107 | pending: { label: " |
| 108 | dead: { label: " |
| 109 | } as const; |
| 110 | |
| 111 | function DeliveryRow({ |
| 112 | owner, |
| 113 | webhookId, |
| 114 | delivery, |
| 115 | }: DeliveriesProps |
| 116 | const router = |
| 117 | const redeliver = |
| 118 | onSuccess: () => |
| 119 | toast.success("Redelivery queued |
| 120 | router.refresh(); |
| 121 | }, |
| 122 | onError: ({ error |
| 123 | toast.error(error |
| 124 | }); |
| 125 | const latest = |
| 126 | const badge = |
| 127 | delivery.status === |
| 128 | ? { label: "Queued |
| 129 | : statusBadge[delivery. |
| 130 | |
| 131 | return ( |
| 132 | <li> |
| 133 | <details className="group |
| 134 | <summary className="flex cursor-pointer list-none items-center gap-3 px-4 py-3 text-sm hover:bg-muted/50 [&::-webkit-details-marker]:hidden |
| 135 | <ChevronRight className=" |
| 136 | <Badge variant={ |
| 137 | <span className="min-w-0 flex-1 truncate font-mono text-xs |
| 138 | {delivery.event} |
| 139 | </span> |
| 140 | {latest && ( |
| 141 | <span className="hidden text-xs text-muted-foreground tabular-nums sm:inline |
| 142 | {latest.statusCode ?? |
| 143 | </span> |
| 144 | )} |
| 145 | <FromNowHoverCard |
| 146 | date={delivery.createdAt |
| 147 | className="shrink-0 text-xs text-muted-foreground" |
| 148 | /> |
| 149 | </summary> |
| 150 | <div className="flex flex-col gap-4 border-t bg-muted/30 px-4 py-4 |
| 151 | <div className="flex flex-wrap items-center justify-between gap-2 text-xs text-muted-foreground |
| 152 | <span className="font-mono |
| 153 | <Button |
| 154 | size="sm" |
| 155 | variant="outline" |
| 156 | disabled={redeliver.isExecuting |
| 157 | onClick={() => |
| 158 | redeliver.execute({ |
| 159 | owner, |
| 160 | webhookId, |
| 161 | deliveryId: delivery.id |
| 162 | }) |
| 163 | } |
| 164 | > |
| 165 | {redeliver.isExecuting ? |
| 166 | Redeliver |
| 167 | </Button> |
| 168 | </div> |
| 169 | {delivery.nextAttemptAt && |
| 170 | <p className="text-xs text-muted-foreground |
| 171 | Next try <FromNowHoverCard date={ |
| 172 | </p> |
| 173 | )} |
| 174 | <CodeBlock title=" |
| 175 | {delivery.attempts.map |
| 176 | <div key={attempt |
| 177 | <h3 className="text-xs font-medium |
| 178 | Attempt {delivery.attempts.length |
| 179 | <span |
| 180 | className={cn( |
| 181 | "tabular-nums", |
| 182 | !isSuccess(attempt.statusCode |
| 183 | )} |
| 184 | > |
| 185 | {attempt.statusCode ?? |
| 186 | </span>{" "} |
| 187 | <span className="font-normal text-muted-foreground |
| 188 | in {attempt.durationMs} ms, |
| 189 | <FromNowHoverCard date={ |
| 190 | </span> |
| 191 | </h3> |
| 192 | <CodeBlock title=" |
| 193 | {Object.entries(attempt |
| 194 | .map(([name |
| 195 | .join("\n") |
| 196 | </CodeBlock> |
| 197 | {attempt.responseBody && |
| 198 | <CodeBlock title=" |
| 199 | )} |
| 200 | </div> |
| 201 | ))} |
| 202 | </div> |
| 203 | </details> |
| 204 | </li> |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | function CodeBlock({ title |
| 209 | return ( |
| 210 | <div className="flex flex-col gap-1 |
| 211 | <span className="text-xs text-muted-foreground |
| 212 | <pre className="max-h-64 overflow-auto rounded-md border bg-background p-3 font-mono text-xs whitespace-pre-wrap break-all |
| 213 | {children} |
| 214 | </pre> |
| 215 | </div> |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | function isSuccess(status: |
| 220 | return status !== |
| 221 | } |
| 222 | |
| 223 | function formatJson(body: |
| 224 | try { |
| 225 | return JSON.stringify |
| 226 | } catch { |
| 227 | return body; |
| 228 | } |
| 229 | } |