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