WelcometoMyGarden/welcometomygarden

New incoming chats don't stream to an open client

Closed this issue · 2 comments

New messages in existing chats stream to a client, but new chats themselves do not. If the user is using client-side navigation in a session, and happens to be checking if they have any new chats at the end of that, the chat view will display an out-of-date state. A reload is required to show any new incoming chats.

User behavior as mentioned here #75 (comment) further strengthens the need to fix this issue.

There is already code that should stream in new chats, perhaps it has a bug or isn't fully utilized:

const q = query(collection(db(), CHATS), where('users', 'array-contains', currentUserId));
return onSnapshot(
q,
async (querySnapshot) => {
const changes = querySnapshot.docChanges();
const amount = querySnapshot.size;
let counter = 0;
await Promise.all(
changes.map(async (change) => {
const chat = change.doc.data();
const partnerId = chat.users.find((id: string) => currentUserId !== id);
const partner = await getPublicUserProfile(partnerId);
chat.partner = partner;
addChat({ id: change.doc.id, ...chat });
counter++;
})
);
if (counter === amount) hasInitialized.set(true);
},
(err) => {
hasInitialized.set(true);
console.error(err);
throw err;
}
);