| 1 | "use client"; |
| 2 | |
| 3 | import { useMutation, useQuery |
| 4 | import { CircleCheck, Mail |
| 5 | import { useRouter, useSearchParams |
| 6 | import { Fragment, useEffect |
| 7 | import { toast } |
| 8 | |
| 9 | import { Badge } |
| 10 | import { Button } |
| 11 | import { Card, CardContent |
| 12 | import { Input } |
| 13 | import { |
| 14 | Item, |
| 15 | ItemActions, |
| 16 | ItemContent, |
| 17 | ItemGroup, |
| 18 | ItemMedia, |
| 19 | ItemSeparator, |
| 20 | ItemTitle, |
| 21 | } from "@/components/ui/item"; |
| 22 | import { Skeleton } |
| 23 | import { |
| 24 | Tooltip, |
| 25 | TooltipContent, |
| 26 | TooltipProvider, |
| 27 | TooltipTrigger, |
| 28 | } from "@/components/ui/tooltip"; |
| 29 | import { apiClient, apiErrorMessage |
| 30 | import { cn } |
| 31 | |
| 32 | export type EmailAddressesProps |
| 33 | className?: string |
| 34 | }; |
| 35 | |
| 36 | type UserEmail |
| 37 | id: string |
| 38 | email: string |
| 39 | verified: boolean |
| 40 | primary: boolean |
| 41 | }; |
| 42 | |
| 43 | const QUERY_KEY = [ |
| 44 | |
| 45 | /** |
| 46 | * Manage the addresses an account owns beyond the one it signs in with. |
| 47 | * |
| 48 | * Adding one mails it a link; until that link is followed the address counts for nothing, which is why unverified rows say so and offer no actions. |
| 49 | */ |
| 50 | export function EmailAddresses({ |
| 51 | const queryClient = |
| 52 | const [email, |
| 53 | |
| 54 | useVerificationResult(); |
| 55 | |
| 56 | const { data, |
| 57 | queryKey: QUERY_KEY, |
| 58 | queryFn: async () |
| 59 | const { data, |
| 60 | if (error) throw |
| 61 | return data.emails |
| 62 | }, |
| 63 | }); |
| 64 | |
| 65 | const invalidate = |
| 66 | queryClient.invalidateQueries({ |
| 67 | |
| 68 | const add = |
| 69 | mutationFn: async ( |
| 70 | const { error |
| 71 | body: { email: value |
| 72 | }); |
| 73 | if (error) throw |
| 74 | }, |
| 75 | onSuccess: async ( |
| 76 | setEmail(""); |
| 77 | toast.success(`Verification email sent to |
| 78 | await invalidate(); |
| 79 | }, |
| 80 | onError: (error: |
| 81 | }); |
| 82 | |
| 83 | const resend = |
| 84 | mutationFn: async ( |
| 85 | const { error |
| 86 | params: { path: { |
| 87 | }); |
| 88 | if (error) throw |
| 89 | }, |
| 90 | onSuccess: () => |
| 91 | onError: (error: |
| 92 | }); |
| 93 | |
| 94 | const makePrimary = |
| 95 | mutationFn: async ( |
| 96 | const { error |
| 97 | params: { path: { |
| 98 | }); |
| 99 | if (error) throw |
| 100 | }, |
| 101 | onSuccess: async () |
| 102 | toast.success("Primary address updated |
| 103 | await invalidate(); |
| 104 | }, |
| 105 | onError: (error: |
| 106 | }); |
| 107 | |
| 108 | const remove = |
| 109 | mutationFn: async ( |
| 110 | const { error |
| 111 | params: { path: { |
| 112 | }); |
| 113 | if (error) throw |
| 114 | }, |
| 115 | onSuccess: async () |
| 116 | toast.success("Address removed |
| 117 | await invalidate(); |
| 118 | }, |
| 119 | onError: (error: |
| 120 | }); |
| 121 | |
| 122 | const isBusy = |
| 123 | add.isPending || |
| 124 | resend.isPending || |
| 125 | makePrimary.isPending || |
| 126 | remove.isPending; |
| 127 | const emails = |
| 128 | |
| 129 | return ( |
| 130 | <div> |
| 131 | <h2 className="mb-3 text-sm font-semibold |
| 132 | |
| 133 | <TooltipProvider> |
| 134 | <Card className={ |
| 135 | <CardContent className=" |
| 136 | <ItemGroup className=" |
| 137 | {isPending ? ( |
| 138 | <EmailRowSkeleton /> |
| 139 | ) : ( |
| 140 | emails.map((entry |
| 141 | <Fragment key={ |
| 142 | {index > 0 |
| 143 | <Item> |
| 144 | <ItemMedia variant=" |
| 145 | <Mail /> |
| 146 | </ItemMedia> |
| 147 | <ItemContent> |
| 148 | <ItemTitle>{entry. |
| 149 | </ItemContent> |
| 150 | <ItemActions> |
| 151 | {entry.verified ? |
| 152 | <Tooltip> |
| 153 | <TooltipTrigger asChild> |
| 154 | <CircleCheck |
| 155 | aria-label="Verified" |
| 156 | className="size-4 text-emerald-500" |
| 157 | /> |
| 158 | </TooltipTrigger> |
| 159 | <TooltipContent>Verified address</TooltipContent |
| 160 | </Tooltip> |
| 161 | ) : ( |
| 162 | <Badge variant=" |
| 163 | )} |
| 164 | {entry.primary && |
| 165 | <Badge variant=" |
| 166 | )} |
| 167 | {!entry.verified |
| 168 | <Button |
| 169 | size="sm" |
| 170 | variant="outline" |
| 171 | disabled={isBusy} |
| 172 | onClick={() => |
| 173 | > |
| 174 | Resend |
| 175 | </Button> |
| 176 | )} |
| 177 | {!entry.primary |
| 178 | <Button |
| 179 | size="sm" |
| 180 | variant="outline" |
| 181 | disabled={isBusy} |
| 182 | onClick={() => |
| 183 | > |
| 184 | Make primary |
| 185 | </Button> |
| 186 | )} |
| 187 | {!entry.primary |
| 188 | <Button |
| 189 | size="sm" |
| 190 | variant="ghost" |
| 191 | disabled={isBusy} |
| 192 | onClick={() => |
| 193 | > |
| 194 | Remove |
| 195 | </Button> |
| 196 | )} |
| 197 | </ItemActions> |
| 198 | </Item> |
| 199 | </Fragment> |
| 200 | )) |
| 201 | )} |
| 202 | </ItemGroup> |
| 203 | </CardContent> |
| 204 | |
| 205 | <form |
| 206 | className="flex flex-wrap items-center justify-end gap-2 border-t p-4" |
| 207 | onSubmit={(event) |
| 208 | event.preventDefault(); |
| 209 | if (email.trim()) |
| 210 | }} |
| 211 | > |
| 212 | <Input |
| 213 | type="email" |
| 214 | name="email" |
| 215 | autoComplete="email" |
| 216 | className="h-8 flex-1" |
| 217 | placeholder="another@example.com" |
| 218 | value={email} |
| 219 | disabled={isBusy} |
| 220 | onChange={(event) |
| 221 | required |
| 222 | /> |
| 223 | <Button type=" |
| 224 | Add address |
| 225 | </Button> |
| 226 | </form> |
| 227 | </Card> |
| 228 | </TooltipProvider> |
| 229 | |
| 230 | <p className="mt-2 text-xs text-muted-foreground |
| 231 | Sign in with any verified address. Commits you push from one count |
| 232 | towards your contributions. |
| 233 | </p> |
| 234 | </div> |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | /** The verification link lands back here with its result in the URL. Report it once, then strip it so a refresh does not repeat the toast. */ |
| 239 | function useVerificationResult() { |
| 240 | const router = |
| 241 | const searchParams = |
| 242 | const result = |
| 243 | |
| 244 | useEffect(() => |
| 245 | if (!result) |
| 246 | |
| 247 | if (result === |
| 248 | if (result === |
| 249 | toast.error("That verification link is invalid or has expired |
| 250 | } |
| 251 | |
| 252 | router.replace("/settings/account |
| 253 | }, [result, |
| 254 | } |
| 255 | |
| 256 | function EmailRowSkeleton() { |
| 257 | return ( |
| 258 | <Item> |
| 259 | <ItemMedia> |
| 260 | <Skeleton className=" |
| 261 | </ItemMedia> |
| 262 | <ItemContent> |
| 263 | <Skeleton className=" |
| 264 | <Skeleton className=" |
| 265 | </ItemContent> |
| 266 | </Item> |
| 267 | ); |
| 268 | } |