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