| 1 | "use client"; |
| 2 | |
| 3 | import { Check, Copy |
| 4 | import Link from "next/link |
| 5 | import { useRouter } |
| 6 | import { useAction } |
| 7 | import { Fragment, useState |
| 8 | import { toast } |
| 9 | |
| 10 | import { Alert, AlertDescription |
| 11 | import { Badge } |
| 12 | import { Button } |
| 13 | import { Card } |
| 14 | import { Checkbox } |
| 15 | import { |
| 16 | Empty, |
| 17 | EmptyDescription, |
| 18 | EmptyHeader, |
| 19 | EmptyMedia, |
| 20 | EmptyTitle, |
| 21 | } from "@/components/ui/empty"; |
| 22 | import { Field, FieldLabel |
| 23 | import { Input } |
| 24 | import { |
| 25 | Item, |
| 26 | ItemActions, |
| 27 | ItemContent, |
| 28 | ItemDescription, |
| 29 | ItemGroup, |
| 30 | ItemMedia, |
| 31 | ItemSeparator, |
| 32 | ItemTitle, |
| 33 | } from "@/components/ui/item"; |
| 34 | import type { components |
| 35 | import { |
| 36 | type WebhookEvent, |
| 37 | webhookEventLabels, |
| 38 | webhookEvents, |
| 39 | } from "@/lib/webhook-events"; |
| 40 | |
| 41 | import { SettingCard } |
| 42 | import { createWebhook } |
| 43 | |
| 44 | type Webhook |
| 45 | |
| 46 | type RepositoryProps |
| 47 | username: string |
| 48 | slug: string |
| 49 | }; |
| 50 | |
| 51 | export function RepositoryWebhooks({ |
| 52 | username, |
| 53 | slug, |
| 54 | webhooks, |
| 55 | }: RepositoryProps |
| 56 | const [created, |
| 57 | |
| 58 | return ( |
| 59 | <div className="flex flex-col gap-8 |
| 60 | {created && <SecretAlert |
| 61 | <CreateWebhookCard |
| 62 | username={username} |
| 63 | slug={slug} |
| 64 | onCreated={setCreated} |
| 65 | /> |
| 66 | |
| 67 | <section> |
| 68 | <h2 className="mb-3 text-sm font-semibold |
| 69 | Webhooks |
| 70 | <span className="ml-2 font-normal text-muted-foreground tabular-nums |
| 71 | {webhooks.length} |
| 72 | </span> |
| 73 | </h2> |
| 74 | <Card className=" |
| 75 | {webhooks.length === |
| 76 | <Empty className=" |
| 77 | <EmptyHeader> |
| 78 | <EmptyMedia variant=" |
| 79 | <Webhook /> |
| 80 | </EmptyMedia> |
| 81 | <EmptyTitle>No webhooks yet</EmptyTitle |
| 82 | <EmptyDescription> |
| 83 | Ghost POSTs a signed JSON payload to each URL you add here |
| 84 | whenever one of its events happens. |
| 85 | </EmptyDescription> |
| 86 | </EmptyHeader> |
| 87 | </Empty> |
| 88 | ) : ( |
| 89 | <ItemGroup className=" |
| 90 | {webhooks.map(( |
| 91 | <Fragment key={ |
| 92 | {index > 0 |
| 93 | <Item className=" |
| 94 | <ItemMedia variant=" |
| 95 | <Webhook /> |
| 96 | </ItemMedia> |
| 97 | <ItemContent className=" |
| 98 | <ItemTitle className=" |
| 99 | <span className="truncate font-mono text-xs |
| 100 | {webhook.url} |
| 101 | </span> |
| 102 | {!webhook.active |
| 103 | <Badge variant=" |
| 104 | )} |
| 105 | </ItemTitle> |
| 106 | <ItemDescription className=" |
| 107 | {webhook.disabledReason ?? |
| 108 | `${webhook.events. |
| 109 | </ItemDescription> |
| 110 | </ItemContent> |
| 111 | <ItemActions> |
| 112 | <Button size=" |
| 113 | <Link |
| 114 | href={`/${username |
| 115 | > |
| 116 | Edit |
| 117 | </Link> |
| 118 | </Button> |
| 119 | </ItemActions> |
| 120 | </Item> |
| 121 | </Fragment> |
| 122 | ))} |
| 123 | </ItemGroup> |
| 124 | )} |
| 125 | </Card> |
| 126 | </section> |
| 127 | </div> |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | function CreateWebhookCard({ |
| 132 | username, |
| 133 | slug, |
| 134 | onCreated, |
| 135 | }: RepositoryProps |
| 136 | onCreated: (created |
| 137 | }) { |
| 138 | const router = |
| 139 | const [url, |
| 140 | const [events, |
| 141 | |
| 142 | const create = |
| 143 | onSuccess: ({ data |
| 144 | if (data) onCreated |
| 145 | setUrl(""); |
| 146 | router.refresh(); |
| 147 | }, |
| 148 | onError: ({ error |
| 149 | toast.error( |
| 150 | error.serverError ?? |
| 151 | error.validationErrors?.url |
| 152 | "Could not add this webhook.", |
| 153 | ), |
| 154 | }); |
| 155 | |
| 156 | return ( |
| 157 | <SettingCard |
| 158 | title="Add a webhook" |
| 159 | hint="Ghost sends a ping right away, so you can check the URL works." |
| 160 | onSubmit={() => |
| 161 | create.execute({ |
| 162 | } |
| 163 | pending={create.isExecuting |
| 164 | canSave={Boolean(url |
| 165 | submitText="Add webhook" |
| 166 | > |
| 167 | <div className="flex flex-col gap-5 |
| 168 | <Field> |
| 169 | <FieldLabel htmlFor=" |
| 170 | <Input |
| 171 | id="webhook-url" |
| 172 | type="url" |
| 173 | inputMode="url" |
| 174 | autoComplete="off" |
| 175 | placeholder="https://example.com/webhook" |
| 176 | value={url} |
| 177 | onChange={(event) |
| 178 | /> |
| 179 | </Field> |
| 180 | <EventPicker value={ |
| 181 | </div> |
| 182 | </SettingCard> |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | export function EventPicker({ |
| 187 | value, |
| 188 | onChange, |
| 189 | }: { |
| 190 | value: WebhookEvent |
| 191 | onChange: (events |
| 192 | }) { |
| 193 | const toggle = |
| 194 | onChange( |
| 195 | checked |
| 196 | ? webhookEvents.filter( |
| 197 | : value.filter( |
| 198 | ); |
| 199 | |
| 200 | return ( |
| 201 | <fieldset className="flex flex-col gap-3 |
| 202 | <legend className="mb-3 text-sm font-medium |
| 203 | {webhookEvents.map(( |
| 204 | <label |
| 205 | key={event} |
| 206 | htmlFor={`event-${event |
| 207 | className="flex items-start gap-3 text-sm" |
| 208 | > |
| 209 | <Checkbox |
| 210 | id={`event-${event |
| 211 | className="mt-0.5" |
| 212 | checked={value.includes |
| 213 | onCheckedChange={(checked) |
| 214 | /> |
| 215 | <span className="flex flex-col gap-0.5 |
| 216 | <span className="font-mono text-xs |
| 217 | <span className="text-muted-foreground |
| 218 | {webhookEventLabels[event]} |
| 219 | </span> |
| 220 | </span> |
| 221 | </label> |
| 222 | ))} |
| 223 | </fieldset> |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | function SecretAlert({ url |
| 228 | const [copied, |
| 229 | |
| 230 | async function copy |
| 231 | try { |
| 232 | await navigator.clipboard |
| 233 | setCopied(true); |
| 234 | setTimeout(() => |
| 235 | } catch { |
| 236 | toast.error("Could not copy the secret. |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return ( |
| 241 | <Alert> |
| 242 | <AlertTitle>Copy the signing secret for {url |
| 243 | <AlertDescription className=" |
| 244 | <p> |
| 245 | Ghost will not show it again. Use it to check the{" "} |
| 246 | <code className="font-mono text-xs |
| 247 | header on each delivery. |
| 248 | </p> |
| 249 | <div className="flex w-full items-center gap-2 |
| 250 | <code className="min-w-0 flex-1 truncate rounded-md border bg-muted px-2 py-1.5 font-mono text-xs |
| 251 | {secret} |
| 252 | </code> |
| 253 | <Button |
| 254 | type="button" |
| 255 | size="sm" |
| 256 | variant="outline" |
| 257 | onClick={copy} |
| 258 | aria-label="Copy signing secret" |
| 259 | > |
| 260 | {copied ? <Check |
| 261 | {copied ? "Copied |
| 262 | </Button> |
| 263 | </div> |
| 264 | </AlertDescription> |
| 265 | </Alert> |
| 266 | ); |
| 267 | } |