mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-02 20:58:12 +00:00
40 lines
895 B
TypeScript
40 lines
895 B
TypeScript
import { useTranslation } from 'next-i18next';
|
|
import { useToast } from './useToast';
|
|
|
|
/**
|
|
* copy text data
|
|
*/
|
|
export const useCopyData = () => {
|
|
const { t } = useTranslation();
|
|
const { toast } = useToast();
|
|
|
|
return {
|
|
copyData: async (
|
|
data: string,
|
|
title: string | null = t('common.Copy Successful'),
|
|
duration = 1000
|
|
) => {
|
|
try {
|
|
if (navigator.clipboard) {
|
|
await navigator.clipboard.writeText(data);
|
|
} else {
|
|
throw new Error('');
|
|
}
|
|
} catch (error) {
|
|
const textarea = document.createElement('textarea');
|
|
textarea.value = data;
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
document.execCommand('copy');
|
|
document.body?.removeChild(textarea);
|
|
}
|
|
|
|
toast({
|
|
title,
|
|
status: 'success',
|
|
duration
|
|
});
|
|
}
|
|
};
|
|
};
|