import { Check, Copy, X } from "lucide-react"; import { useState } from "react"; import { createPortal } from "react-dom"; import { api, errorMessage } from "../api"; import { copyToClipboard } from "../clipboard"; import { Alert } from "./ui"; // CopyBotTokenModal writes a non-system bot's token straight to the // clipboard without ever rendering it on screen -- the value only ever // lives in the fetch response and the clipboard API call; React state only // ever tracks whether the copy succeeded, never the token itself. export function CopyBotTokenModal({ botID, onClose }: { botID: number; onClose: () => void }) { const [reason, setReason] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const [copied, setCopied] = useState(false); async function copyToken() { if (!reason.trim()) { setError("Please enter an operation reason"); return; } setBusy(true); setError(""); setCopied(false); try { const result = await api.action("/api/actions/export-bot-token", { command_id: "", reason: reason.trim(), confirm: true, bot_user_id: botID }); const token = result.details?.token; if (result.error || typeof token !== "string" || !token) { setError(result.error || "No token returned."); return; } await copyToClipboard(token); setCopied(true); } catch (err) { setError(errorMessage(err)); } finally { setBusy(false); } } return createPortal(
{"The token is written straight to your clipboard and is never shown on screen. Paste it wherever it's needed right after copying."}
{error &&