fix(web-core): stabilize unread sync and realtime dedup behavior
Some checks failed
CI / test (push) Failing after 22s
Some checks failed
CI / test (push) Failing after 22s
- make prependMessage return insertion status for dedup-safe unread increments - increment unread only on newly inserted incoming messages - trigger chat list refresh on websocket reconnect to resync unread counters
This commit is contained in:
@@ -42,6 +42,7 @@ export function useRealtime() {
|
||||
|
||||
ws.onopen = () => {
|
||||
reconnectAttemptsRef.current = 0;
|
||||
void useChatStore.getState().loadChats();
|
||||
};
|
||||
|
||||
ws.onmessage = (messageEvent) => {
|
||||
@@ -60,17 +61,18 @@ export function useRealtime() {
|
||||
if (!Number.isFinite(chatId) || !message?.id) {
|
||||
return;
|
||||
}
|
||||
let wasInserted = false;
|
||||
if (clientMessageId && message.sender_id === authStore.me?.id) {
|
||||
chatStore.confirmMessageByClientId(chatId, clientMessageId, message);
|
||||
} else {
|
||||
chatStore.prependMessage(chatId, message);
|
||||
wasInserted = chatStore.prependMessage(chatId, message);
|
||||
}
|
||||
if (message.sender_id !== authStore.me?.id) {
|
||||
ws.send(JSON.stringify({ event: "message_delivered", payload: { chat_id: chatId, message_id: message.id } }));
|
||||
if (chatId === chatStore.activeChatId) {
|
||||
ws.send(JSON.stringify({ event: "message_read", payload: { chat_id: chatId, message_id: message.id } }));
|
||||
chatStore.clearUnread(chatId);
|
||||
} else {
|
||||
} else if (wasInserted) {
|
||||
chatStore.incrementUnread(chatId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ interface ChatState {
|
||||
loadChats: (query?: string) => Promise<void>;
|
||||
setActiveChatId: (chatId: number | null) => void;
|
||||
loadMessages: (chatId: number) => Promise<void>;
|
||||
prependMessage: (chatId: number, message: Message) => void;
|
||||
prependMessage: (chatId: number, message: Message) => boolean;
|
||||
addOptimisticMessage: (params: {
|
||||
chatId: number;
|
||||
senderId: number;
|
||||
@@ -70,11 +70,12 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
prependMessage: (chatId, message) => {
|
||||
const old = get().messagesByChat[chatId] ?? [];
|
||||
if (old.some((m) => m.id === message.id || (message.client_message_id && m.client_message_id === message.client_message_id))) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
set((state) => ({
|
||||
messagesByChat: { ...state.messagesByChat, [chatId]: [...old, message] }
|
||||
}));
|
||||
return true;
|
||||
},
|
||||
addOptimisticMessage: ({ chatId, senderId, type, text, clientMessageId }) => {
|
||||
const old = get().messagesByChat[chatId] ?? [];
|
||||
|
||||
Reference in New Issue
Block a user