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 () => {
try {
await navigator.clipboard.writeText(translatedContent)
setCopied(true)
await copy(translatedContent)
} catch (error) {
logger.error('Failed to copy text to clipboard:', error as Error)
window.toast.error(t('common.copy_failed'))
}
}, [setCopied, t, translatedContent])
}, [copy, t, translatedContent])
/**
*
@ -183,7 +190,7 @@ const TranslatePage: FC = () => {
setTimeoutTimer(
'auto-copy',
async () => {
await onCopy()
await copy(translated)
},
100
)
@ -200,7 +207,7 @@ const TranslatePage: FC = () => {
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]
)
// 控制翻译按钮是否可用