scira-chat / lib /hooks /use-copy.ts
mukaddamzaid's picture
feat: add CopyButton component and integrate copy functionality into messages
55947a0
raw
history blame
688 Bytes
import { useState, useCallback } from 'react';
export function useCopy(timeout = 2000) {
const [copied, setCopied] = useState(false);
const copy = useCallback(
async (text: string) => {
if (!navigator.clipboard) {
console.error('Clipboard API not available');
return false;
}
try {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => {
setCopied(false);
}, timeout);
return true;
} catch (error) {
console.error('Failed to copy text:', error);
return false;
}
},
[timeout]
);
return { copied, copy };
}