| 1 | "use client"; |
| 2 | |
| 3 | import { Check, Copy |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | |
| 9 | import { SettingCard } |
| 10 | import { Alert, AlertDescription |
| 11 | import { Button } |
| 12 | import { Field, FieldLabel |
| 13 | import { Input } |
| 14 | import { |
| 15 | type WebhookEvent, |
| 16 | type WebhookOwner, |
| 17 | webhookEvents, |
| 18 | } from "@/lib/webhooks"; |
| 19 | |
| 20 | import { createWebhook } |
| 21 | import { EventPicker } |
| 22 | |
| 23 | /** The new endpoint's secret shows above the form, the only time anyone sees it. */ |
| 24 | export function CreateWebhookCard({ |
| 25 | owner, |
| 26 | hint, |
| 27 | }: { |
| 28 | owner: WebhookOwner |
| 29 | hint: string |
| 30 | }) { |
| 31 | const router = |
| 32 | const [url, |
| 33 | const [events, |
| 34 | const [created, |
| 35 | |
| 36 | const create = |
| 37 | onSuccess: ({ data |
| 38 | if (data) setCreated |
| 39 | setUrl(""); |
| 40 | router.refresh(); |
| 41 | }, |
| 42 | onError: ({ error |
| 43 | toast.error( |
| 44 | error.serverError ?? |
| 45 | error.validationErrors?.url |
| 46 | "Could not add this webhook.", |
| 47 | ), |
| 48 | }); |
| 49 | |
| 50 | return ( |
| 51 | <div className="flex flex-col gap-8 |
| 52 | {created && <SecretAlert |
| 53 | <SettingCard |
| 54 | title="Add a webhook" |
| 55 | hint={hint} |
| 56 | onSubmit={() => |
| 57 | pending={create.isExecuting |
| 58 | canSave={Boolean(url |
| 59 | submitText="Add webhook" |
| 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 | placeholder="https://example.com/webhook" |
| 70 | value={url} |
| 71 | onChange={(event) |
| 72 | /> |
| 73 | </Field> |
| 74 | <EventPicker value={ |
| 75 | </div> |
| 76 | </SettingCard> |
| 77 | </div> |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | function SecretAlert({ url |
| 82 | const [copied, |
| 83 | |
| 84 | async function copy |
| 85 | try { |
| 86 | await navigator.clipboard |
| 87 | setCopied(true); |
| 88 | setTimeout(() => |
| 89 | } catch { |
| 90 | toast.error("Could not copy the secret. |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return ( |
| 95 | <Alert> |
| 96 | <AlertTitle>Copy the signing secret for {url |
| 97 | <AlertDescription className=" |
| 98 | <p> |
| 99 | Ghost will not show it again. Use it to check the{" "} |
| 100 | <code className="font-mono text-xs |
| 101 | header on each delivery. |
| 102 | </p> |
| 103 | <div className="flex w-full items-center gap-2 |
| 104 | <code className="min-w-0 flex-1 truncate rounded-md border bg-muted px-2 py-1.5 font-mono text-xs |
| 105 | {secret} |
| 106 | </code> |
| 107 | <Button |
| 108 | type="button" |
| 109 | size="sm" |
| 110 | variant="outline" |
| 111 | onClick={copy} |
| 112 | aria-label="Copy signing secret" |
| 113 | > |
| 114 | {copied ? <Check |
| 115 | {copied ? "Copied |
| 116 | </Button> |
| 117 | </div> |
| 118 | </AlertDescription> |
| 119 | </Alert> |
| 120 | ); |
| 121 | } |