cherry-studio/src/renderer/src/hooks/useMessageOperations.ts
MyPrototypeWhat 2fd3ebb378
pert: Optimize/message structure (#3136)
* refactor: Simplify message operations with new useMessageOperations hook

- Introduce useMessageOperations hook to centralize message-related actions
- Remove prop drilling for message deletion and management
- Refactor MessageMenubar, MessageGroup, and Messages components to use new hook
- Remove commented-out code and simplify message state management
- Improve type safety and reduce component complexity

* feat: Enhance topic management with sequence-based sorting and lazy loading

- Add sequence field to topics for better sorting
- Implement lazy loading mechanism for topic messages
- Modify Redux store to support per-topic loading states
- Update database schema to use sequence as an auto-incrementing primary key
- Optimize message initialization and retrieval process

* refactor: Simplify message operations with new useMessageOperations hook

- Introduce useMessageOperations hook to centralize message-related actions
- Remove prop drilling for message deletion and management
- Refactor MessageMenubar, MessageGroup, and Messages components to use new hook
- Remove commented-out code and simplify message state management
- Improve type safety and reduce component complexity

* refactor(database): Enhance topic management with timestamps and upgrade logic

- Modify database schema to include createdAt and updatedAt for topics
- Add database hooks for automatic timestamp handling
- Refactor topic upgrade process to support new timestamp fields
- Remove redundant upgradesV6.ts file
- Update topic retrieval to use updatedAt for sorting
- Improve database consistency and tracking of topic modifications

* fix: Improve message loading state management and UI synchronization

- Update Inputbar to use useMessageOperations hook for loading state
- Correct topic loading state management in Redux store
- Fix loading state synchronization in sendMessage action
- Remove unnecessary commented-out code
- Enhance error handling and loading state tracking

* refactor: Streamline message state management and remove unused code

- Remove commented-out code in multiple components
- Delete initializeMessagesState thunk from messages store
- Simplify message sending and streaming logic
- Remove unnecessary console logs
- Optimize MessageStream component with memo
- Using loading to control message generation within a single session
- Lift the restriction on not being able to switch topics in message generation
2025-03-11 11:43:22 +08:00

169 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
import { useAppDispatch, useAppSelector } from '@renderer/store'
import {
clearStreamMessage,
clearTopicMessages,
commitStreamMessage,
resendMessage,
selectDisplayCount,
selectTopicLoading,
selectTopicMessages,
setStreamMessage,
updateMessage,
updateMessages
} from '@renderer/store/messages'
import type { Assistant, Message, Topic } from '@renderer/types'
import { useCallback } from 'react'
/**
* 自定义Hook提供消息操作相关的功能
*
* @param topic 当前主题
* @returns 一组消息操作方法
*/
export function useMessageOperations(topic: Topic) {
const dispatch = useAppDispatch()
const messages = useAppSelector((state) => selectTopicMessages(state, topic.id))
/**
* 删除单个消息
*/
const deleteMessage = useCallback(
async (message: Message) => {
const newMessages = messages.filter((m) => m.id !== message.id)
await dispatch(updateMessages(topic, newMessages))
},
[dispatch, topic, messages]
)
/**
* 删除一组消息基于askId
*/
const deleteGroupMessages = useCallback(
async (askId: string) => {
const newMessages = messages.filter((m) => m.askId !== askId)
await dispatch(updateMessages(topic, newMessages))
},
[dispatch, topic, messages]
)
/**
* 编辑消息内容
*/
const editMessage = useCallback(
async (messageId: string, updates: Partial<Message>) => {
await dispatch(
updateMessage({
topicId: topic.id,
messageId,
updates
})
)
},
[dispatch, topic.id]
)
/**
* 重新发送消息
*/
const resendMessageAction = useCallback(
async (message: Message, assistant: Assistant, isMentionModel = false) => {
return dispatch(resendMessage(message, assistant, topic, isMentionModel))
},
[dispatch, topic]
)
/**
* 重新发送用户消息(编辑后)
*/
const resendUserMessageWithEdit = useCallback(
async (message: Message, editedContent: string, assistant: Assistant) => {
// 先更新消息内容
await editMessage(message.id, { content: editedContent })
// 然后重新发送
return dispatch(resendMessage({ ...message, content: editedContent }, assistant, topic))
},
[dispatch, editMessage, topic]
)
/**
* 设置流式消息
*/
const setStreamMessageAction = useCallback(
(message: Message | null) => {
dispatch(setStreamMessage({ topicId: topic.id, message }))
},
[dispatch, topic.id]
)
/**
* 提交流式消息
*/
const commitStreamMessageAction = useCallback(
(messageId: string) => {
dispatch(commitStreamMessage({ topicId: topic.id, messageId }))
},
[dispatch, topic.id]
)
/**
* 清除流式消息
*/
const clearStreamMessageAction = useCallback(
(messageId: string) => {
dispatch(clearStreamMessage({ topicId: topic.id, messageId }))
},
[dispatch, topic.id]
)
/**
* 清除会话消息
*/
const clearTopicMessagesAction = useCallback(
async (_topicId?: string) => {
await dispatch(clearTopicMessages(_topicId || topic.id))
},
[dispatch, topic.id]
)
/**
* 更新消息数据
*/
const updateMessagesAction = useCallback(
async (messages: Message[]) => {
await dispatch(updateMessages(topic, messages))
},
[dispatch, topic]
)
/**
* 创建新的上下文clear message
*/
const createNewContext = useCallback(async () => {
EventEmitter.emit(EVENT_NAMES.NEW_CONTEXT)
}, [])
const loading = useAppSelector((state) => selectTopicLoading(state, topic.id))
const displayCount = useAppSelector(selectDisplayCount)
// /**
// * 获取当前消息列表
// */
// const getMessages = useCallback(() => messages, [messages])
return {
messages,
loading,
displayCount,
updateMessages: updateMessagesAction,
deleteMessage,
deleteGroupMessages,
editMessage,
resendMessage: resendMessageAction,
resendUserMessageWithEdit,
setStreamMessage: setStreamMessageAction,
commitStreamMessage: commitStreamMessageAction,
clearStreamMessage: clearStreamMessageAction,
createNewContext,
clearTopicMessages: clearTopicMessagesAction
}
}