@@ -37,6 +37,22 @@ export const useConversation = (config: Config) => {
37
37
) ;
38
38
} , [ selectedContactName , contacts ] ) ;
39
39
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
+
40
56
//For now we do not support pagination hence we always fetch all pages
41
57
useEffect ( ( ) => {
42
58
setConversationsInitialized ( false ) ;
@@ -48,6 +64,8 @@ export const useConversation = (config: Config) => {
48
64
}
49
65
const currentConversationsPage = await getConversations ( page ) ;
50
66
67
+ console . log ( 'currentConversationsPage' , currentConversationsPage ) ;
68
+
51
69
//Hydrate the contacts by fetching their profile and DS profile
52
70
const storedContacts = await Promise . all (
53
71
currentConversationsPage . map ( ( conversation ) => {
@@ -64,16 +82,9 @@ export const useConversation = (config: Config) => {
64
82
} ) ,
65
83
) ;
66
84
//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
- ) ;
75
85
76
- setContacts ( ( prev ) => [ ...prev , ...contactsWithoutDuplicates ] ) ;
86
+ _setContactsSafe ( storedContacts ) ;
87
+
77
88
//as long as there is no pagination we fetch the next page until we get an empty page
78
89
if ( currentConversationsPage . length > 0 ) {
79
90
await init ( page + 1 ) ;
@@ -104,7 +115,7 @@ export const useConversation = (config: Config) => {
104
115
mainnetProvider ,
105
116
defaultConversation ,
106
117
) ;
107
- setContacts ( ( prev ) => [ hydratedDefaultContact , ... prev ] ) ;
118
+ _setContactsSafe ( [ hydratedDefaultContact ] ) ;
108
119
}
109
120
}
110
121
} ;
@@ -123,6 +134,7 @@ export const useConversation = (config: Config) => {
123
134
} ;
124
135
125
136
const addConversation = ( _ensName : string ) => {
137
+ console . log ( 'add new conversation ' ) ;
126
138
const ensName = normalizeEnsName ( _ensName ) ;
127
139
const alreadyAddedContact = contacts . find (
128
140
( existingContact ) =>
@@ -139,7 +151,7 @@ export const useConversation = (config: Config) => {
139
151
140
152
const newContact : ContactPreview = getDefaultContract ( ensName ) ;
141
153
//Set the new contact to the list
142
- setContacts ( ( prev ) => [ ... prev , newContact ] ) ;
154
+ _setContactsSafe ( [ newContact ] ) ;
143
155
//Add the contact to the storage in the background
144
156
addConversationAsync ( ensName ) ;
145
157
//Hydrate the contact in the background
0 commit comments