| 1 | "use client"; |
| 2 | |
| 3 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 4 | import { KeyRound } |
| 5 | import { Fragment, useState |
| 6 | import { toast } |
| 7 | |
| 8 | import { Button } |
| 9 | import { Card, CardContent |
| 10 | import { |
| 11 | Item, |
| 12 | ItemActions, |
| 13 | ItemContent, |
| 14 | ItemDescription, |
| 15 | ItemGroup, |
| 16 | ItemMedia, |
| 17 | ItemSeparator, |
| 18 | ItemTitle, |
| 19 | } from "@/components/ui/item"; |
| 20 | import { Skeleton } |
| 21 | import { Textarea } |
| 22 | import { apiClient, apiErrorMessage |
| 23 | import { DOCS_URL } |
| 24 | import { cn } |
| 25 | |
| 26 | export type GpgKeysProps |
| 27 | className?: string |
| 28 | }; |
| 29 | |
| 30 | type GpgKey |
| 31 | id: string |
| 32 | keyId: string |
| 33 | fingerprint: string |
| 34 | emails: string |
| 35 | createdAt: string |
| 36 | }; |
| 37 | |
| 38 | const QUERY_KEY = [ |
| 39 | |
| 40 | /** |
| 41 | * Public keys that make this account's signed commits read as verified. |
| 42 | * |
| 43 | * A key is only accepted once it carries an address the account has already verified, so the form points at the email settings when it is refused. |
| 44 | */ |
| 45 | export function GpgKeys({ |
| 46 | const queryClient = |
| 47 | const [publicKey, |
| 48 | |
| 49 | const { data, |
| 50 | queryKey: QUERY_KEY, |
| 51 | queryFn: async () |
| 52 | const { data, |
| 53 | if (error) throw |
| 54 | return data.keys |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | const invalidate = |
| 59 | queryClient.invalidateQueries({ |
| 60 | |
| 61 | const add = |
| 62 | mutationFn: async ( |
| 63 | const { error |
| 64 | body: { publicKey: value |
| 65 | }); |
| 66 | if (error) throw |
| 67 | }, |
| 68 | onSuccess: async () |
| 69 | setPublicKey(""); |
| 70 | toast.success("Key added |
| 71 | await invalidate(); |
| 72 | }, |
| 73 | onError: (error: |
| 74 | }); |
| 75 | |
| 76 | const remove = |
| 77 | mutationFn: async ( |
| 78 | const { error |
| 79 | params: { path: { |
| 80 | }); |
| 81 | if (error) throw |
| 82 | }, |
| 83 | onSuccess: async () |
| 84 | toast.success("Key removed |
| 85 | await invalidate(); |
| 86 | }, |
| 87 | onError: (error: |
| 88 | }); |
| 89 | |
| 90 | const isBusy = |
| 91 | const keys = |
| 92 | |
| 93 | return ( |
| 94 | <div> |
| 95 | <h2 className="mb-3 text-sm font-semibold |
| 96 | |
| 97 | <Card className={ |
| 98 | <CardContent className=" |
| 99 | <ItemGroup className=" |
| 100 | {isPending ? ( |
| 101 | <KeyRowSkeleton /> |
| 102 | ) : keys.length |
| 103 | <Item> |
| 104 | <ItemContent> |
| 105 | <ItemDescription> |
| 106 | No keys yet. Commits you sign will read as unverified. |
| 107 | </ItemDescription> |
| 108 | </ItemContent> |
| 109 | </Item> |
| 110 | ) : ( |
| 111 | keys.map((key |
| 112 | <Fragment key={ |
| 113 | {index > 0 |
| 114 | <Item> |
| 115 | <ItemMedia variant=" |
| 116 | <KeyRound /> |
| 117 | </ItemMedia> |
| 118 | <ItemContent> |
| 119 | <ItemTitle className=" |
| 120 | <ItemDescription> |
| 121 | {key.emails.join |
| 122 | </ItemDescription> |
| 123 | </ItemContent> |
| 124 | <ItemActions> |
| 125 | <Button |
| 126 | size="sm" |
| 127 | variant="ghost" |
| 128 | disabled={isBusy} |
| 129 | onClick={() => |
| 130 | > |
| 131 | Remove |
| 132 | </Button> |
| 133 | </ItemActions> |
| 134 | </Item> |
| 135 | </Fragment> |
| 136 | )) |
| 137 | )} |
| 138 | </ItemGroup> |
| 139 | </CardContent> |
| 140 | |
| 141 | <form |
| 142 | className="flex flex-col gap-2 border-t p-4" |
| 143 | onSubmit={(event) |
| 144 | event.preventDefault(); |
| 145 | if (publicKey.trim()) |
| 146 | }} |
| 147 | > |
| 148 | <Textarea |
| 149 | name="publicKey" |
| 150 | className="h-28 font-mono text-xs" |
| 151 | placeholder="-----BEGIN PGP PUBLIC KEY BLOCK-----" |
| 152 | value={publicKey} |
| 153 | disabled={isBusy} |
| 154 | onChange={(event) |
| 155 | required |
| 156 | /> |
| 157 | <Button |
| 158 | type="submit" |
| 159 | size="sm" |
| 160 | className="self-end" |
| 161 | disabled={isBusy || |
| 162 | > |
| 163 | Add key |
| 164 | </Button> |
| 165 | </form> |
| 166 | </Card> |
| 167 | |
| 168 | <p className="mt-2 text-xs text-muted-foreground |
| 169 | Paste what <code>gpg --armor --export <key id& |
| 170 | key must carry an address you have verified on this account, and only |
| 171 | commits authored from that address read as verified.{" "} |
| 172 | <a |
| 173 | className="underline underline-offset-2" |
| 174 | href={`${DOCS_URL |
| 175 | target="_blank" |
| 176 | rel="noreferrer" |
| 177 | > |
| 178 | Read the guide |
| 179 | </a> |
| 180 | . |
| 181 | </p> |
| 182 | </div> |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | function KeyRowSkeleton() { |
| 187 | return ( |
| 188 | <Item> |
| 189 | <ItemMedia variant=" |
| 190 | <KeyRound /> |
| 191 | </ItemMedia> |
| 192 | <ItemContent className=" |
| 193 | <Skeleton className=" |
| 194 | <Skeleton className=" |
| 195 | </ItemContent> |
| 196 | </Item> |
| 197 | ); |
| 198 | } |