feat(message): enhance message store management and UI stability
Implement `removeMessage` in `useMessageStore` to allow for atomic message deletion from the state. Improve `useChatScreen` loading logic to prevent layout jumps and ensure consistent UI state during initial message loading. Additionally, add type safety for WebSocket sequence numbers and optimize message selector stability. - Add `removeMessage` action to `useMessageStore` for state-consistent deletions - Refine `useChatScreen` loading state logic to prevent FlashList layout issues - Ensure `useMessages` hook returns a stable `EMPTY_MESSAGES` constant - Cast WebSocket `seq` to integer to ensure consistent numeric comparison - Update `useChatScreen` to use `useMessageStore` for immediate local deletion feedback
This commit is contained in:
@@ -18,6 +18,8 @@ import { messageManager } from './MessageManager';
|
||||
import { useMessageStore } from './store';
|
||||
import { useUnreadCountQuery } from '../../hooks/useUnreadCount';
|
||||
|
||||
const EMPTY_MESSAGES: MessageResponse[] = [];
|
||||
|
||||
// ==================== useConversations - 获取会话列表 ====================
|
||||
|
||||
interface UseConversationsReturn {
|
||||
@@ -100,9 +102,10 @@ export function useMessages(conversationId: string | null): UseMessagesReturn {
|
||||
|
||||
// 使用 useShallow 避免每次返回新数组引用导致的无限循环
|
||||
const messages = useMessageStore(
|
||||
useShallow(state =>
|
||||
normalizedConversationId ? (state.messagesMap.get(normalizedConversationId) || []) : []
|
||||
)
|
||||
useShallow(state => {
|
||||
if (!normalizedConversationId) return EMPTY_MESSAGES;
|
||||
return state.messagesMap.get(normalizedConversationId) ?? EMPTY_MESSAGES;
|
||||
})
|
||||
);
|
||||
const isLoadingMessages = useMessageStore(state =>
|
||||
normalizedConversationId ? state.loadingMessagesSet.has(normalizedConversationId) : false
|
||||
|
||||
Reference in New Issue
Block a user