"use client"; import { useEffect, useState } from "react"; import { toast } from "sonner"; import { Copy, Download, ShieldAlert } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { useI18n } from "@/i18n/i18n-context"; import { copyToClipboard } from "@/lib/clipboard"; import type { CreatedApiKey } from "@/types/api-key"; interface ApiKeySecretRevealedDialogProps { apiKey: CreatedApiKey | null; onClose: () => void; } export function ApiKeySecretRevealedDialog({ apiKey, onClose, }: ApiKeySecretRevealedDialogProps) { const { t } = useI18n(); const [confirmed, setConfirmed] = useState(false); useEffect(() => { if (apiKey) setConfirmed(false); }, [apiKey]); const open = Boolean(apiKey); async function copy(value: string, label: string) { try { await copyToClipboard(value); toast.success(`${t("common.copied")} ${label}`); } catch { toast.error(t("common.copyFailed")); } } function download() { if (!apiKey) return; const tail = apiKey.serviceCredentialId.slice(-6); const stamp = new Date().toISOString().replace(/[:.]/g, "-"); const content = [ `ServiceCredentialId: ${apiKey.serviceCredentialId}`, `ServiceCredentialSecret: ${apiKey.serviceCredentialSecret}`, `ServiceCredentialName: ${apiKey.serviceCredentialName}`, `ServiceName: ${apiKey.serviceName}`, `CreateTime: ${apiKey.createTime}`, `UserPrincipalName: ${apiKey.userPrincipalName}`, "", "WARNING: This secret cannot be retrieved again. Store it safely.", "", ].join("\n"); const blob = new Blob([content], { type: "text/plain;charset=utf-8" }); const url = URL.createObjectURL(blob); const anchor = document.createElement("a"); anchor.href = url; anchor.download = `tablestore-api-key-${tail}-${stamp}.txt`; document.body.appendChild(anchor); anchor.click(); document.body.removeChild(anchor); URL.revokeObjectURL(url); } function handleOpenChange(nextOpen: boolean) { if (!nextOpen && !confirmed) return; if (!nextOpen) onClose(); } return ( { if (!confirmed) event.preventDefault(); }} onPointerDownOutside={(event) => { if (!confirmed) event.preventDefault(); }} > {t("apiKeys.secret.title")} {t("apiKeys.secret.description")} {apiKey ? (
{apiKey.serviceCredentialId}
{apiKey.serviceCredentialSecret}
) : null}
); }