l-min-components 1.0.1215 → 1.0.1216
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/hooks/messaging-kit/index.jsx +137 -135
package/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import useAxios from "axios-hooks";
|
|
2
2
|
import { useCallback, useEffect, useState } from "react";
|
|
3
|
-
import useWebSocket from "react-use-websocket";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Represents the details of the last message in a chat room overview.
|
|
@@ -233,13 +232,13 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
|
|
|
233
232
|
}, [selectedAccount?.id]);
|
|
234
233
|
|
|
235
234
|
// handle socket connection
|
|
236
|
-
const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket(
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
);
|
|
235
|
+
// const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket(
|
|
236
|
+
// buildSocketUrl(),
|
|
237
|
+
// {
|
|
238
|
+
// share: false,
|
|
239
|
+
// shouldReconnect: () => true,
|
|
240
|
+
// }
|
|
241
|
+
// );
|
|
243
242
|
|
|
244
243
|
// get account type
|
|
245
244
|
const [, request] = useAxios(
|
|
@@ -609,149 +608,152 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
|
|
|
609
608
|
})();
|
|
610
609
|
}, [selectedAccount?.id]); // Rerun when selectedAccount changes
|
|
611
610
|
|
|
612
|
-
// Effect to log incoming WebSocket messages (for debugging)
|
|
613
|
-
useEffect(() => {
|
|
614
|
-
console.log(`Got a new message: ${JSON.stringify(lastJsonMessage)}`);
|
|
615
|
-
}, [lastJsonMessage]);
|
|
616
|
-
|
|
617
611
|
// --- Handle Incoming WebSocket Messages ---
|
|
618
612
|
useEffect(() => {
|
|
619
|
-
if (
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
const
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
613
|
+
if (!selectedAccount) return;
|
|
614
|
+
const socket = new WebSocket(buildSocketUrl());
|
|
615
|
+
socket.addEventListener("message", (event) => {
|
|
616
|
+
const lastJsonMessage = JSON.parse(event.data);
|
|
617
|
+
if (lastJsonMessage && lastJsonMessage.event === "new.message.result") {
|
|
618
|
+
/** @type {WebSocketMessageData} */
|
|
619
|
+
const webSocketData = lastJsonMessage.data;
|
|
620
|
+
const roomId = webSocketData.room;
|
|
621
|
+
|
|
622
|
+
// Helper to get YYYY-MM-DD from ISO string (duplicate from sendMessage, consider extracting)
|
|
623
|
+
const getLocalDateString = (isoTimestamp) => {
|
|
624
|
+
if (!isoTimestamp) return new Date().toISOString().split("T")[0];
|
|
625
|
+
try {
|
|
626
|
+
return new Date(isoTimestamp).toISOString().split("T")[0];
|
|
627
|
+
} catch (e) {
|
|
628
|
+
console.error("Error parsing date:", e);
|
|
629
|
+
return new Date().toISOString().split("T")[0];
|
|
630
|
+
}
|
|
631
|
+
};
|
|
634
632
|
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
633
|
+
// --- Check if room exists in current state ---
|
|
634
|
+
let roomExists = false;
|
|
635
|
+
for (const courseGroup of state.roomsByCourses) {
|
|
636
|
+
if (courseGroup.rooms.some((room) => room.id === roomId)) {
|
|
637
|
+
roomExists = true;
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
641
640
|
}
|
|
642
|
-
}
|
|
643
641
|
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
642
|
+
// --- If room doesn't exist, refetch room list and skip state update for this message ---
|
|
643
|
+
if (!roomExists) {
|
|
644
|
+
console.log(
|
|
645
|
+
`Room ${roomId} not found in state, refetching room list.`
|
|
646
|
+
);
|
|
647
|
+
getMessageRoomsByCourses(null, true); // Refetch rooms once
|
|
648
|
+
return; // Stop processing this message until room list is updated
|
|
649
|
+
}
|
|
650
650
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
651
|
+
// --- Format WebSocket data into ChatMessage ---
|
|
652
|
+
/** @type {ChatMessage} */
|
|
653
|
+
const newChatMessage = {
|
|
654
|
+
id: webSocketData.id,
|
|
655
|
+
text: webSocketData.text,
|
|
656
|
+
// Map media - assuming WebSocketMedia is compatible enough
|
|
657
|
+
media: webSocketData.media,
|
|
658
|
+
sender: {
|
|
659
|
+
id: webSocketData.sender.id,
|
|
660
|
+
first_name:
|
|
661
|
+
webSocketData.sender.full_name || webSocketData.sender.name,
|
|
662
|
+
},
|
|
663
|
+
is_read: webSocketData.is_read,
|
|
664
|
+
is_delivered: webSocketData.is_delivered,
|
|
665
|
+
created_at: webSocketData.created_at,
|
|
666
|
+
user_message: webSocketData.user_message, // Crucial for unread count
|
|
667
|
+
date: getLocalDateString(webSocketData.created_at),
|
|
668
|
+
};
|
|
669
669
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
670
|
+
// --- Update State ---
|
|
671
|
+
setState((prevState) => {
|
|
672
|
+
// --- Update chats state ---
|
|
673
|
+
const currentChatHistory = prevState.chats[roomId] || [];
|
|
674
|
+
let updatedChatHistory = [...currentChatHistory];
|
|
675
|
+
const todayDateString = "today"; // Or derive actual date string
|
|
676
676
|
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
677
|
+
const todayGroupIndex = updatedChatHistory.findIndex(
|
|
678
|
+
(group) => group.date === todayDateString
|
|
679
|
+
);
|
|
680
680
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
681
|
+
if (todayGroupIndex !== -1) {
|
|
682
|
+
const todayGroup = updatedChatHistory[todayGroupIndex];
|
|
683
|
+
// Avoid adding duplicate messages if WS sends what we already added via sendMessage response
|
|
684
|
+
if (
|
|
685
|
+
!todayGroup.messages.some((msg) => msg.id === newChatMessage.id)
|
|
686
|
+
) {
|
|
687
|
+
const updatedTodayGroup = {
|
|
688
|
+
...todayGroup,
|
|
689
|
+
messages: [...todayGroup.messages, newChatMessage],
|
|
690
|
+
};
|
|
691
|
+
updatedChatHistory.splice(todayGroupIndex, 1, updatedTodayGroup);
|
|
692
|
+
}
|
|
693
|
+
} else {
|
|
694
|
+
const newTodayGroup = {
|
|
695
|
+
date: todayDateString,
|
|
696
|
+
messages: [newChatMessage],
|
|
690
697
|
};
|
|
691
|
-
updatedChatHistory
|
|
698
|
+
updatedChatHistory = [newTodayGroup, ...updatedChatHistory];
|
|
692
699
|
}
|
|
693
|
-
} else {
|
|
694
|
-
const newTodayGroup = {
|
|
695
|
-
date: todayDateString,
|
|
696
|
-
messages: [newChatMessage],
|
|
697
|
-
};
|
|
698
|
-
updatedChatHistory = [newTodayGroup, ...updatedChatHistory];
|
|
699
|
-
}
|
|
700
700
|
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
701
|
+
const updatedChats = {
|
|
702
|
+
...prevState.chats,
|
|
703
|
+
[roomId]: updatedChatHistory,
|
|
704
|
+
};
|
|
705
705
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
706
|
+
// --- Update roomsByCourses state ---
|
|
707
|
+
const isIncomingMessage = !newChatMessage.user_message;
|
|
708
|
+
const updatedRoomsByCourses = prevState.roomsByCourses.map(
|
|
709
|
+
(courseGroup) => {
|
|
710
|
+
let courseUnreadIncrement = 0;
|
|
711
|
+
const roomIndex = courseGroup.rooms.findIndex(
|
|
712
|
+
(room) => room.id === roomId
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
if (roomIndex !== -1) {
|
|
716
|
+
const updatedRooms = [...courseGroup.rooms];
|
|
717
|
+
const currentRoom = updatedRooms[roomIndex];
|
|
718
|
+
const roomUnreadIncrement = isIncomingMessage ? 1 : 0;
|
|
719
|
+
courseUnreadIncrement = roomUnreadIncrement; // Track increment for the course total
|
|
720
|
+
|
|
721
|
+
updatedRooms[roomIndex] = {
|
|
722
|
+
...currentRoom,
|
|
723
|
+
last_message: {
|
|
724
|
+
id: newChatMessage.id,
|
|
725
|
+
text: newChatMessage.text,
|
|
726
|
+
media: newChatMessage.media,
|
|
727
|
+
created_at: newChatMessage.created_at,
|
|
728
|
+
},
|
|
729
|
+
// Increment unread count only for incoming messages
|
|
730
|
+
unread_count: currentRoom.unread_count + roomUnreadIncrement,
|
|
731
|
+
};
|
|
732
|
+
return {
|
|
733
|
+
...courseGroup,
|
|
734
|
+
rooms: updatedRooms,
|
|
735
|
+
// Increment total unread count for the course group
|
|
736
|
+
total_unread_count:
|
|
737
|
+
courseGroup.total_unread_count + courseUnreadIncrement,
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
return courseGroup; // Return unchanged if room not in this group
|
|
739
741
|
}
|
|
740
|
-
|
|
741
|
-
}
|
|
742
|
-
);
|
|
742
|
+
);
|
|
743
743
|
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
744
|
+
// --- Return combined state update ---
|
|
745
|
+
return {
|
|
746
|
+
...prevState,
|
|
747
|
+
chats: updatedChats,
|
|
748
|
+
roomsByCourses: updatedRoomsByCourses,
|
|
749
|
+
};
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
return () => socket.close();
|
|
754
|
+
}, [selectedAccount?.id]);
|
|
753
755
|
|
|
754
|
-
|
|
756
|
+
console.log(state, "messages"); // Keep console log for debugging if needed
|
|
755
757
|
// send messages to account based on account type
|
|
756
758
|
|
|
757
759
|
// Return the state and potentially the functions if they need to be called externally
|