| 1 | "use client"; |
| 2 | |
| 3 | import type { |
| 4 | ApiKeyAuthClient, |
| 5 | ListedApiKey, |
| 6 | } from "@better-auth-ui/core/plugins/api-key"; |
| 7 | import { useAuth, useAuthPlugin |
| 8 | import { useDeleteApiKey } |
| 9 | import { Key } |
| 10 | |
| 11 | import { |
| 12 | AlertDialog, |
| 13 | AlertDialogCancel, |
| 14 | AlertDialogContent, |
| 15 | AlertDialogDescription, |
| 16 | AlertDialogFooter, |
| 17 | AlertDialogHeader, |
| 18 | AlertDialogMedia, |
| 19 | AlertDialogTitle, |
| 20 | } from "@/components/ui/alert-dialog"; |
| 21 | import { Button } |
| 22 | import { Field, FieldLabel |
| 23 | import { Input } |
| 24 | import { Spinner } |
| 25 | import { apiKeyPlugin } |
| 26 | |
| 27 | export type DeleteApiKeyDialogProps |
| 28 | open: boolean |
| 29 | onOpenChange: (open |
| 30 | apiKey: ListedApiKey |
| 31 | /** Scope the delete payload to an organization (sets `configId`). */ |
| 32 | organizationId?: string |
| 33 | }; |
| 34 | |
| 35 | export function DeleteApiKeyDialog({ |
| 36 | open, |
| 37 | onOpenChange, |
| 38 | apiKey, |
| 39 | organizationId, |
| 40 | }: DeleteApiKeyDialogProps) |
| 41 | const { authClient, |
| 42 | const { localization: |
| 43 | const preview = |
| 44 | const previewId = |
| 45 | const { mutate: |
| 46 | authClient, |
| 47 | { |
| 48 | onSuccess: () => |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | return ( |
| 53 | <AlertDialog open={ |
| 54 | <AlertDialogContent> |
| 55 | <AlertDialogHeader> |
| 56 | <AlertDialogMedia> |
| 57 | <Key /> |
| 58 | </AlertDialogMedia> |
| 59 | |
| 60 | <AlertDialogTitle>{apiKeyLocalization. |
| 61 | |
| 62 | <AlertDialogDescription> |
| 63 | {apiKeyLocalization.deleteApiKeyWarning} |
| 64 | </AlertDialogDescription> |
| 65 | </AlertDialogHeader> |
| 66 | |
| 67 | <Field> |
| 68 | <FieldLabel htmlFor={ |
| 69 | {apiKey.name || |
| 70 | </FieldLabel> |
| 71 | |
| 72 | <Input |
| 73 | id={previewId} |
| 74 | value={preview} |
| 75 | readOnly |
| 76 | className="font-mono text-xs" |
| 77 | disabled |
| 78 | /> |
| 79 | </Field> |
| 80 | |
| 81 | <AlertDialogFooter> |
| 82 | <AlertDialogCancel disabled={ |
| 83 | {localization.settings.cancel |
| 84 | </AlertDialogCancel> |
| 85 | |
| 86 | <Button |
| 87 | type="button" |
| 88 | variant="destructive" |
| 89 | disabled={isDeleting} |
| 90 | onClick={() => |
| 91 | deleteApiKey({ |
| 92 | keyId: apiKey.id |
| 93 | ...(organizationId ? |
| 94 | }) |
| 95 | } |
| 96 | > |
| 97 | {isDeleting && <Spinner |
| 98 | |
| 99 | {apiKeyLocalization.deleteApiKey} |
| 100 | </Button> |
| 101 | </AlertDialogFooter> |
| 102 | </AlertDialogContent> |
| 103 | </AlertDialog> |
| 104 | ); |
| 105 | } |