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

Loading…
Cancel
Save