import { useState, FC } from "react"; import { useRouter } from "next/router"; import { Copy, Eye, EyeOff, RefreshCw } from "lucide-react"; import { observer } from "mobx-react-lite"; // hooks import { useWebhook, useWorkspace } from "hooks/store"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; import { csvDownload } from "helpers/download.helper"; // utils import { getCurrentHookAsCSV } from "../utils"; // ui import { Button, Tooltip, TOAST_TYPE, setToast } from "@plane/ui"; // types import { IWebhook } from "@plane/types"; type Props = { data: Partial; }; export const WebhookSecretKey: FC = observer((props) => { const { data } = props; // states const [isRegenerating, setIsRegenerating] = useState(false); const [shouldShowKey, setShouldShowKey] = useState(false); // router const router = useRouter(); const { workspaceSlug, webhookId } = router.query; // store hooks const { currentWorkspace } = useWorkspace(); const { currentWebhook, regenerateSecretKey, webhookSecretKey } = useWebhook(); const handleCopySecretKey = () => { if (!webhookSecretKey) return; copyTextToClipboard(webhookSecretKey) .then(() => setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Secret key copied to clipboard.", }) ) .catch(() => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Error occurred while copying secret key.", }) ); }; const handleRegenerateSecretKey = () => { if (!workspaceSlug || !data.id) return; setIsRegenerating(true); regenerateSecretKey(workspaceSlug.toString(), data.id) .then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "New key regenerated successfully.", }); if (currentWebhook && webhookSecretKey) { const csvData = getCurrentHookAsCSV(currentWorkspace, currentWebhook, webhookSecretKey); csvDownload(csvData, `webhook-secret-key-${Date.now()}`); } }) .catch((err) => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }) ) .finally(() => setIsRegenerating(false)); }; const toggleShowKey = () => setShouldShowKey((prevState) => !prevState); const SECRET_KEY_OPTIONS = [ { label: "View secret key", Icon: shouldShowKey ? EyeOff : Eye, onClick: toggleShowKey, key: "eye" }, { label: "Copy secret key", Icon: Copy, onClick: handleCopySecretKey, key: "copy" }, ]; return ( <> {(data || webhookSecretKey) && (
{webhookId &&
Secret key
}
Generate a token to sign-in to the webhook payload
{shouldShowKey ? (

{webhookSecretKey}

) : (
{[...Array(30)].map((_, index) => (
))}
)}
{webhookSecretKey && (
{SECRET_KEY_OPTIONS.map((option) => ( ))}
)}
{data && (
)}
)} ); });