import { useCallback, useEffect, useState } from 'react'; import { useSuperagentConversationRuntime } from './runtimeContext'; // Copy text to the clipboard via the host-injected `copyToClipboard` adapter, // with a transient `copied` flag for confirmation UI. Components read this hook // directly instead of drilling the handler as a prop. `canCopy` is false when the // host hasn't wired clipboard, so callers can hide the affordance entirely. export function useClipboard(resetDelayMs = 1500) { const { copyToClipboard } = useSuperagentConversationRuntime(); const [copied, setCopied] = useState(false); useEffect(() => { if (!copied) return; const timer = setTimeout(() => setCopied(false), resetDelayMs); return () => clearTimeout(timer); }, [copied, resetDelayMs]); const copy = useCallback(async (text: string) => { const trimmed = text?.trim(); if (!trimmed || !copyToClipboard) return false; try { await copyToClipboard(trimmed); setCopied(true); // confirm only once the clipboard write actually succeeds return true; } catch { return false; // leave the icon unchanged so a failed copy doesn't claim success } }, [copyToClipboard]); return { canCopy: Boolean(copyToClipboard), copied, copy }; }