| 1 | "use client"; |
| 2 | |
| 3 | import type { ListedApiKey } |
| 4 | import { useAuth, useAuthPlugin |
| 5 | import { Key, Pencil |
| 6 | import { useState } |
| 7 | |
| 8 | import { Button } |
| 9 | import { |
| 10 | Item, |
| 11 | ItemActions, |
| 12 | ItemContent, |
| 13 | ItemDescription, |
| 14 | ItemMedia, |
| 15 | ItemTitle, |
| 16 | } from "@/components/ui/item"; |
| 17 | import { apiKeyPlugin } |
| 18 | import { DeleteApiKeyDialog } |
| 19 | import { EditApiKeyDialog } |
| 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 |
| 38 | const { localization: |
| 39 | const [deleteOpen, |
| 40 | const [editOpen, |
| 41 | |
| 42 | const preview = |
| 43 | |
| 44 | return ( |
| 45 | <Item> |
| 46 | <ItemMedia variant=" |
| 47 | <Key /> |
| 48 | </ItemMedia> |
| 49 | <ItemContent> |
| 50 | <ItemTitle>{apiKey. |
| 51 | <ItemDescription className=" |
| 52 | <ItemDescription> |
| 53 | {apiKeyLocalization.created}{" |
| 54 | {new Date(apiKey |
| 55 | dateStyle: "medium", |
| 56 | timeStyle: "short", |
| 57 | })} |
| 58 | </ItemDescription> |
| 59 | <ItemDescription> |
| 60 | {apiKey.expiresAt |
| 61 | ? `${apiKeyLocalization.expires |
| 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 |
| 74 | {apiKey.remaining === |
| 75 | ? "" |
| 76 | : ` · ${apiKeyLocalization. |
| 77 | </ItemDescription> |
| 78 | <ItemDescription> |
| 79 | {apiKeyLocalization.lastRequest}: |
| 80 | {apiKey.lastRequest |
| 81 | ? new Date( |
| 82 | : apiKeyLocalization.neverRequested} |
| 83 | </ItemDescription> |
| 84 | </ItemContent> |
| 85 | <ItemActions> |
| 86 | {!hideUpdate && ( |
| 87 | <> |
| 88 | <Button |
| 89 | variant="outline" |
| 90 | size="sm" |
| 91 | onClick={() => |
| 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={() => |
| 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 | } |