| 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 { useUpdateApiKey } |
| 9 | import { useEffect } |
| 10 | import { buttonVariants } |
| 11 | import { |
| 12 | Dialog, |
| 13 | DialogClose, |
| 14 | DialogContent, |
| 15 | DialogFooter, |
| 16 | DialogHeader, |
| 17 | DialogTitle, |
| 18 | } from "@/components/ui/dialog"; |
| 19 | import { |
| 20 | Field, |
| 21 | FieldError, |
| 22 | FieldGroup, |
| 23 | FieldLabel, |
| 24 | } from "@/components/ui/field"; |
| 25 | import { Input } |
| 26 | import { apiKeyPlugin } |
| 27 | import { useAuthForm } |
| 28 | |
| 29 | export function EditApiKeyDialog({ |
| 30 | apiKey, |
| 31 | open, |
| 32 | onOpenChange, |
| 33 | }: { |
| 34 | apiKey: ListedApiKey |
| 35 | open: boolean |
| 36 | onOpenChange: (open |
| 37 | }) { |
| 38 | const { authClient, |
| 39 | const { localization: |
| 40 | |
| 41 | const updateApiKey = |
| 42 | onSuccess: () => |
| 43 | }); |
| 44 | const form = |
| 45 | defaultValues: { name: apiKey |
| 46 | onSubmit: async ( |
| 47 | await updateApiKey.mutateAsync |
| 48 | configId: apiKey.configId |
| 49 | keyId: apiKey.id |
| 50 | name: value.name |
| 51 | }), |
| 52 | }); |
| 53 | useEffect(() => |
| 54 | if (open) form |
| 55 | }, [apiKey.name |
| 56 | return ( |
| 57 | <Dialog open={ |
| 58 | <DialogContent> |
| 59 | <form.AppForm> |
| 60 | <form.AuthFormRoot className=" |
| 61 | <DialogHeader> |
| 62 | <DialogTitle>{labels. |
| 63 | </DialogHeader> |
| 64 | <FieldGroup> |
| 65 | <form.AppField name=" |
| 66 | {(field) => |
| 67 | <Field> |
| 68 | <FieldLabel htmlFor={ |
| 69 | {labels.name} |
| 70 | </FieldLabel> |
| 71 | <Input |
| 72 | id={`api-key-name-${apiKey |
| 73 | name={field.name |
| 74 | value={field.state |
| 75 | onBlur={field.handleBlur |
| 76 | onChange={(event) |
| 77 | field.handleChange(event |
| 78 | } |
| 79 | /> |
| 80 | </Field> |
| 81 | )} |
| 82 | </form.AppField> |
| 83 | {updateApiKey.error && |
| 84 | <FieldError> |
| 85 | {updateApiKey.error.error |
| 86 | updateApiKey.error.message |
| 87 | </FieldError> |
| 88 | )} |
| 89 | </FieldGroup> |
| 90 | <DialogFooter> |
| 91 | <DialogClose |
| 92 | className={buttonVariants({ |
| 93 | type="button" |
| 94 | > |
| 95 | {localization.settings.cancel |
| 96 | </DialogClose> |
| 97 | <form.AuthFormSubmitButton |
| 98 | isPending={updateApiKey.isPending |
| 99 | disabled={updateApiKey.isPending |
| 100 | > |
| 101 | {localization.settings.saveChanges |
| 102 | </form.AuthFormSubmitButton> |
| 103 | </DialogFooter> |
| 104 | </form.AuthFormRoot> |
| 105 | </form.AppForm> |
| 106 | </DialogContent> |
| 107 | </Dialog> |
| 108 | ); |
| 109 | } |