@@ -110,9 +110,7 @@ const Chat = () => {
110110 alicePublicKey : publicKeyRef . current
111111 } ) ;
112112
113- // TODO: need to handle image
114-
115- await sendMessage ( {
113+ const { id } = await sendMessage ( {
116114 channelID,
117115 userId,
118116 image,
@@ -125,6 +123,7 @@ const Chat = () => {
125123 setMessages ( ( prevMsg ) => {
126124 const { ...message } = prevMsg [ index ] ;
127125 message . local = false ;
126+ message . id = id ;
128127 prevMsg [ index ] = message ;
129128 return [ ...prevMsg ] ;
130129 } ) ;
@@ -148,25 +147,7 @@ const Chat = () => {
148147 } ;
149148
150149 const initChat = async ( ) => {
151- // TODO: handle error
152- // const messages = await fetchMessages(pubnub, channelID);
153- //
154- // const formatMessages = messages.map((msg) => {
155- // const {
156- // image,
157- // sender,
158- // body: { box, nonce }
159- // } = msg;
160- //
161- // return {
162- // encrypted: true,
163- // encryptionDetail: { box, nonce },
164- // sender,
165- // image,
166- // body: btoa(strToTypedArr(box)) // let's just stringify the array, to decrypt later
167- // };
168- // });
169- // setMessages(formatMessages);
150+ // TODO: restore previous messages from local storage
170151 } ;
171152
172153 useEffect ( ( ) => {
@@ -190,11 +171,6 @@ const Chat = () => {
190171 } ) ;
191172 socket . on ( 'delivered' , ( id ) => {
192173 setDeliveredID ( ( prev ) => [ ...prev , id ] ) ;
193- setMessages ( ( prev ) => {
194- const copy = [ ...prev ] ;
195- copy [ copy . length - 1 ] . id = id ;
196- return [ ...copy ] ;
197- } ) ;
198174 } ) ;
199175 // an event to notify that the other person is joined.
200176 socket . on ( 'on-alice-join' , ( { publicKey } ) => {
@@ -213,6 +189,7 @@ const Chat = () => {
213189 getSetUsers ( channelID ) ;
214190 } ) ;
215191
192+ //handle incoming message
216193 socket . on ( 'chat-message' , ( msg ) => {
217194 try {
218195 const box = strToTypedArr ( msg . message . box ) ;
@@ -245,6 +222,7 @@ const Chat = () => {
245222 } , [ channelID ] ) ;
246223
247224 const alice = usersInChannel . find ( ( u ) => u . uuid !== userId ) ;
225+ console . log ( messages ) ;
248226 const messagesFormatted = messages . map ( ( { body, sender, image, local, id } , i ) => {
249227 return {
250228 owner : sender === userId ,
0 commit comments