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:
@@ -85,6 +85,7 @@ export interface MessageActions {
|
||||
removeConversation: (conversationId: string) => void;
|
||||
setMessages: (conversationId: string, messages: MessageResponse[]) => void;
|
||||
mergeMessages: (conversationId: string, incoming: MessageResponse[]) => void;
|
||||
removeMessage: (conversationId: string, messageId: string) => void;
|
||||
patchMessages: (conversationId: string, patches: Map<string, Partial<MessageResponse>>) => void;
|
||||
setUnreadCount: (total: number, system: number) => void;
|
||||
setLastSystemMessageAt: (time: string | null) => void;
|
||||
@@ -349,6 +350,19 @@ export const useMessageStore = create<MessageStore>((set, get) => ({
|
||||
});
|
||||
},
|
||||
|
||||
removeMessage: (conversationId: string, messageId: string) => {
|
||||
const normalizedId = normalizeConversationId(conversationId);
|
||||
set(state => {
|
||||
const existing = state.messagesMap.get(normalizedId);
|
||||
if (!existing) return state;
|
||||
const filtered = existing.filter(m => String(m.id) !== String(messageId));
|
||||
if (filtered.length === existing.length) return state;
|
||||
const newMessagesMap = new Map(state.messagesMap);
|
||||
newMessagesMap.set(normalizedId, filtered);
|
||||
return { messagesMap: newMessagesMap };
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 原子性 patch:按消息 ID 更新指定字段(如 sender、status)
|
||||
* 不影响其他消息,不丢失并发写入
|
||||
|
||||
Reference in New Issue
Block a user