| 1 | "use client"; |
| 2 | |
| 3 | import { useRouter } from |
| 4 | import { useAction } |
| 5 | import { useState } |
| 6 | import { toast } |
| 7 | |
| 8 | import { SettingCard } |
| 9 | import { Field, FieldLabel |
| 10 | import { Input } |
| 11 | import { Skeleton } |
| 12 | import { Switch } |
| 13 | import type { components |
| 14 | import type { WebhookEvent, |
| 15 | |
| 16 | import { updateWebhook } |
| 17 | import { EventPicker } |
| 18 | |
| 19 | export function WebhookSettingsCard({ |
| 20 | owner, |
| 21 | webhook, |
| 22 | }: { |
| 23 | owner: WebhookOwner |
| 24 | webhook: components |
| 25 | }) { |
| 26 | const router = |
| 27 | const [url, |
| 28 | const [events, |
| 29 | const [active, |
| 30 | |
| 31 | const update = |
| 32 | onSuccess: () => |
| 33 | toast.success("Webhook saved |
| 34 | router.refresh(); |
| 35 | }, |
| 36 | onError: ({ error |
| 37 | toast.error(error |
| 38 | }); |
| 39 | const changed = |
| 40 | url.trim() !== |
| 41 | active !== webhook. |
| 42 | events.length !== webhook |
| 43 | events.some((event |
| 44 | |
| 45 | return ( |
| 46 | <SettingCard |
| 47 | title="Webhook" |
| 48 | hint="Changes apply to the next delivery, retries included." |
| 49 | onSubmit={() => |
| 50 | update.execute({ |
| 51 | owner, |
| 52 | webhookId: webhook.id |
| 53 | url: url.trim |
| 54 | events, |
| 55 | active, |
| 56 | }) |
| 57 | } |
| 58 | pending={update.isExecuting |
| 59 | canSave={changed && |
| 60 | > |
| 61 | <div className="flex flex-col gap-5 |
| 62 | <Field> |
| 63 | <FieldLabel htmlFor=" |
| 64 | <Input |
| 65 | id="webhook-url" |
| 66 | type="url" |
| 67 | inputMode="url" |
| 68 | autoComplete="off" |
| 69 | value={url} |
| 70 | onChange={(event) |
| 71 | /> |
| 72 | </Field> |
| 73 | <Field orientation=" |
| 74 | <Switch |
| 75 | id="webhook-active" |
| 76 | checked={active} |
| 77 | onCheckedChange={setActive} |
| 78 | /> |
| 79 | <FieldLabel htmlFor=" |
| 80 | </Field> |
| 81 | <EventPicker value={ |
| 82 | </div> |
| 83 | </SettingCard> |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | const skeletonRows = [ |
| 88 | |
| 89 | /** Also stands in for the create card, which has the same shape. */ |
| 90 | export function WebhookSettingsCardSkeleton() |
| 91 | return ( |
| 92 | <section> |
| 93 | <Skeleton className=" |
| 94 | <div className="flex flex-col gap-5 rounded-lg border p-6 |
| 95 | <Skeleton className=" |
| 96 | {skeletonRows.map(( |
| 97 | <Skeleton key={ |
| 98 | ))} |
| 99 | </div> |
| 100 | </section> |
| 101 | ); |
| 102 | } |