1 import { PencilSquare, Trash, XCircle } from "@medusajs/icons"
2 import { ApiKeyDTO } from "@medusajs/types"
3 import {
4 Badge,
5 Container,
6 Copy,
7 Heading,
8 StatusBadge,
9 Text,
10 toast,
11 usePrompt,
12 } from "@medusajs/ui"
13 import { useTranslation } from "react-i18next"
14 import { useNavigate } from "react-router-dom"
15 import {
16 Action,
17 ActionMenu,
18 } from "../../../../../components/common/action-menu"
19 import { Skeleton } from "../../../../../components/common/skeleton"
20 import { UserLink } from "../../../../../components/common/user-link"
21 import {
22 useDeleteApiKey,
23 useRevokeApiKey,
24 } from "../../../../../hooks/api/api-keys"
25 import { useUser } from "../../../../../hooks/api/users"
26 import { useDate } from "../../../../../hooks/use-date"
27 import {
28 getApiKeyStatusProps,
29 getApiKeyTypeProps,
30 prettifyRedactedToken,
31 } from "../../../common/utils"
32
33 type ApiKeyGeneralSectionProps = {
34 apiKey: ApiKeyDTO
35 }
36
37 export const ApiKeyGeneralSection = ({ apiKey }: ApiKeyGeneralSectionProps) => {
38 const { t } = useTranslation()
39 const navigate = useNavigate()
40 const prompt = usePrompt()
41 const { getFullDate } = useDate()
42
43 const { mutateAsync: revokeAsync } = useRevokeApiKey(apiKey.id)
44 const { mutateAsync: deleteAsync } = useDeleteApiKey(apiKey.id)
45
46 const handleDelete = async () => {
47 const res = await prompt({
48 title: t("general.areYouSure"),
49 description: t("apiKeyManagement.delete.warning", {
50 title: apiKey.title,
51 }),
52 confirmText: t("actions.delete"),
53 cancelText: t("actions.cancel"),
54 })
55
56 if (!res) {
57 return
58 }
59
60 await deleteAsync(undefined, {
61 onSuccess: () => {
62 toast.success(
63 t("apiKeyManagement.delete.successToast", {
64 title: apiKey.title,
65 })
66 )
67 navigate("..", { replace: true })
68 },
69 onError: (err) => {
70 toast.error(err.message)
71 },
72 })
73 }
74
75 const handleRevoke = async () => {
76 const res = await prompt({
77 title: t("general.areYouSure"),
78 description: t("apiKeyManagement.revoke.warning", {
79 title: apiKey.title,
80 }),
81 confirmText: t("apiKeyManagement.actions.revoke"),
82 cancelText: t("actions.cancel"),
83 })
84
85 if (!res) {
86 return
87 }
88
89 await revokeAsync(undefined, {
90 onSuccess: () => {
91 toast.success(
92 t("apiKeyManagement.revoke.successToast", {
93 title: apiKey.title,
94 })
95 )
96 },
97 onError: (err) => {
98 toast.error(err.message)
99 },
100 })
101 }
102
103 const dangerousActions: Action[] = [
104 {
105 icon: <Trash />,
106 label: t("actions.delete"),
107 onClick: handleDelete,
108 disabled: !apiKey.revoked_at,
109 },
110 ]
111
112 if (!apiKey.revoked_at) {
113 dangerousActions.unshift({
114 icon: <XCircle />,
115 label: t("apiKeyManagement.actions.revoke"),
116 onClick: handleRevoke,
117 disabled: !!apiKey.revoked_at,
118 })
119 }
120
121 const apiKeyStatus = getApiKeyStatusProps(apiKey.revoked_at, t)
122 const apiKeyType = getApiKeyTypeProps(apiKey.type, t)
123
124 return (
125 <Container className="divide-y p-0">
126 <div className="flex items-center justify-between px-6 py-4">
127 <Heading>{apiKey.title}</Heading>
128 <div className="flex items-center gap-x-4">
129 <div className="flex items-center gap-x-2">
130 <StatusBadge color={apiKeyStatus.color}>
131 {apiKeyStatus.label}
132 </StatusBadge>
133 </div>
134 <ActionMenu
135 groups={[
136 {
137 actions: [
138 {
139 label: t("actions.edit"),
140 icon: <PencilSquare />,
141 to: "edit",
142 },
143 ],
144 },
145 {
146 actions: dangerousActions,
147 },
148 ]}
149 />
150 </div>
151 </div>
152 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
153 <Text size="small" leading="compact" weight="plus">
154 {t("fields.key")}
155 </Text>
156 {apiKey.type === "secret" ? (
157 <Badge size="2xsmall" className="inline-block w-fit">
158 {prettifyRedactedToken(apiKey.redacted)}
159 </Badge>
160 ) : (
161 <Copy asChild content={apiKey.token} className="cursor-pointer">
162 <Badge size="2xsmall" className="text-ui-tag-neutral-text">
163 {prettifyRedactedToken(apiKey.redacted)}
164 </Badge>
165 </Copy>
166 )}
167 </div>
168 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
169 <Text size="small" leading="compact" weight="plus">
170 {t("fields.type")}
171 </Text>
172 <Text size="small" leading="compact">
173 {apiKeyType.label}
174 </Text>
175 </div>
176 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
177 <Text size="small" leading="compact" weight="plus">
178 {t("apiKeyManagement.fields.lastUsedAtLabel")}
179 </Text>
180 <Text size="small" leading="compact">
181 {apiKey.last_used_at
182 ? getFullDate({ date: apiKey.last_used_at, includeTime: true })
183 : "-"}
184 </Text>
185 </div>
186 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
187 <Text size="small" leading="compact" weight="plus">
188 {t("apiKeyManagement.fields.createdByLabel")}
189 </Text>
190 <ActionBy userId={apiKey.created_by} />
191 </div>
192 {apiKey.revoked_at && (
193 <>
194 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
195 <Text size="small" leading="compact" weight="plus">
196 {t("apiKeyManagement.fields.revokedAtLabel")}
197 </Text>
198 <Text size="small" leading="compact">
199 {getFullDate({ date: apiKey.revoked_at, includeTime: true })}
200 </Text>
201 </div>
202 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
203 <Text size="small" leading="compact" weight="plus">
204 {t("apiKeyManagement.fields.revokedByLabel")}
205 </Text>
206 <ActionBy userId={apiKey.revoked_by} />
207 </div>
208 </>
209 )}
210 </Container>
211 )
212 }
213
214 const ActionBy = ({ userId }: { userId: string | null }) => {
215 const { user, isLoading, isError, error } = useUser(userId!, undefined, {
216 enabled: !!userId,
217 })
218
219 if (!userId) {
220 return (
221 <Text size="small" className="text-ui-fg-subtle">
222 -
223 </Text>
224 )
225 }
226
227 if (isError) {
228 throw error
229 }
230
231 if (isLoading) {
232 return (
233 <div className="grid grid-cols-[20px_1fr]">
234 <Skeleton className="h-5 w-5 rounded-full" />
235 <Skeleton className="w-full max-w-[220px]" />
236 </div>
237 )
238 }
239
240 if (!user) {
241 return (
242 <Text size="small" className="text-ui-fg-subtle">
243 -
244 </Text>
245 )
246 }
247
248 return <UserLink {...user} />
249 }