Files
tkmind_go/ui/desktop/src/components/MessageCopyLink.tsx
T
Zane 77ea27f5f5 UI update with sidebar and settings tabs (#3288)
Co-authored-by: Nahiyan Khan <nahiyan@squareup.com>
Co-authored-by: Taylor Ho <taylorkmho@gmail.com>
Co-authored-by: Lily Delalande <119957291+lily-de@users.noreply.github.com>
Co-authored-by: Spence <spencrmartin@gmail.com>
Co-authored-by: spencrmartin <spencermartin@squareup.com>
Co-authored-by: Judson Stephenson <Jud@users.noreply.github.com>
Co-authored-by: Max Novich <mnovich@squareup.com>
Co-authored-by: Best Codes <106822363+The-Best-Codes@users.noreply.github.com>
Co-authored-by: caroline-a-mckenzie <cmckenzie@squareup.com>
Co-authored-by: Michael Neale <michael.neale@gmail.com>
2025-07-15 17:24:41 -07:00

61 lines
2.0 KiB
TypeScript

/* global ClipboardItem */
import React, { useState } from 'react';
import { Copy } from './icons';
interface MessageCopyLinkProps {
text: string;
contentRef: React.RefObject<HTMLElement>;
}
export default function MessageCopyLink({ text, contentRef }: MessageCopyLinkProps) {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
try {
if (contentRef?.current) {
// Create a temporary container to handle HTML content
const container = document.createElement('div');
container.innerHTML = contentRef.current.innerHTML;
// Clean up any copy buttons from the content
const copyButtons = container.querySelectorAll('button');
copyButtons.forEach((button) => button.remove());
// Create the clipboard data
const clipboardData = new ClipboardItem({
'text/plain': new Blob([text], { type: 'text/plain' }),
'text/html': new Blob([container.innerHTML], { type: 'text/html' }),
});
await navigator.clipboard.write([clipboardData]);
} else {
await navigator.clipboard.writeText(text);
}
setCopied(true);
setTimeout(() => setCopied(false), 2000); // Reset after 2 seconds
} catch (err) {
console.error('Failed to copy text: ', err);
// Fallback to plain text if HTML copy fails
try {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch (fallbackErr) {
console.error('Failed to copy text (fallback): ', fallbackErr);
}
}
};
return (
<button
onClick={handleCopy}
className="flex font-mono items-center gap-1 text-xs text-textSubtle hover:cursor-pointer hover:text-textProminent transition-all duration-200 opacity-0 group-hover:opacity-100 -translate-y-4 group-hover:translate-y-0"
>
<Copy className="h-3 w-3" />
<span>{copied ? 'Copied!' : 'Copy'}</span>
</button>
);
}