Skip to content

Commit 7fec0b2

Browse files
committed
fix contact list bug
1 parent 9259f25 commit 7fec0b2

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

packages/messenger-widget/src/hooks/conversation/useConversation.tsx

+23-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ export const useConversation = (config: Config) => {
3737
);
3838
}, [selectedContactName, contacts]);
3939

40+
const _setContactsSafe = (newContacts: ContactPreview[]) => {
41+
setContacts((prev) => {
42+
//We do not want to add duplicates to the list
43+
const withoutDuplicates = prev.filter(
44+
(existingContact) =>
45+
!newContacts.some(
46+
(newContact) =>
47+
newContact.contactDetails.account.ensName ===
48+
existingContact.contactDetails.account.ensName,
49+
),
50+
);
51+
52+
return [...withoutDuplicates, ...newContacts];
53+
});
54+
};
55+
4056
//For now we do not support pagination hence we always fetch all pages
4157
useEffect(() => {
4258
setConversationsInitialized(false);
@@ -48,6 +64,8 @@ export const useConversation = (config: Config) => {
4864
}
4965
const currentConversationsPage = await getConversations(page);
5066

67+
console.log('currentConversationsPage', currentConversationsPage);
68+
5169
//Hydrate the contacts by fetching their profile and DS profile
5270
const storedContacts = await Promise.all(
5371
currentConversationsPage.map((conversation) => {
@@ -64,16 +82,9 @@ export const useConversation = (config: Config) => {
6482
}),
6583
);
6684
//It might be the case that contacts are added via websocket. In this case we do not want to add them again
67-
const contactsWithoutDuplicates = storedContacts.filter(
68-
(newContact) =>
69-
!contacts.some(
70-
(existingContact) =>
71-
existingContact.contactDetails.account.ensName ===
72-
newContact.contactDetails.account.ensName,
73-
),
74-
);
7585

76-
setContacts((prev) => [...prev, ...contactsWithoutDuplicates]);
86+
_setContactsSafe(storedContacts);
87+
7788
//as long as there is no pagination we fetch the next page until we get an empty page
7889
if (currentConversationsPage.length > 0) {
7990
await init(page + 1);
@@ -104,7 +115,7 @@ export const useConversation = (config: Config) => {
104115
mainnetProvider,
105116
defaultConversation,
106117
);
107-
setContacts((prev) => [hydratedDefaultContact, ...prev]);
118+
_setContactsSafe([hydratedDefaultContact]);
108119
}
109120
}
110121
};
@@ -123,6 +134,7 @@ export const useConversation = (config: Config) => {
123134
};
124135

125136
const addConversation = (_ensName: string) => {
137+
console.log('add new conversation ');
126138
const ensName = normalizeEnsName(_ensName);
127139
const alreadyAddedContact = contacts.find(
128140
(existingContact) =>
@@ -139,7 +151,7 @@ export const useConversation = (config: Config) => {
139151

140152
const newContact: ContactPreview = getDefaultContract(ensName);
141153
//Set the new contact to the list
142-
setContacts((prev) => [...prev, newContact]);
154+
_setContactsSafe([newContact]);
143155
//Add the contact to the storage in the background
144156
addConversationAsync(ensName);
145157
//Hydrate the contact in the background

0 commit comments

Comments
 (0)