feat: implement map and find optimization

This commit is contained in:
JoeMakuta 2026-04-01 18:47:57 +02:00
parent b1631cd6f1
commit 03a24686fe
3 changed files with 11 additions and 6 deletions

View file

@ -228,13 +228,14 @@ export default function NewChatPage() {
return prev; return prev;
} }
const memberById = new Map(membersData?.map((m) => [m.user_id, m]) ?? []);
const prevById = new Map(prev.map((m) => [m.id, m]));
return syncedMessages.map((msg) => { return syncedMessages.map((msg) => {
const member = msg.author_id const member = msg.author_id ? memberById.get(msg.author_id) ?? null : null;
? membersData?.find((m) => m.user_id === msg.author_id)
: null;
// Preserve existing author info if member lookup fails (e.g., cloned chats) // Preserve existing author info if member lookup fails (e.g., cloned chats)
const existingMsg = prev.find((m) => m.id === `msg-${msg.id}`); const existingMsg = prevById.get(`msg-${msg.id}`);
const existingAuthor = existingMsg?.metadata?.custom?.author as const existingAuthor = existingMsg?.metadata?.custom?.author as
| { displayName?: string | null; avatarUrl?: string | null } | { displayName?: string | null; avatarUrl?: string | null }
| undefined; | undefined;

View file

@ -246,9 +246,11 @@ export function useDocuments(
status: (doc.status as unknown as DocumentStatusType) ?? { state: "ready" }, status: (doc.status as unknown as DocumentStatusType) ?? { state: "ready" },
})); }));
const liveById = new Map(validItems.map((v) => [v.id, v]));
let updated = prev.map((existing) => { let updated = prev.map((existing) => {
if (liveIds.has(existing.id)) { if (liveIds.has(existing.id)) {
const liveItem = validItems.find((v) => v.id === existing.id); const liveItem = liveById.get(existing.id);
if (liveItem) { if (liveItem) {
return { return {
...existing, ...existing,

View file

@ -157,8 +157,10 @@ export function useInbox(
}) as InboxItem }) as InboxItem
); );
const liveById = new Map(recentItems.map((v) => [v.id, v]));
let updated = prev.map((existing) => { let updated = prev.map((existing) => {
const liveItem = recentItems.find((v) => v.id === existing.id); const liveItem = liveById.get(existing.id);
if (liveItem) { if (liveItem) {
return { return {
...existing, ...existing,