l-min-components 1.6.1221 → 1.6.1223

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.6.1221",
3
+ "version": "1.6.1223",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src/assets",
@@ -26,6 +26,7 @@ export const MainLayout = styled.div`
26
26
  justify-content: space-between;
27
27
  width: 100%;
28
28
  padding: 84px 20px 20px 0;
29
+ max-height: 100vh;
29
30
  `;
30
31
 
31
32
  export const LeftLayout = styled.div`
@@ -348,48 +348,9 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
348
348
  // --- End Find latest message ID ---
349
349
 
350
350
  setState((prevState) => {
351
- let unreadCountToSubtract = 0;
352
-
353
- // --- Update roomsByCourses ---
354
- const updatedRoomsByCourses = prevState.roomsByCourses.map(
355
- (courseGroup) => {
356
- // Find the room within this course group
357
- const roomIndex = courseGroup.rooms.findIndex(
358
- (room) => room.id === roomId
359
- );
360
-
361
- if (roomIndex !== -1) {
362
- // Found the room in this course group
363
- const targetRoom = courseGroup.rooms[roomIndex];
364
- unreadCountToSubtract = targetRoom.unread_count; // Store the count before resetting
365
-
366
- // Create a new rooms array with the updated room
367
- const updatedRooms = [
368
- ...courseGroup.rooms.slice(0, roomIndex),
369
- { ...targetRoom, unread_count: 0 }, // Reset unread count for the specific room
370
- ...courseGroup.rooms.slice(roomIndex + 1),
371
- ];
372
-
373
- // Return a new course group object with updated total count and rooms
374
- return {
375
- ...courseGroup,
376
- // Ensure total_unread_count doesn't go below zero
377
- total_unread_count: Math.max(
378
- 0,
379
- courseGroup.total_unread_count - unreadCountToSubtract
380
- ),
381
- rooms: updatedRooms,
382
- };
383
- }
384
-
385
- // If the room wasn't in this group, return the group unchanged
386
- return courseGroup;
387
- }
388
- );
389
- // --- End Update roomsByCourses ---
390
-
391
- // --- Update chats ---
351
+ // --- Update chats ONLY ---
392
352
  // Retrieve existing date groups directly from the array mapped to roomId
353
+ // Count updates are now handled by readRoomMessages
393
354
  const existingDateGroups = prevState.chats[roomId] || [];
394
355
  const allDateGroups = [...existingDateGroups, ...newDateGroups];
395
356
 
@@ -425,10 +386,10 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
425
386
  // Optional: Sort the date groups themselves if needed (e.g., chronologically)
426
387
  // finalDateGroups.sort((a, b) => /* comparison logic based on date */);
427
388
 
428
- // --- Return combined state update ---
389
+ // --- Return combined state update (only updating chats here) ---
429
390
  return {
430
391
  ...prevState,
431
- roomsByCourses: updatedRoomsByCourses, // Include the updated course/room list
392
+ // roomsByCourses is NOT updated here anymore
432
393
  chats: {
433
394
  ...prevState.chats,
434
395
  // Update the structure: Map roomId directly to the array of finalDateGroups
@@ -443,19 +404,79 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
443
404
  latestMessageIdFromFetch?.id && // Optional chaining for safety
444
405
  !latestMessageIdFromFetch?.is_read // Optional chaining for safety
445
406
  ) {
446
- await readRoomMessages(latestMessageIdFromFetch.id); // Pass only the ID
407
+ // Pass both message ID and room ID to readRoomMessages
408
+ await readRoomMessages({
409
+ latestMessageId: latestMessageIdFromFetch.id,
410
+ roomId: roomId,
411
+ });
447
412
  }
448
413
 
449
414
  // Pass roomId correctly in the recursive call
450
415
  if (responseData.next) getIndividualChats(responseData.next, roomId);
451
416
  };
452
417
 
453
- // read messages (using the latest message ID passed)
454
- const readRoomMessages = async (latestMessageId) => {
455
- // Assuming the API uses the latest message ID directly in the path as defined
456
- return await request({
457
- url: `/notify/v1/enterprise/chats/${latestMessageId}/read_message/`,
458
- });
418
+ // read messages (using the latest message ID passed) and update counts
419
+ const readRoomMessages = async ({ latestMessageId, roomId }) => {
420
+ // 1. Make the API call to mark as read
421
+ try {
422
+ await request({
423
+ url: `/notify/v1/enterprise/chats/${latestMessageId}/read_message/`,
424
+ });
425
+
426
+ // 2. If API call is successful, update the local state counts
427
+ setState((prevState) => {
428
+ let unreadCountToSubtract = 0;
429
+
430
+ const updatedRoomsByCourses = prevState.roomsByCourses.map(
431
+ (courseGroup) => {
432
+ const roomIndex = courseGroup.rooms.findIndex(
433
+ (room) => room.id === roomId
434
+ );
435
+
436
+ if (roomIndex !== -1) {
437
+ const targetRoom = courseGroup.rooms[roomIndex];
438
+ // Store the count *before* resetting, only if it's positive
439
+ unreadCountToSubtract = Math.max(0, targetRoom.unread_count);
440
+
441
+ if (unreadCountToSubtract > 0) {
442
+ // Only update if there were unread messages
443
+ const updatedRooms = [
444
+ ...courseGroup.rooms.slice(0, roomIndex),
445
+ { ...targetRoom, unread_count: 0 }, // Reset count
446
+ ...courseGroup.rooms.slice(roomIndex + 1),
447
+ ];
448
+
449
+ return {
450
+ ...courseGroup,
451
+ total_unread_count: Math.max(
452
+ 0, // Ensure total doesn't go below zero
453
+ courseGroup.total_unread_count - unreadCountToSubtract
454
+ ),
455
+ rooms: updatedRooms,
456
+ };
457
+ }
458
+ }
459
+ // If room not found or no unread messages, return group unchanged
460
+ return courseGroup;
461
+ }
462
+ );
463
+
464
+ // Only return updated state if changes were actually made
465
+ if (unreadCountToSubtract > 0) {
466
+ return {
467
+ ...prevState,
468
+ roomsByCourses: updatedRoomsByCourses,
469
+ };
470
+ } else {
471
+ // No change needed, return previous state
472
+ return prevState;
473
+ }
474
+ });
475
+ } catch (error) {
476
+ console.error("Failed to mark messages as read:", error);
477
+ // Optionally handle the error (e.g., show a notification)
478
+ // We don't update the state counts if the API call fails
479
+ }
459
480
  };
460
481
 
461
482
  // sending messages
@@ -598,7 +619,11 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
598
619
  (async () => {
599
620
  await getMessageRoomsByCourses();
600
621
  // Example: Fetch initial chat for a specific room if needed on load
601
- // await getIndividualChats(null, "some-initial-room-id");
622
+ // await getIndividualChats(null, "5be9b48281ac4c2885d3b719654ed59d");
623
+ // await readRoomMessages({
624
+ // latestMessageId: "57e31128214a4269be8b9e3bb18495c4",
625
+ // roomId: "5be9b48281ac4c2885d3b719654ed59d",
626
+ // });
602
627
  })();
603
628
  }, [selectedAccount?.id]); // Rerun when selectedAccount changes
604
629
 
@@ -757,7 +782,13 @@ const useMessageKit = (affiliatesActive, selectedAccount, affiliateAccount) => {
757
782
  // send messages to account based on account type
758
783
 
759
784
  // Return the state and potentially the functions if they need to be called externally
760
- return { state, getIndividualChats, getMessageRoomsByCourses, sendMessage }; // Ensure sendMessage is returned
785
+ return {
786
+ state,
787
+ getIndividualChats,
788
+ getMessageRoomsByCourses,
789
+ sendMessage,
790
+ readRoomMessages,
791
+ };
761
792
  };
762
793
 
763
794
  export default useMessageKit;