| 4 | import { useAuth, useAuthPlugin } from "@better-auth-ui/react"; |
| 5 | import { Key, Pencil, X } from "lucide-react"; |
| 6 | import { useState } from "react"; |
| 7 |
| 8 | import { Button } from "@/components/ui/button"; |
| 9 | import { |
| 10 | Item, |
| 11 | ItemActions, |
| 12 | ItemContent, |
| 13 | ItemDescription, |
| 14 | ItemMedia, |
| 15 | ItemTitle, |
| 16 | } from "@/components/ui/item"; |
| 17 | import { apiKeyPlugin } from "@/lib/auth/api-key-plugin"; |
| 18 | import { DeleteApiKeyDialog } from "./delete-api-key-dialog"; |
| 19 | import { EditApiKeyDialog } from "./edit-api-key-dialog"; |
| 20 |
| 21 | export type ApiKeyProps = { |
| 22 | apiKey: ListedApiKey; |
| 23 | /** Hide the row's delete button (e.g., when caller lacks `apiKey:delete`). */ |
| 24 | hideDelete?: boolean; |
| 25 | /** Hide the row's edit button (e.g., when caller lacks `apiKey:update`). */ |
| 26 | hideUpdate?: boolean; |
| 27 | /** Scope the delete payload to an organization (sets `configId`). */ |
| 28 | organizationId?: string; |
| 29 | }; |
| 30 |
| 31 | export function ApiKey({ |
| 32 | apiKey, |
| 33 | hideDelete, |
| 34 | hideUpdate, |
| 35 | organizationId, |
| 36 | }: ApiKeyProps) { |
| 37 | const { localization } = useAuth(); |
| 38 | const { localization: apiKeyLocalization } = useAuthPlugin(apiKeyPlugin); |
| 39 | const [deleteOpen, setDeleteOpen] = useState(false); |
| 40 | const [editOpen, setEditOpen] = useState(false); |
| 41 |
| 42 | const preview = `${apiKey.start}${"*".repeat(16)}`; |
| 43 |
| 44 | return ( |
| 45 | <Item> |
| 46 | <ItemMedia variant="icon"> |
| 47 | <Key /> |
| 48 | </ItemMedia> |
| 49 | <ItemContent> |
| 50 | <ItemTitle>{apiKey.name || apiKeyLocalization.apiKey}</ItemTitle> |
| 51 | <ItemDescription className="font-mono">{preview}</ItemDescription> |
| 52 | <ItemDescription> |
| 53 | {apiKeyLocalization.created}{" "} |
| 54 | {new Date(apiKey.createdAt).toLocaleString(undefined, { |
| 55 | dateStyle: "medium", |
| 56 | timeStyle: "short", |
| 57 | })} |
| 58 | </ItemDescription> |
| 59 | <ItemDescription> |
| 60 | {apiKey.expiresAt |
| 61 | ? `${apiKeyLocalization.expires} ${new Date( |
| 62 | apiKey.expiresAt, |
| 63 | ).toLocaleString(undefined, { |
| 64 | dateStyle: "medium", |
| 65 | timeStyle: "short", |
| 66 | })}` |
| 67 | : apiKeyLocalization.neverExpires} |
| 68 | </ItemDescription> |
| 69 | <ItemDescription> |
| 70 | {apiKey.enabled |
| 71 | ? apiKeyLocalization.enabled |
| 72 | : apiKeyLocalization.disabled} |
| 73 | {` · ${apiKeyLocalization.requests}: ${apiKey.requestCount}`} |
| 74 | {apiKey.remaining === null |
| 75 | ? "" |
| 76 | : ` · ${apiKeyLocalization.remaining}: ${apiKey.remaining}`} |
| 77 | </ItemDescription> |
| 78 | <ItemDescription> |
| 79 | {apiKeyLocalization.lastRequest}:{" "} |
| 80 | {apiKey.lastRequest |
| 81 | ? new Date(apiKey.lastRequest).toLocaleString() |
| 82 | : apiKeyLocalization.neverRequested} |
| 83 | </ItemDescription> |
| 84 | </ItemContent> |
| 85 | <ItemActions> |
| 86 | {!hideUpdate && ( |
| 87 | <> |
| 88 | <Button |
| 89 | variant="outline" |
| 90 | size="sm" |
| 91 | onClick={() => setEditOpen(true)} |
| 92 | > |
| 93 | <Pencil /> |
| 94 | {apiKeyLocalization.editApiKey} |
| 95 | </Button> |
| 96 | <EditApiKeyDialog |
| 97 | apiKey={apiKey} |
| 98 | open={editOpen} |
| 99 | onOpenChange={setEditOpen} |
| 100 | /> |
| 101 | </> |
| 102 | )} |
| 103 | {!hideDelete && ( |
| 104 | <> |
| 105 | <Button |
| 106 | variant="outline" |
| 107 | size="sm" |
| 108 | onClick={() => setDeleteOpen(true)} |
| 109 | aria-label={apiKeyLocalization.deleteApiKey} |
| 110 | > |
| 111 | <X /> |
| 112 |
| 113 | {localization.settings.delete} |
| 114 | </Button> |
| 115 |
| 116 | <DeleteApiKeyDialog |
| 117 | open={deleteOpen} |
| 118 | onOpenChange={setDeleteOpen} |
| 119 | apiKey={apiKey} |
| 120 | organizationId={organizationId} |
| 121 | /> |
| 122 | </> |
| 123 | )} |
| 124 | </ItemActions> |
| 125 | </Item> |
| 126 | ); |
| 127 | } |