| 1 | "use client"; |
| 2 | |
| 3 | import Link from "next/link" |
| 4 | import { useAction } |
| 5 | import { useState } |
| 6 | import { toast } |
| 7 | |
| 8 | import { SettingCard } |
| 9 | import { Button } |
| 10 | import { Field, FieldLabel |
| 11 | import { Input } |
| 12 | import type { components |
| 13 | import { |
| 14 | type WebhookEvent, |
| 15 | type WebhookOwner, |
| 16 | webhookEvents, |
| 17 | webhooksPath, |
| 18 | } from "@/lib/webhooks"; |
| 19 | |
| 20 | import { createWebhook } |
| 21 | import { EventPicker } |
| 22 | import { SecretAlert } |
| 23 | |
| 24 | /** Once the endpoint exists, its secret replaces the form: the only time anyone sees it. */ |
| 25 | export function CreateWebhookCard({ |
| 26 | owner, |
| 27 | hint, |
| 28 | }: { |
| 29 | owner: WebhookOwner |
| 30 | hint: string |
| 31 | }) { |
| 32 | const [url, |
| 33 | const [events, |
| 34 | const [created, |
| 35 | useState<components[ |
| 36 | |
| 37 | const create = |
| 38 | onSuccess: ({ data |
| 39 | onError: ({ error |
| 40 | toast.error( |
| 41 | error.serverError ?? |
| 42 | error.validationErrors?.url |
| 43 | "Could not add this webhook.", |
| 44 | ), |
| 45 | }); |
| 46 | |
| 47 | if (created) { |
| 48 | return ( |
| 49 | <div className="flex flex-col items-start gap-4 |
| 50 | <SecretAlert url={ |
| 51 | <Button asChild> |
| 52 | <Link href={ |
| 53 | I have copied it |
| 54 | </Link> |
| 55 | </Button> |
| 56 | </div> |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | return ( |
| 61 | <SettingCard |
| 62 | title="Add a webhook" |
| 63 | hint={hint} |
| 64 | onSubmit={() => |
| 65 | pending={create.isExecuting |
| 66 | canSave={Boolean(url |
| 67 | submitText="Add webhook" |
| 68 | > |
| 69 | <div className="flex flex-col gap-5 |
| 70 | <Field> |
| 71 | <FieldLabel htmlFor=" |
| 72 | <Input |
| 73 | id="webhook-url" |
| 74 | type="url" |
| 75 | inputMode="url" |
| 76 | autoComplete="off" |
| 77 | placeholder="https://example.com/webhook" |
| 78 | value={url} |
| 79 | onChange={(event) |
| 80 | /> |
| 81 | </Field> |
| 82 | <EventPicker value={ |
| 83 | </div> |
| 84 | </SettingCard> |
| 85 | ); |
| 86 | } |