import { useState, FC } from "react"; import { useRouter } from "next/router"; import { Button } from "@plane/ui"; import { Copy, Eye, EyeOff, RefreshCw } from "lucide-react"; import { observer } from "mobx-react-lite"; // hooks import useToast from "hooks/use-toast"; // store import { RootStore } from "store/root"; import { useMobxStore } from "lib/mobx/store-provider"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; import { csvDownload } from "helpers/download.helper"; // utils import { getCurrentHookAsCSV } from "../utils"; // enum import { WebHookFormTypes } from "./index"; interface IGenerateKey { type: WebHookFormTypes.CREATE | WebHookFormTypes.EDIT; } export const GenerateKey: FC = observer((props) => { const { type } = props; // states const [regenerating, setRegenerate] = useState(false); const [shouldShowKey, setShouldShowKey] = useState(false); // router const router = useRouter(); const { workspaceSlug, webhookId } = router.query as { workspaceSlug: string; webhookId: string }; // store const { webhook: webhookStore, workspace: workspaceStore }: RootStore = useMobxStore(); // hooks const { setToastAlert } = useToast(); const handleCopySecret = () => { if (webhookStore?.webhookSecretKey) { copyTextToClipboard(webhookStore.webhookSecretKey); setToastAlert({ title: "Success", type: "success", message: "Secret key copied", }); } else { setToastAlert({ title: "Oops", type: "error", message: "Error occurred while copying secret key", }); } }; function handleRegenerate() { setRegenerate(true); webhookStore .regenerate(workspaceSlug, webhookId) .then(() => { setToastAlert({ title: "Success", type: "success", message: "Successfully regenerated", }); const csvData = getCurrentHookAsCSV( workspaceStore.currentWorkspace, webhookStore.currentWebhook, webhookStore.webhookSecretKey ); csvDownload(csvData, `Secret-key-${Date.now()}`); }) .catch((err) => { setToastAlert({ title: "Oops!", type: "error", message: err?.error, }); }) .finally(() => { setRegenerate(false); }); } const toggleShowKey = () => { setShouldShowKey((prevState) => !prevState); }; const icons = [ { Component: Copy, onClick: handleCopySecret, key: "copy" }, { Component: shouldShowKey ? EyeOff : Eye, onClick: toggleShowKey, key: "eye" }, ]; return ( <> {(type === WebHookFormTypes.EDIT || (type === WebHookFormTypes.CREATE && webhookStore?.webhookSecretKey)) && (
Secret Key
Genarate a token to sign-in the webhook payload
{webhookStore?.webhookSecretKey && shouldShowKey ? (
{webhookStore?.webhookSecretKey}
) : (
{[...Array(41)].map((_, index) => (
))}
)}
{webhookStore?.webhookSecretKey && ( <> {icons.map(({ Component, onClick, key }) => (
))} )}
{type != WebHookFormTypes.CREATE && ( )}
)} ); });