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 = () => {
|
ws.onopen = () => {
|
||||||
reconnectAttemptsRef.current = 0;
|
reconnectAttemptsRef.current = 0;
|
||||||
|
void useChatStore.getState().loadChats();
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onmessage = (messageEvent) => {
|
ws.onmessage = (messageEvent) => {
|
||||||
@@ -60,17 +61,18 @@ export function useRealtime() {
|
|||||||
if (!Number.isFinite(chatId) || !message?.id) {
|
if (!Number.isFinite(chatId) || !message?.id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let wasInserted = false;
|
||||||
if (clientMessageId && message.sender_id === authStore.me?.id) {
|
if (clientMessageId && message.sender_id === authStore.me?.id) {
|
||||||
chatStore.confirmMessageByClientId(chatId, clientMessageId, message);
|
chatStore.confirmMessageByClientId(chatId, clientMessageId, message);
|
||||||
} else {
|
} else {
|
||||||
chatStore.prependMessage(chatId, message);
|
wasInserted = chatStore.prependMessage(chatId, message);
|
||||||
}
|
}
|
||||||
if (message.sender_id !== authStore.me?.id) {
|
if (message.sender_id !== authStore.me?.id) {
|
||||||
ws.send(JSON.stringify({ event: "message_delivered", payload: { chat_id: chatId, message_id: message.id } }));
|
ws.send(JSON.stringify({ event: "message_delivered", payload: { chat_id: chatId, message_id: message.id } }));
|
||||||
if (chatId === chatStore.activeChatId) {
|
if (chatId === chatStore.activeChatId) {
|
||||||
ws.send(JSON.stringify({ event: "message_read", payload: { chat_id: chatId, message_id: message.id } }));
|
ws.send(JSON.stringify({ event: "message_read", payload: { chat_id: chatId, message_id: message.id } }));
|
||||||
chatStore.clearUnread(chatId);
|
chatStore.clearUnread(chatId);
|
||||||
} else {
|
} else if (wasInserted) {
|
||||||
chatStore.incrementUnread(chatId);
|
chatStore.incrementUnread(chatId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ interface ChatState {
|
|||||||
loadChats: (query?: string) => Promise<void>;
|
loadChats: (query?: string) => Promise<void>;
|
||||||
setActiveChatId: (chatId: number | null) => void;
|
setActiveChatId: (chatId: number | null) => void;
|
||||||
loadMessages: (chatId: number) => Promise<void>;
|
loadMessages: (chatId: number) => Promise<void>;
|
||||||
prependMessage: (chatId: number, message: Message) => void;
|
prependMessage: (chatId: number, message: Message) => boolean;
|
||||||
addOptimisticMessage: (params: {
|
addOptimisticMessage: (params: {
|
||||||
chatId: number;
|
chatId: number;
|
||||||
senderId: number;
|
senderId: number;
|
||||||
@@ -70,11 +70,12 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
|||||||
prependMessage: (chatId, message) => {
|
prependMessage: (chatId, message) => {
|
||||||
const old = get().messagesByChat[chatId] ?? [];
|
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))) {
|
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) => ({
|
set((state) => ({
|
||||||
messagesByChat: { ...state.messagesByChat, [chatId]: [...old, message] }
|
messagesByChat: { ...state.messagesByChat, [chatId]: [...old, message] }
|
||||||
}));
|
}));
|
||||||
|
return true;
|
||||||
},
|
},
|
||||||
addOptimisticMessage: ({ chatId, senderId, type, text, clientMessageId }) => {
|
addOptimisticMessage: ({ chatId, senderId, type, text, clientMessageId }) => {
|
||||||
const old = get().messagesByChat[chatId] ?? [];
|
const old = get().messagesByChat[chatId] ?? [];
|
||||||
|
|||||||
Reference in New Issue
Block a user