Импорт
This commit is contained in:
@@ -123,7 +123,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
|
||||
set({ isLoadingMessages: true });
|
||||
const currentMessages = state.messages[chatId] || [];
|
||||
const cursor = !reset && currentMessages.length > 0 ? currentMessages[0].createdAt : undefined;
|
||||
const cursor = !reset && currentMessages.length > 0 ? currentMessages[0].sequenceId.toString() : undefined;
|
||||
|
||||
const fetched = await ChatApi.getMessages(chatId, cursor);
|
||||
|
||||
@@ -132,12 +132,13 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
const existing = reset ? [] : (state.messages[chatId] || []);
|
||||
const fetchedIds = new Set(fetched.map(m => m.id));
|
||||
const socketOnly = existing.filter(m => !fetchedIds.has(m.id));
|
||||
const merged = [...fetched, ...socketOnly].sort(
|
||||
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
|
||||
);
|
||||
const merged = [...fetched, ...socketOnly].sort((a, b) => {
|
||||
const tDiff = new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime();
|
||||
return tDiff !== 0 ? tDiff : a.sequenceId - b.sequenceId;
|
||||
});
|
||||
return {
|
||||
messages: { ...state.messages, [chatId]: merged },
|
||||
hasMoreMessages: { ...state.hasMoreMessages, [chatId]: fetched.length === 100 },
|
||||
hasMoreMessages: { ...state.hasMoreMessages, [chatId]: fetched.length >= 50 },
|
||||
isLoadingMessages: false,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user