2023-11-27 06:44:06 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { mutate } from "swr";
|
2024-05-07 07:14:36 +00:00
|
|
|
// types
|
2024-03-19 14:38:35 +00:00
|
|
|
import { IApiToken } from "@plane/types";
|
2024-05-07 07:14:36 +00:00
|
|
|
// ui
|
2024-03-06 13:09:14 +00:00
|
|
|
import { TOAST_TYPE, setToast } from "@plane/ui";
|
2024-05-07 07:14:36 +00:00
|
|
|
// components
|
2024-03-19 14:38:35 +00:00
|
|
|
import { CreateApiTokenForm, GeneratedTokenDetails } from "@/components/api-token";
|
2024-05-07 07:14:36 +00:00
|
|
|
import { EModalPosition, EModalWidth, ModalCore } from "@/components/core";
|
|
|
|
// fetch-keys
|
2024-03-19 14:38:35 +00:00
|
|
|
import { API_TOKENS_LIST } from "@/constants/fetch-keys";
|
2024-05-07 07:14:36 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { renderFormattedDate } from "@/helpers/date-time.helper";
|
|
|
|
import { csvDownload } from "@/helpers/download.helper";
|
2024-05-07 07:14:36 +00:00
|
|
|
// services
|
2024-03-19 14:38:35 +00:00
|
|
|
import { APITokenService } from "@/services/api_token.service";
|
2023-11-27 06:44:06 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const apiTokenService = new APITokenService();
|
|
|
|
|
|
|
|
export const CreateApiTokenModal: React.FC<Props> = (props) => {
|
|
|
|
const { isOpen, onClose } = props;
|
|
|
|
// states
|
|
|
|
const [neverExpires, setNeverExpires] = useState<boolean>(false);
|
|
|
|
const [generatedToken, setGeneratedToken] = useState<IApiToken | null | undefined>(null);
|
|
|
|
// router
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug } = router.query;
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
onClose();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
setNeverExpires(false);
|
|
|
|
setGeneratedToken(null);
|
|
|
|
}, 350);
|
|
|
|
};
|
|
|
|
|
|
|
|
const downloadSecretKey = (data: IApiToken) => {
|
|
|
|
const csvData = {
|
|
|
|
Title: data.label,
|
|
|
|
Description: data.description,
|
2024-01-02 09:15:51 +00:00
|
|
|
Expiry: data.expired_at ? renderFormattedDate(data.expired_at)?.replace(",", " ") ?? "" : "Never expires",
|
2023-11-27 06:44:06 +00:00
|
|
|
"Secret key": data.token ?? "",
|
|
|
|
};
|
|
|
|
|
|
|
|
csvDownload(csvData, `secret-key-${Date.now()}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCreateToken = async (data: Partial<IApiToken>) => {
|
|
|
|
if (!workspaceSlug) return;
|
|
|
|
|
|
|
|
// make the request to generate the token
|
|
|
|
await apiTokenService
|
|
|
|
.createApiToken(workspaceSlug.toString(), data)
|
|
|
|
.then((res) => {
|
|
|
|
setGeneratedToken(res);
|
|
|
|
downloadSecretKey(res);
|
|
|
|
|
|
|
|
mutate<IApiToken[]>(
|
|
|
|
API_TOKENS_LIST(workspaceSlug.toString()),
|
|
|
|
(prevData) => {
|
|
|
|
if (!prevData) return;
|
|
|
|
|
|
|
|
return [res, ...prevData];
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2024-03-06 08:48:41 +00:00
|
|
|
setToast({
|
|
|
|
type: TOAST_TYPE.ERROR,
|
2024-05-07 09:33:24 +00:00
|
|
|
title: "Error!",
|
2024-03-06 08:48:41 +00:00
|
|
|
message: err.message,
|
2023-11-27 06:44:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-05-07 07:14:36 +00:00
|
|
|
<ModalCore isOpen={isOpen} handleClose={() => {}} position={EModalPosition.TOP} width={EModalWidth.XXL}>
|
|
|
|
{generatedToken ? (
|
|
|
|
<GeneratedTokenDetails handleClose={handleClose} tokenDetails={generatedToken} />
|
|
|
|
) : (
|
|
|
|
<CreateApiTokenForm
|
|
|
|
handleClose={handleClose}
|
|
|
|
neverExpires={neverExpires}
|
|
|
|
toggleNeverExpires={() => setNeverExpires((prevData) => !prevData)}
|
|
|
|
onSubmit={handleCreateToken}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ModalCore>
|
2023-11-27 06:44:06 +00:00
|
|
|
);
|
|
|
|
};
|