refactor(messageThunk): streamline loading state management for topics (#7809)

* refactor(messageThunk): streamline loading state management for topics

- Reintroduced the handleChangeLoadingOfTopic function to manage loading states more effectively.
- Updated thunk implementations to ensure loading state is correctly set after message processing.
- Removed commented-out code for clarity and maintainability.

* fix(messageThunk): ensure loading state is managed correctly after message sending

- Added a finally block to guarantee that the loading state is updated after the sendMessage thunk execution.
- Removed commented-out code for improved clarity and maintainability.
This commit is contained in:
MyPrototypeWhat 2025-07-04 16:07:13 +08:00 committed by GitHub
parent 985859f1c3
commit 2fad7c0ff6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,6 +35,7 @@ import {
} from '@renderer/utils/messageUtils/create'
import { getMainTextContent } from '@renderer/utils/messageUtils/find'
import { getTopicQueue } from '@renderer/utils/queue'
import { waitForTopicQueue } from '@renderer/utils/queue'
import { isOnHomePage } from '@renderer/utils/window'
import { t } from 'i18next'
import { isEmpty, throttle } from 'lodash'
@ -44,10 +45,10 @@ import type { AppDispatch, RootState } from '../index'
import { removeManyBlocks, updateOneBlock, upsertManyBlocks, upsertOneBlock } from '../messageBlock'
import { newMessagesActions, selectMessagesForTopic } from '../newMessage'
// const handleChangeLoadingOfTopic = async (topicId: string) => {
// await waitForTopicQueue(topicId)
// store.dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false }))
// }
const handleChangeLoadingOfTopic = async (topicId: string) => {
await waitForTopicQueue(topicId)
store.dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false }))
}
// TODO: 后续可以将db操作移到Listener Middleware中
export const saveMessageAndBlocksToDB = async (message: Message, blocks: MessageBlock[], messageIndex: number = -1) => {
try {
@ -801,7 +802,7 @@ const fetchAndProcessAssistantResponseImpl = async (
const usage = await estimateMessagesUsage({ assistant, messages: finalContextWithAssistant })
response.usage = usage
}
dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false }))
// dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false }))
}
if (response && response.metrics) {
if (response.metrics.completion_tokens === 0 && response.usage?.completion_tokens) {
@ -887,10 +888,9 @@ export const sendMessage =
}
} catch (error) {
console.error('Error in sendMessage thunk:', error)
} finally {
handleChangeLoadingOfTopic(topicId)
}
// finally {
// handleChangeLoadingOfTopic(topicId)
// }
}
/**
@ -1136,10 +1136,9 @@ export const resendMessageThunk =
}
} catch (error) {
console.error(`[resendMessageThunk] Error resending user message ${userMessageToResend.id}:`, error)
} finally {
handleChangeLoadingOfTopic(topicId)
}
// finally {
// handleChangeLoadingOfTopic(topicId)
// }
}
/**
@ -1271,10 +1270,9 @@ export const regenerateAssistantResponseThunk =
error
)
// dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false }))
} finally {
handleChangeLoadingOfTopic(topicId)
}
// finally {
// handleChangeLoadingOfTopic(topicId)
// }
}
// --- Thunk to initiate translation and create the initial block ---
@ -1447,10 +1445,9 @@ export const appendAssistantResponseThunk =
console.error(`[appendAssistantResponseThunk] Error appending assistant response:`, error)
// Optionally dispatch an error action or notification
// Resetting loading state should be handled by the underlying fetchAndProcessAssistantResponseImpl
} finally {
handleChangeLoadingOfTopic(topicId)
}
// finally {
// handleChangeLoadingOfTopic(topicId)
// }
}
/**