fix: resolve ActionTranslate stalling after initialization (#12329)

* fix: resolve ActionTranslate stalling after initialization

Issue: When invoking translate from the selection assistant, the fetchResult function does not react to the completion of initialize, causing the ActionTranslate component to enter an infinite loading state.

Cause: In commit 680bda3993, the initialize effect hook was refactored into a callback function. This refactor omitted the notification that fetchResult should run after initialization, so fetchResult never executes post‑initialization.

Fix: Change the initialized flag from a ref to a state variable and have fetchResult listen to this state. This modification ensures the effect hook triggers fetchResult exactly once after initialization is complete.

* fix(ActionTranslate): fix missing dependency in useEffect hook

---------

Co-authored-by: icarus <eurfelux@gmail.com>
This commit is contained in:
Little White Dog 2026-01-07 11:53:10 +08:00 committed by GitHub
parent 6b0bb64795
commit c940b5613f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -51,9 +51,9 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
const [isContented, setIsContented] = useState(false) const [isContented, setIsContented] = useState(false)
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
const [contentToCopy, setContentToCopy] = useState('') const [contentToCopy, setContentToCopy] = useState('')
const [initialized, setInitialized] = useState(false)
// Use useRef for values that shouldn't trigger re-renders // Use useRef for values that shouldn't trigger re-renders
const initialized = useRef(false)
const assistantRef = useRef<Assistant | null>(null) const assistantRef = useRef<Assistant | null>(null)
const topicRef = useRef<Topic | null>(null) const topicRef = useRef<Topic | null>(null)
const askId = useRef('') const askId = useRef('')
@ -85,7 +85,7 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
// Initialize values only once // Initialize values only once
const initialize = useCallback(async () => { const initialize = useCallback(async () => {
if (initialized.current) { if (initialized) {
logger.silly('[initialize] Already initialized.') logger.silly('[initialize] Already initialized.')
return return
} }
@ -114,8 +114,8 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
// Initialize topic // Initialize topic
topicRef.current = getDefaultTopic(currentAssistant.id) topicRef.current = getDefaultTopic(currentAssistant.id)
initialized.current = true setInitialized(true)
}, [action.selectedText, isLanguagesLoaded, updateLanguagePair]) }, [action.selectedText, initialized, isLanguagesLoaded, updateLanguagePair])
// Try to initialize when: // Try to initialize when:
// 1. action.selectedText change (generally will not) // 1. action.selectedText change (generally will not)
@ -126,7 +126,7 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
}, [initialize]) }, [initialize])
const fetchResult = useCallback(async () => { const fetchResult = useCallback(async () => {
if (!assistantRef.current || !topicRef.current || !action.selectedText || !initialized.current) return if (!assistantRef.current || !topicRef.current || !action.selectedText || !initialized) return
const setAskId = (id: string) => { const setAskId = (id: string) => {
askId.current = id askId.current = id
@ -174,7 +174,7 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
assistantRef.current = assistant assistantRef.current = assistant
logger.debug('process once') logger.debug('process once')
processMessages(assistant, topicRef.current, assistant.content, setAskId, onStream, onFinish, onError) processMessages(assistant, topicRef.current, assistant.content, setAskId, onStream, onFinish, onError)
}, [action, targetLanguage, alterLanguage, scrollToBottom]) }, [action, targetLanguage, alterLanguage, scrollToBottom, initialized])
useEffect(() => { useEffect(() => {
fetchResult() fetchResult()
@ -189,7 +189,7 @@ const ActionTranslate: FC<Props> = ({ action, scrollToBottom }) => {
}, [allMessages]) }, [allMessages])
const handleChangeLanguage = (targetLanguage: TranslateLanguage, alterLanguage: TranslateLanguage) => { const handleChangeLanguage = (targetLanguage: TranslateLanguage, alterLanguage: TranslateLanguage) => {
if (!initialized.current) { if (!initialized) {
return return
} }
setTargetLanguage(targetLanguage) setTargetLanguage(targetLanguage)