| 1 | "use client"; |
| 2 | |
| 3 | import { ChevronRight, RotateCw |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | |
| 9 | import { FromNowHoverCard } |
| 10 | import { |
| 11 | AlertDialog, |
| 12 | AlertDialogCancel, |
| 13 | AlertDialogContent, |
| 14 | AlertDialogDescription, |
| 15 | AlertDialogFooter, |
| 16 | AlertDialogHeader, |
| 17 | AlertDialogTitle, |
| 18 | AlertDialogTrigger, |
| 19 | } from "@/components/ui/alert-dialog"; |
| 20 | import { Alert, AlertDescription |
| 21 | import { Badge } |
| 22 | import { Button } |
| 23 | import { Card } |
| 24 | import { Field, FieldLabel |
| 25 | import { Input } |
| 26 | import { Spinner } |
| 27 | import { Switch } |
| 28 | import type { components |
| 29 | import type { WebhookEvent |
| 30 | import { cn } |
| 31 | |
| 32 | import { EventPicker } |
| 33 | import { SettingCard } |
| 34 | import { |
| 35 | deleteWebhook, |
| 36 | pingWebhook, |
| 37 | redeliverWebhook, |
| 38 | updateWebhook, |
| 39 | } from "./webhook-actions"; |
| 40 | |
| 41 | type Webhook |
| 42 | type Delivery |
| 43 | |
| 44 | type WebhookProps |
| 45 | username: string |
| 46 | slug: string |
| 47 | webhook: Webhook |
| 48 | }; |
| 49 | |
| 50 | export function WebhookDetail({ |
| 51 | username, |
| 52 | slug, |
| 53 | webhook, |
| 54 | deliveries, |
| 55 | }: WebhookProps |
| 56 | return ( |
| 57 | <div className="flex flex-col gap-8 |
| 58 | {!webhook.active |
| 59 | <Alert variant=" |
| 60 | <AlertTitle>This webhook is off</AlertTitle |
| 61 | <AlertDescription> |
| 62 | {webhook.disabledReason} Turn it back on below once the receiver is |
| 63 | fixed. |
| 64 | </AlertDescription> |
| 65 | </Alert> |
| 66 | )} |
| 67 | <EditWebhookCard username={ |
| 68 | <Deliveries |
| 69 | username={username} |
| 70 | slug={slug} |
| 71 | webhook={webhook} |
| 72 | deliveries={deliveries} |
| 73 | /> |
| 74 | <DangerZone username={ |
| 75 | </div> |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | function EditWebhookCard({ username |
| 80 | const router = |
| 81 | const [url, |
| 82 | const [events, |
| 83 | const [active, |
| 84 | |
| 85 | const update = |
| 86 | onSuccess: () => |
| 87 | toast.success("Webhook saved |
| 88 | router.refresh(); |
| 89 | }, |
| 90 | onError: ({ error |
| 91 | toast.error(error |
| 92 | }); |
| 93 | const changed = |
| 94 | url.trim() !== |
| 95 | active !== webhook. |
| 96 | events.length !== webhook |
| 97 | events.some((event |
| 98 | |
| 99 | return ( |
| 100 | <SettingCard |
| 101 | title="Webhook" |
| 102 | hint="Changes apply to the next delivery, retries included." |
| 103 | onSubmit={() => |
| 104 | update.execute({ |
| 105 | username, |
| 106 | slug, |
| 107 | webhookId: webhook.id |
| 108 | url: url.trim |
| 109 | events, |
| 110 | active, |
| 111 | }) |
| 112 | } |
| 113 | pending={update.isExecuting |
| 114 | canSave={changed && |
| 115 | > |
| 116 | <div className="flex flex-col gap-5 |
| 117 | <Field> |
| 118 | <FieldLabel htmlFor=" |
| 119 | <Input |
| 120 | id="webhook-url" |
| 121 | type="url" |
| 122 | inputMode="url" |
| 123 | autoComplete="off" |
| 124 | value={url} |
| 125 | onChange={(event) |
| 126 | /> |
| 127 | </Field> |
| 128 | <Field orientation=" |
| 129 | <Switch |
| 130 | id="webhook-active" |
| 131 | checked={active} |
| 132 | onCheckedChange={setActive} |
| 133 | /> |
| 134 | <FieldLabel htmlFor=" |
| 135 | </Field> |
| 136 | <EventPicker value={ |
| 137 | </div> |
| 138 | </SettingCard> |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | function Deliveries({ |
| 143 | username, |
| 144 | slug, |
| 145 | webhook, |
| 146 | deliveries, |
| 147 | }: WebhookProps |
| 148 | const router = |
| 149 | const ping = |
| 150 | onSuccess: () => |
| 151 | toast.success("Test delivery queued |
| 152 | router.refresh(); |
| 153 | }, |
| 154 | onError: ({ error |
| 155 | toast.error(error |
| 156 | }); |
| 157 | |
| 158 | return ( |
| 159 | <section> |
| 160 | <div className="mb-3 flex items-center justify-between gap-3 |
| 161 | <h2 className="text-sm font-semibold |
| 162 | <div className="flex gap-2 |
| 163 | <Button size=" |
| 164 | <RotateCw /> |
| 165 | Refresh |
| 166 | </Button> |
| 167 | <Button |
| 168 | size="sm" |
| 169 | variant="outline" |
| 170 | disabled={ping.isExecuting |
| 171 | onClick={() => |
| 172 | ping.execute({ |
| 173 | } |
| 174 | > |
| 175 | {ping.isExecuting ? |
| 176 | Send test |
| 177 | </Button> |
| 178 | </div> |
| 179 | </div> |
| 180 | <Card className=" |
| 181 | {deliveries.length === |
| 182 | <p className="px-6 py-10 text-center text-sm text-muted-foreground |
| 183 | Nothing sent yet. |
| 184 | </p> |
| 185 | ) : ( |
| 186 | <ul className="divide-y |
| 187 | {deliveries.map(( |
| 188 | <DeliveryRow |
| 189 | key={delivery.id |
| 190 | username={username} |
| 191 | slug={slug} |
| 192 | webhook={webhook} |
| 193 | delivery={delivery} |
| 194 | /> |
| 195 | ))} |
| 196 | </ul> |
| 197 | )} |
| 198 | </Card> |
| 199 | </section> |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | const statusBadge = { |
| 204 | succeeded: { label: " |
| 205 | pending: { label: " |
| 206 | dead: { label: " |
| 207 | } as const; |
| 208 | |
| 209 | function DeliveryRow({ |
| 210 | username, |
| 211 | slug, |
| 212 | webhook, |
| 213 | delivery, |
| 214 | }: WebhookProps |
| 215 | const router = |
| 216 | const redeliver = |
| 217 | onSuccess: () => |
| 218 | toast.success("Redelivery queued |
| 219 | router.refresh(); |
| 220 | }, |
| 221 | onError: ({ error |
| 222 | toast.error(error |
| 223 | }); |
| 224 | const latest = |
| 225 | const pending = |
| 226 | const badge = |
| 227 | ? { label: "Queued |
| 228 | : statusBadge[delivery. |
| 229 | |
| 230 | return ( |
| 231 | <li> |
| 232 | <details className="group |
| 233 | <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 |
| 234 | <ChevronRight className=" |
| 235 | <Badge variant={ |
| 236 | <span className="min-w-0 flex-1 truncate font-mono text-xs |
| 237 | {delivery.event} |
| 238 | </span> |
| 239 | {latest && ( |
| 240 | <span className="hidden text-xs text-muted-foreground tabular-nums sm:inline |
| 241 | {latest.statusCode ?? |
| 242 | </span> |
| 243 | )} |
| 244 | <FromNowHoverCard |
| 245 | date={delivery.createdAt |
| 246 | className="shrink-0 text-xs text-muted-foreground" |
| 247 | /> |
| 248 | </summary> |
| 249 | <div className="flex flex-col gap-4 border-t bg-muted/30 px-4 py-4 |
| 250 | <div className="flex flex-wrap items-center justify-between gap-2 text-xs text-muted-foreground |
| 251 | <span className="font-mono |
| 252 | <Button |
| 253 | size="sm" |
| 254 | variant="outline" |
| 255 | disabled={redeliver.isExecuting |
| 256 | onClick={() => |
| 257 | redeliver.execute({ |
| 258 | username, |
| 259 | slug, |
| 260 | webhookId: webhook.id |
| 261 | deliveryId: delivery.id |
| 262 | }) |
| 263 | } |
| 264 | > |
| 265 | {redeliver.isExecuting ? |
| 266 | Redeliver |
| 267 | </Button> |
| 268 | </div> |
| 269 | {delivery.nextAttemptAt && |
| 270 | <p className="text-xs text-muted-foreground |
| 271 | Next try <FromNowHoverCard date={ |
| 272 | </p> |
| 273 | )} |
| 274 | <Block title=" |
| 275 | {delivery.attempts.map |
| 276 | <div key={attempt |
| 277 | <h3 className="text-xs font-medium |
| 278 | Attempt {delivery.attempts.length |
| 279 | <span |
| 280 | className={cn( |
| 281 | "tabular-nums", |
| 282 | !isSuccess(attempt.statusCode |
| 283 | )} |
| 284 | > |
| 285 | {attempt.statusCode ?? |
| 286 | </span>{" "} |
| 287 | <span className="font-normal text-muted-foreground |
| 288 | in {attempt.durationMs} ms, |
| 289 | <FromNowHoverCard date={ |
| 290 | </span> |
| 291 | </h3> |
| 292 | <Block title=" |
| 293 | {Object.entries(attempt |
| 294 | .map(([name |
| 295 | .join("\n") |
| 296 | </Block> |
| 297 | {attempt.responseBody && |
| 298 | <Block title=" |
| 299 | )} |
| 300 | </div> |
| 301 | ))} |
| 302 | </div> |
| 303 | </details> |
| 304 | </li> |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | function Block({ title |
| 309 | return ( |
| 310 | <div className="flex flex-col gap-1 |
| 311 | <span className="text-xs text-muted-foreground |
| 312 | <pre className="max-h-64 overflow-auto rounded-md border bg-background p-3 font-mono text-xs whitespace-pre-wrap break-all |
| 313 | {children} |
| 314 | </pre> |
| 315 | </div> |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | function DangerZone({ username |
| 320 | const router = |
| 321 | const remove = |
| 322 | onSuccess: () => |
| 323 | toast.success("Webhook deleted |
| 324 | router.push(`/ |
| 325 | }, |
| 326 | }); |
| 327 | |
| 328 | return ( |
| 329 | <section> |
| 330 | <h2 className="mb-3 text-sm font-semibold |
| 331 | <Card className=" |
| 332 | <p className="text-sm text-muted-foreground |
| 333 | Stops all deliveries, including retries still waiting. |
| 334 | </p> |
| 335 | <AlertDialog> |
| 336 | <AlertDialogTrigger asChild> |
| 337 | <Button size=" |
| 338 | <Trash2 /> |
| 339 | Delete |
| 340 | </Button> |
| 341 | </AlertDialogTrigger> |
| 342 | <AlertDialogContent> |
| 343 | <AlertDialogHeader> |
| 344 | <AlertDialogTitle>Delete this webhook?</AlertDialogTitle |
| 345 | <AlertDialogDescription> |
| 346 | {webhook.url} stops receiving deliveries, and its delivery log |
| 347 | is deleted. |
| 348 | </AlertDialogDescription> |
| 349 | </AlertDialogHeader> |
| 350 | {remove.result.serverError |
| 351 | <p className="text-sm text-destructive |
| 352 | {remove.result.serverError |
| 353 | </p> |
| 354 | )} |
| 355 | <AlertDialogFooter> |
| 356 | <AlertDialogCancel>Cancel</AlertDialogCancel |
| 357 | <Button |
| 358 | variant="destructive" |
| 359 | disabled={remove.isExecuting |
| 360 | onClick={() => |
| 361 | remove.execute({ |
| 362 | } |
| 363 | > |
| 364 | {remove.isExecuting && |
| 365 | Delete webhook |
| 366 | </Button> |
| 367 | </AlertDialogFooter> |
| 368 | </AlertDialogContent> |
| 369 | </AlertDialog> |
| 370 | </Card> |
| 371 | </section> |
| 372 | ); |
| 373 | } |
| 374 | |
| 375 | function isSuccess(status: |
| 376 | return status !== |
| 377 | } |
| 378 | |
| 379 | function formatJson(body: |
| 380 | try { |
| 381 | return JSON.stringify |
| 382 | } catch { |
| 383 | return body; |
| 384 | } |
| 385 | } |