fix(translate): auto copy failed (#10745)

* fix(translate): auto copy failed

Because translatedContent may be stale

* refactor(translate): improve copy functionality dependency handling

Update copy callback dependencies to include setCopied and ensure proper memoization
Fix onCopy and translateText dependencies to include copy function
This commit is contained in:
Phantom 2025-10-16 19:23:09 +08:00 committed by GitHub
parent 96ce645064
commit f943f05cb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -134,15 +134,22 @@ const TranslatePage: FC = () => {
) )
// 控制复制行为 // 控制复制行为
const copy = useCallback(
async (text: string) => {
await navigator.clipboard.writeText(text)
setCopied(true)
},
[setCopied]
)
const onCopy = useCallback(async () => { const onCopy = useCallback(async () => {
try { try {
await navigator.clipboard.writeText(translatedContent) await copy(translatedContent)
setCopied(true)
} catch (error) { } catch (error) {
logger.error('Failed to copy text to clipboard:', error as Error) logger.error('Failed to copy text to clipboard:', error as Error)
window.toast.error(t('common.copy_failed')) window.toast.error(t('common.copy_failed'))
} }
}, [setCopied, t, translatedContent]) }, [copy, t, translatedContent])
/** /**
* *
@ -183,7 +190,7 @@ const TranslatePage: FC = () => {
setTimeoutTimer( setTimeoutTimer(
'auto-copy', 'auto-copy',
async () => { async () => {
await onCopy() await copy(translated)
}, },
100 100
) )
@ -200,7 +207,7 @@ const TranslatePage: FC = () => {
window.toast.error(t('translate.error.unknown') + ': ' + formatErrorMessage(e)) window.toast.error(t('translate.error.unknown') + ': ' + formatErrorMessage(e))
} }
}, },
[autoCopy, dispatch, onCopy, setTimeoutTimer, setTranslatedContent, setTranslating, t, translating] [autoCopy, copy, dispatch, setTimeoutTimer, setTranslatedContent, setTranslating, t, translating]
) )
// 控制翻译按钮是否可用 // 控制翻译按钮是否可用