mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 22:52:08 +08:00
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:
parent
96ce645064
commit
f943f05cb1
@ -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]
|
||||||
)
|
)
|
||||||
|
|
||||||
// 控制翻译按钮是否可用
|
// 控制翻译按钮是否可用
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user