2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-04-16 02:58:34 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { inferQueryOutput, trpc } from "@calcom/trpc/react";
|
2022-04-16 02:58:34 +00:00
|
|
|
import Button from "@calcom/ui/Button";
|
2022-07-28 19:58:26 +00:00
|
|
|
import ConfirmationDialogContent from "@calcom/ui/ConfirmationDialogContent";
|
2022-04-16 02:58:34 +00:00
|
|
|
import { Dialog, DialogTrigger } from "@calcom/ui/Dialog";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { ListItem } from "@calcom/ui/List";
|
2022-05-05 21:16:25 +00:00
|
|
|
import { Tooltip } from "@calcom/ui/Tooltip";
|
2022-11-04 15:40:46 +00:00
|
|
|
import { Badge } from "@calcom/ui/components/badge";
|
2022-04-16 02:58:34 +00:00
|
|
|
|
|
|
|
export type TApiKeys = inferQueryOutput<"viewer.apiKeys.list">[number];
|
|
|
|
|
|
|
|
export default function ApiKeyListItem(props: { apiKey: TApiKeys; onEditApiKey: () => void }) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const isExpired = props?.apiKey?.expiresAt ? props.apiKey.expiresAt < new Date() : null;
|
|
|
|
const neverExpires = props?.apiKey?.expiresAt === null;
|
|
|
|
const deleteApiKey = trpc.useMutation("viewer.apiKeys.delete", {
|
|
|
|
async onSuccess() {
|
|
|
|
await utils.invalidateQueries(["viewer.apiKeys.list"]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return (
|
2022-06-01 17:24:41 +00:00
|
|
|
<ListItem className="flex w-full p-4">
|
2022-04-16 02:58:34 +00:00
|
|
|
<div className="flex w-full justify-between">
|
|
|
|
<div className="flex max-w-full flex-col truncate">
|
|
|
|
<div className="flex space-x-2">
|
|
|
|
<span className="text-gray-900">
|
|
|
|
{props?.apiKey?.note ? props.apiKey.note : t("api_key_no_note")}
|
|
|
|
</span>
|
|
|
|
{!neverExpires && isExpired && (
|
|
|
|
<Badge className="-p-2" variant="default">
|
|
|
|
{t("expired")}
|
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 flex">
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
"flex flex-col space-x-2 space-y-1 text-xs sm:flex-row sm:space-y-0 sm:rtl:space-x-reverse",
|
|
|
|
isExpired ? "text-red-600" : "text-gray-500",
|
|
|
|
neverExpires ? "text-yellow-600" : ""
|
|
|
|
)}>
|
|
|
|
{neverExpires ? (
|
|
|
|
<div className="flex flex-row space-x-3 text-gray-500">
|
2022-08-03 16:01:29 +00:00
|
|
|
<Icon.FiAlertTriangle className="w-4" />
|
2022-04-16 02:58:34 +00:00
|
|
|
{t("api_key_never_expires")}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
`${isExpired ? t("expired") : t("expires")} ${dayjs(
|
|
|
|
props?.apiKey?.expiresAt?.toString()
|
|
|
|
).fromNow()}`
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="flex">
|
2022-07-14 11:32:28 +00:00
|
|
|
<Tooltip side="top" content={t("edit_api_key")}>
|
2022-04-16 02:58:34 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => props.onEditApiKey()}
|
|
|
|
color="minimal"
|
|
|
|
size="icon"
|
2022-08-03 16:01:29 +00:00
|
|
|
StartIcon={Icon.FiEdit2}
|
2022-04-16 02:58:34 +00:00
|
|
|
className="ml-4 w-full self-center p-2"
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
<Dialog>
|
2022-07-14 11:32:28 +00:00
|
|
|
<Tooltip side="top" content={t("delete_api_key")}>
|
2022-04-16 02:58:34 +00:00
|
|
|
<DialogTrigger asChild>
|
|
|
|
<Button
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
}}
|
2022-08-29 11:57:01 +00:00
|
|
|
color="warn"
|
2022-04-16 02:58:34 +00:00
|
|
|
size="icon"
|
2022-08-29 11:57:01 +00:00
|
|
|
StartIcon={Icon.FiTrash}
|
2022-04-16 02:58:34 +00:00
|
|
|
className="ml-2 w-full self-center p-2"
|
|
|
|
/>
|
|
|
|
</DialogTrigger>
|
|
|
|
</Tooltip>
|
|
|
|
<ConfirmationDialogContent
|
|
|
|
variety="danger"
|
|
|
|
title={t("confirm_delete_api_key")}
|
|
|
|
confirmBtnText={t("revoke_api_key")}
|
|
|
|
cancelBtnText={t("cancel")}
|
|
|
|
onConfirm={() =>
|
|
|
|
deleteApiKey.mutate({
|
|
|
|
id: props.apiKey.id,
|
|
|
|
})
|
|
|
|
}>
|
|
|
|
{t("delete_api_key_confirm_title")}
|
|
|
|
</ConfirmationDialogContent>
|
|
|
|
</Dialog>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ListItem>
|
|
|
|
);
|
|
|
|
}
|