import { useState } from "react"; import Row from "@moduletrace/twui/layout/Row"; import Span from "@moduletrace/twui/layout/Span"; import { Check, Copy, Eye, EyeOff, KeyRound } from "lucide-react"; import revealSecret from "@/src/functions/frontend/reveal-secret"; import copyPendingText from "@/src/utils/copy-pending-text"; import useSecretRef from "../(hooks)/use-secret-ref"; import SecretChipButton from "./secret-chip-button"; type Props = { env_id: number; }; /** * A secret a bot showed (`secretRef`): its key, masked, with Reveal and * Copy for whoever may see it (`canRevealEnv`). The value is fetched only * on a click, held only while shown, and never in the page or the thread. */ export default function SecretRefChip({ env_id }: Props) { const { view, loading } = useSecretRef({ env_id }); const [value, setValue] = useState(); const [copied, setCopied] = useState(false); const [error, setError] = useState(); if (!view) { return ( {loading ? `secret ${env_id}` : `secret ${env_id} (not available)`} ); } const reveal = async () => { if (value !== undefined) { setValue(undefined); return; } setError(undefined); try { setValue(await revealSecret({ env_id })); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } }; const copy = async () => { setError(undefined); const ok = await copyPendingText({ text: revealSecret({ env_id }) }).catch((e: Error) => { setError(e.message); return false; }); if (!ok) return; setCopied(true); setTimeout(() => setCopied(false), 2000); }; return ( {view.key} {value ?? "••••••••"} {view.can_reveal ? ( <> ) : null} {error ? {error} : null} ); }