From db7b4b5f6d92bc89e981cbda8fef531657a217c0 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Fri, 30 Aug 2024 16:46:34 +1000 Subject: [PATCH] fix: copy to clipbaord from react-use is not reliable --- .../buttons/CopyToClipboardButton.tsx | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/ts/components/buttons/CopyToClipboardButton.tsx b/ts/components/buttons/CopyToClipboardButton.tsx index c9161a00b..943476f71 100644 --- a/ts/components/buttons/CopyToClipboardButton.tsx +++ b/ts/components/buttons/CopyToClipboardButton.tsx @@ -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 { 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); } };