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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "l-min-components",
3
- "version": "1.0.1215",
3
+ "version": "1.0.1216",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src/assets",
@@ -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
- buildSocketUrl(),
238
- {
239
- share: false,
240
- shouldReconnect: () => true,
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 (lastJsonMessage && lastJsonMessage.event === "new.message.result") {
620
- /** @type {WebSocketMessageData} */
621
- const webSocketData = lastJsonMessage.data;
622
- const roomId = webSocketData.room;
623
-
624
- // Helper to get YYYY-MM-DD from ISO string (duplicate from sendMessage, consider extracting)
625
- const getLocalDateString = (isoTimestamp) => {
626
- if (!isoTimestamp) return new Date().toISOString().split("T")[0];
627
- try {
628
- return new Date(isoTimestamp).toISOString().split("T")[0];
629
- } catch (e) {
630
- console.error("Error parsing date:", e);
631
- return new Date().toISOString().split("T")[0];
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
- // --- Check if room exists in current state ---
636
- let roomExists = false;
637
- for (const courseGroup of state.roomsByCourses) {
638
- if (courseGroup.rooms.some((room) => room.id === roomId)) {
639
- roomExists = true;
640
- break;
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
- // --- If room doesn't exist, refetch room list and skip state update for this message ---
645
- if (!roomExists) {
646
- console.log(`Room ${roomId} not found in state, refetching room list.`);
647
- getMessageRoomsByCourses(null, true); // Refetch rooms once
648
- return; // Stop processing this message until room list is updated
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
- // --- 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
- };
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
- // --- 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
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
- const todayGroupIndex = updatedChatHistory.findIndex(
678
- (group) => group.date === todayDateString
679
- );
677
+ const todayGroupIndex = updatedChatHistory.findIndex(
678
+ (group) => group.date === todayDateString
679
+ );
680
680
 
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],
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.splice(todayGroupIndex, 1, updatedTodayGroup);
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
- const updatedChats = {
702
- ...prevState.chats,
703
- [roomId]: updatedChatHistory,
704
- };
701
+ const updatedChats = {
702
+ ...prevState.chats,
703
+ [roomId]: updatedChatHistory,
704
+ };
705
705
 
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
- };
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
- return courseGroup; // Return unchanged if room not in this group
741
- }
742
- );
742
+ );
743
743
 
744
- // --- Return combined state update ---
745
- return {
746
- ...prevState,
747
- chats: updatedChats,
748
- roomsByCourses: updatedRoomsByCourses,
749
- };
750
- });
751
- }
752
- }, [lastJsonMessage]); // Also depend on roomsByCourses to re-check existence after refetch
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
- // console.log(state, "messages"); // Keep console log for debugging if needed
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