Skip to content

Commit 93ea088

Browse files
committed
fix: lint warnings
1 parent d0c7a7f commit 93ea088

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

src/CuteChat.tsx

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,20 @@ export function CuteChat(props: CuteChatProps) {
118118
[chatId]
119119
);
120120

121-
const prepareSnapshot = async (
122-
snapshot: FirebaseFirestore.QuerySnapshot
123-
): Promise<SnapshotChange[]> => {
124-
console.log('Preparing snapshot');
125-
return Promise.all(
126-
snapshot.docChanges().map(async (change) => ({
127-
type: change.type,
128-
message: await docToMessage(change.doc),
129-
}))
130-
);
131-
};
121+
const prepareSnapshot = useCallback(
122+
async (
123+
snapshot: FirebaseFirestore.QuerySnapshot
124+
): Promise<SnapshotChange[]> => {
125+
console.log('Preparing snapshot');
126+
return Promise.all(
127+
snapshot.docChanges().map(async (change) => ({
128+
type: change.type,
129+
message: await docToMessage(change.doc),
130+
}))
131+
);
132+
},
133+
[docToMessage]
134+
);
132135

133136
const appendSnapshot = (
134137
currentMessages: IMessage[],
@@ -142,9 +145,9 @@ export function CuteChat(props: CuteChatProps) {
142145
case 'removed':
143146
console.log('Message remove id:', change.message._id);
144147
const deleteIndex = newMessages.findIndex(
145-
(m) => change.message._id == m._id
148+
(m) => change.message._id === m._id
146149
);
147-
if (deleteIndex == -1) {
150+
if (deleteIndex === -1) {
148151
console.log('Message does not exist in currentMessage');
149152
break;
150153
}
@@ -155,9 +158,9 @@ export function CuteChat(props: CuteChatProps) {
155158
case 'modified':
156159
console.log('Message modified or added id:', change.message._id);
157160
const modifiedIndex = newMessages.findIndex(
158-
(m) => change.message._id == m._id
161+
(m) => change.message._id === m._id
159162
);
160-
if (modifiedIndex == -1) {
163+
if (modifiedIndex === -1) {
161164
console.log('Message added');
162165
newMessages.push(change.message);
163166
} else {
@@ -267,7 +270,14 @@ export function CuteChat(props: CuteChatProps) {
267270
unsubscribeOldMessages();
268271
unsubscribeNewMessages();
269272
};
270-
}, [chatId, docToMessage, markMessagesAsRead, setIsLoading, startDate]);
273+
}, [
274+
chatId,
275+
docToMessage,
276+
markMessagesAsRead,
277+
setIsLoading,
278+
startDate,
279+
prepareSnapshot,
280+
]);
271281

272282
// Handle outgoing messages
273283
const onSend = async (newMessages: IMessage[] = []) => {
@@ -329,6 +339,7 @@ export function CuteChat(props: CuteChatProps) {
329339
}
330340
};
331341

342+
// Function to fetch more messages
332343
const fetchMoreMessages = useCallback(async () => {
333344
if (initializing) {
334345
return;
@@ -353,15 +364,9 @@ export function CuteChat(props: CuteChatProps) {
353364
} catch (error) {
354365
console.error('Error fetching more messages: ', error);
355366
}
356-
}, [
357-
chatId,
358-
lastMessageDoc,
359-
docToMessage,
360-
markMessagesAsRead,
361-
setIsLoading,
362-
initializing,
363-
]);
367+
}, [chatId, lastMessageDoc, setIsLoading, initializing, prepareSnapshot]);
364368

369+
// Keep `lastMessageDoc` up to date based on `messages`
365370
useEffect(() => {
366371
if (!messages.length) {
367372
setLastMessageDoc(null);
@@ -370,19 +375,27 @@ export function CuteChat(props: CuteChatProps) {
370375

371376
try {
372377
const lastMessage = messages[messages.length - 1];
373-
console.log('Las message: ', lastMessage);
378+
379+
if (!lastMessage) {
380+
console.log('No last message. Skipping setting last message.');
381+
return;
382+
}
383+
384+
console.log('Last message: ', lastMessage);
374385
const lastMessageRef = firestore().doc(
375386
`chats/${chatId}/messages/${lastMessage._id}`
376387
);
388+
377389
const unsubscribe = lastMessageRef.onSnapshot(async (snapshot) => {
378390
setLastMessageDoc(snapshot);
379391
});
380392

381393
return () => unsubscribe();
382394
} catch (error) {
383395
console.error('Failed to set lastMessageDoc:', error);
396+
return;
384397
}
385-
}, [messages]);
398+
}, [messages, chatId]);
386399

387400
console.log('Amount of msgs:', messages.length);
388401

0 commit comments

Comments
 (0)