fix: copy to clipbaord from react-use is not reliable

pull/3206/head
Audric Ackermann 7 months ago
parent 92857101cd
commit db7b4b5f6d

@ -1,6 +1,6 @@
import { isEmpty } from 'lodash';
import { useState } from 'react';
import useCopyToClipboard from 'react-use/lib/useCopyToClipboard';
import { clipboard } from 'electron';
import { useHotkey } from '../../hooks/useHotkey';
import { ToastUtils } from '../../session/utils';
import { SessionButton, SessionButtonProps } from '../basic/SessionButton';
@ -19,24 +19,19 @@ export const CopyToClipboardButton = (props: CopyToClipboardButtonProps) => {
const { copyContent, onCopyComplete, hotkey = false, text } = props;
const [copied, setCopied] = useState(false);
const [{ value }, copyToClipboard] = useCopyToClipboard();
const onClick = () => {
try {
if (!copyContent && !text) {
const toCopy = copyContent || text;
if (!toCopy) {
throw Error('Nothing to copy!');
}
if (copyContent) {
copyToClipboard(copyContent);
} else if (text) {
copyToClipboard(text);
}
clipboard.writeText(toCopy);
ToastUtils.pushCopiedToClipBoard();
setCopied(true);
if (onCopyComplete) {
onCopyComplete(value);
onCopyComplete(text);
}
} catch (err) {
window.log.error('CopyToClipboard:', err);
@ -60,13 +55,12 @@ type CopyToClipboardIconProps = Omit<SessionIconButtonProps, 'children' | 'onCli
export const CopyToClipboardIcon = (props: CopyToClipboardIconProps & { copyContent: string }) => {
const { copyContent, onCopyComplete, hotkey = false } = props;
const [{ value }, copyToClipboard] = useCopyToClipboard();
const onClick = () => {
copyToClipboard(copyContent);
clipboard.writeText(copyContent);
ToastUtils.pushCopiedToClipBoard();
if (onCopyComplete) {
onCopyComplete(value);
onCopyComplete(copyContent);
}
};

Loading…
Cancel
Save