fix(web): robust date parsing and merge strategy in conversation store
This commit is contained in:
@@ -2,6 +2,13 @@ import { create } from "zustand";
|
|||||||
import { api } from "../lib/api.ts";
|
import { api } from "../lib/api.ts";
|
||||||
import { type Reaction } from "./message.ts";
|
import { type Reaction } from "./message.ts";
|
||||||
|
|
||||||
|
function parseDate(iso: string): number {
|
||||||
|
if (!iso) return 0;
|
||||||
|
const normalized = iso.includes("T") ? iso : iso.replace(" ", "T");
|
||||||
|
const t = new Date(normalized).getTime();
|
||||||
|
return isNaN(t) ? 0 : t;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ConversationMember {
|
export interface ConversationMember {
|
||||||
id: string;
|
id: string;
|
||||||
username: string;
|
username: string;
|
||||||
@@ -89,17 +96,26 @@ export const useConversationStore = create<ConversationState>((set, get) => ({
|
|||||||
`/conversations/${conversationId}/messages${params}`,
|
`/conversations/${conversationId}/messages${params}`,
|
||||||
);
|
);
|
||||||
const list = Array.isArray(messages) ? messages : [];
|
const list = Array.isArray(messages) ? messages : [];
|
||||||
set((state) => ({
|
set((state) => {
|
||||||
messagesByConversation: {
|
const existing = state.messagesByConversation[conversationId] || [];
|
||||||
...state.messagesByConversation,
|
const map = new Map<string, ConversationMessage>();
|
||||||
[conversationId]: list,
|
existing.forEach((m) => map.set(m.id, m));
|
||||||
},
|
list.forEach((m) => map.set(m.id, m));
|
||||||
hasMoreByConversation: {
|
const merged = Array.from(map.values()).sort(
|
||||||
...state.hasMoreByConversation,
|
(a, b) => parseDate(a.created_at) - parseDate(b.created_at)
|
||||||
[conversationId]: list.length >= 50,
|
);
|
||||||
},
|
return {
|
||||||
isLoading: false,
|
messagesByConversation: {
|
||||||
}));
|
...state.messagesByConversation,
|
||||||
|
[conversationId]: merged,
|
||||||
|
},
|
||||||
|
hasMoreByConversation: {
|
||||||
|
...state.hasMoreByConversation,
|
||||||
|
[conversationId]: list.length >= 50,
|
||||||
|
},
|
||||||
|
isLoading: false,
|
||||||
|
};
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({ isLoading: false, error: error instanceof Error ? error.message : "Failed" });
|
set({ isLoading: false, error: error instanceof Error ? error.message : "Failed" });
|
||||||
}
|
}
|
||||||
@@ -118,17 +134,24 @@ export const useConversationStore = create<ConversationState>((set, get) => ({
|
|||||||
`/conversations/${conversationId}/messages?before=${encodeURIComponent(oldestId)}`,
|
`/conversations/${conversationId}/messages?before=${encodeURIComponent(oldestId)}`,
|
||||||
);
|
);
|
||||||
const list = Array.isArray(older) ? older : [];
|
const list = Array.isArray(older) ? older : [];
|
||||||
set((state) => ({
|
set((state) => {
|
||||||
messagesByConversation: {
|
const map = new Map<string, ConversationMessage>();
|
||||||
...state.messagesByConversation,
|
[...list, ...existing].forEach((m) => map.set(m.id, m));
|
||||||
[conversationId]: [...list, ...existing],
|
const merged = Array.from(map.values()).sort(
|
||||||
},
|
(a, b) => parseDate(a.created_at) - parseDate(b.created_at)
|
||||||
hasMoreByConversation: {
|
);
|
||||||
...state.hasMoreByConversation,
|
return {
|
||||||
[conversationId]: list.length >= 50,
|
messagesByConversation: {
|
||||||
},
|
...state.messagesByConversation,
|
||||||
isLoadingOlder: false,
|
[conversationId]: merged,
|
||||||
}));
|
},
|
||||||
|
hasMoreByConversation: {
|
||||||
|
...state.hasMoreByConversation,
|
||||||
|
[conversationId]: list.length >= 50,
|
||||||
|
},
|
||||||
|
isLoadingOlder: false,
|
||||||
|
};
|
||||||
|
});
|
||||||
} catch {
|
} catch {
|
||||||
set({ isLoadingOlder: false });
|
set({ isLoadingOlder: false });
|
||||||
}
|
}
|
||||||
@@ -156,7 +179,7 @@ export const useConversationStore = create<ConversationState>((set, get) => ({
|
|||||||
messagesByConversation: {
|
messagesByConversation: {
|
||||||
...state.messagesByConversation,
|
...state.messagesByConversation,
|
||||||
[message.conversation_id]: [...existing, message]
|
[message.conversation_id]: [...existing, message]
|
||||||
.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()),
|
.sort((a, b) => parseDate(a.created_at) - parseDate(b.created_at)),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user