2022-04-16 02:58:34 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-04-16 02:58:34 +00:00
|
|
|
import Button from "@calcom/ui/Button";
|
|
|
|
import { Dialog, DialogContent } 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 { List } from "@calcom/ui/List";
|
|
|
|
import { ShellSubHeading } from "@calcom/ui/Shell";
|
|
|
|
import SkeletonLoader from "@calcom/ui/apps/SkeletonLoader";
|
2022-04-16 02:58:34 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import LicenseRequired from "../../common/components/LicenseRequired";
|
|
|
|
import ApiKeyDialogForm from "./ApiKeyDialogForm";
|
|
|
|
import ApiKeyListItem, { TApiKeys } from "./ApiKeyListItem";
|
2022-05-26 17:07:14 +00:00
|
|
|
|
|
|
|
function ApiKeyListContainer() {
|
2022-04-16 02:58:34 +00:00
|
|
|
const { t } = useLocale();
|
2022-11-10 23:40:01 +00:00
|
|
|
const query = trpc.viewer.apiKeys.list.useQuery();
|
2022-07-28 19:58:26 +00:00
|
|
|
const data = query.data;
|
2022-04-16 02:58:34 +00:00
|
|
|
|
|
|
|
const [newApiKeyModal, setNewApiKeyModal] = useState(false);
|
|
|
|
const [editModalOpen, setEditModalOpen] = useState(false);
|
2022-05-26 17:07:14 +00:00
|
|
|
const [apiKeyToEdit, setApiKeyToEdit] = useState<(TApiKeys & { neverExpires?: boolean }) | null>(null);
|
2022-04-16 02:58:34 +00:00
|
|
|
return (
|
2022-06-01 17:24:41 +00:00
|
|
|
<div className="border-b border-gray-200 py-8 pl-2 pr-1">
|
|
|
|
<ShellSubHeading
|
|
|
|
className="mt-2"
|
|
|
|
title={t("api_keys")}
|
|
|
|
subtitle={t("api_keys_subtitle")}
|
|
|
|
actions={
|
2022-08-17 17:38:21 +00:00
|
|
|
<Button
|
|
|
|
color="minimal"
|
|
|
|
size="icon"
|
|
|
|
StartIcon={Icon.FiPlus}
|
|
|
|
onClick={() => setNewApiKeyModal(true)}
|
|
|
|
/>
|
2022-06-01 17:24:41 +00:00
|
|
|
}
|
|
|
|
/>
|
2022-05-26 17:07:14 +00:00
|
|
|
<LicenseRequired>
|
2022-07-28 19:58:26 +00:00
|
|
|
{(() => {
|
|
|
|
if (query.isLoading) return <SkeletonLoader />;
|
|
|
|
if (!data) return null;
|
|
|
|
return (
|
2022-05-26 17:07:14 +00:00
|
|
|
<>
|
|
|
|
{data.length > 0 && (
|
2022-06-01 17:24:41 +00:00
|
|
|
<List className="mt-6">
|
2022-05-26 17:07:14 +00:00
|
|
|
{data.map((item) => (
|
|
|
|
<ApiKeyListItem
|
|
|
|
key={item.id}
|
|
|
|
apiKey={item}
|
|
|
|
onEditApiKey={() => {
|
|
|
|
setApiKeyToEdit(item);
|
|
|
|
setEditModalOpen(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
)}
|
2022-04-16 02:58:34 +00:00
|
|
|
|
2022-05-26 17:07:14 +00:00
|
|
|
{/* New api key dialog */}
|
|
|
|
<Dialog open={newApiKeyModal} onOpenChange={(isOpen) => !isOpen && setNewApiKeyModal(false)}>
|
|
|
|
<DialogContent>
|
|
|
|
<ApiKeyDialogForm
|
|
|
|
title={t("create_api_key")}
|
|
|
|
handleClose={() => setNewApiKeyModal(false)}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
{/* Edit api key dialog */}
|
|
|
|
<Dialog open={editModalOpen} onOpenChange={(isOpen) => !isOpen && setEditModalOpen(false)}>
|
|
|
|
<DialogContent>
|
|
|
|
{apiKeyToEdit && (
|
|
|
|
<ApiKeyDialogForm
|
|
|
|
title={t("edit_api_key")}
|
|
|
|
key={apiKeyToEdit.id}
|
|
|
|
handleClose={() => setEditModalOpen(false)}
|
|
|
|
defaultValues={apiKeyToEdit}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
</>
|
2022-07-28 19:58:26 +00:00
|
|
|
);
|
|
|
|
})()}
|
2022-05-26 17:07:14 +00:00
|
|
|
</LicenseRequired>
|
2022-06-01 17:24:41 +00:00
|
|
|
</div>
|
2022-04-16 02:58:34 +00:00
|
|
|
);
|
|
|
|
}
|
2022-05-26 17:07:14 +00:00
|
|
|
|
|
|
|
export default ApiKeyListContainer;
|