l-min-components 1.6.1247 → 1.6.1248
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 +171 -122
package/package.json
CHANGED
|
@@ -374,148 +374,159 @@ const useMessageKit = (/*affiliatesActive*/) => {
|
|
|
374
374
|
// Add roomId to loading state
|
|
375
375
|
setState((prevState) => ({
|
|
376
376
|
...prevState,
|
|
377
|
-
loadingRoomIds: new Set([...prevState.loadingRoomIds, roomId])
|
|
377
|
+
loadingRoomIds: new Set([...prevState.loadingRoomIds, roomId]),
|
|
378
378
|
}));
|
|
379
379
|
|
|
380
380
|
try {
|
|
381
381
|
let response;
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (
|
|
394
|
-
String(selectedAccount.type).toLowerCase() === "instructor" &&
|
|
395
|
-
affiliateAccount
|
|
396
|
-
// &&
|
|
397
|
-
// affiliatesActive
|
|
398
|
-
) {
|
|
399
|
-
response = await request({
|
|
400
|
-
url: nextPage || `/notify/v1/instructor/${affiliateAccount}/chats/`,
|
|
401
|
-
params: {
|
|
402
|
-
_account: selectedAccount.id,
|
|
403
|
-
room_id: roomId,
|
|
404
|
-
limit,
|
|
405
|
-
},
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
if (!response?.data) return;
|
|
382
|
+
if (String(selectedAccount.type).toLowerCase() === "enterprise") {
|
|
383
|
+
response = await request({
|
|
384
|
+
url: nextPage || "/notify/v1/enterprise/chats/",
|
|
385
|
+
params: {
|
|
386
|
+
_account: selectedAccount.id,
|
|
387
|
+
room_id: roomId,
|
|
388
|
+
limit,
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
}
|
|
409
392
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
393
|
+
if (
|
|
394
|
+
String(selectedAccount.type).toLowerCase() === "instructor" &&
|
|
395
|
+
affiliateAccount
|
|
396
|
+
// &&
|
|
397
|
+
// affiliatesActive
|
|
398
|
+
) {
|
|
399
|
+
response = await request({
|
|
400
|
+
url: nextPage || `/notify/v1/instructor/${affiliateAccount}/chats/`,
|
|
401
|
+
params: {
|
|
402
|
+
_account: selectedAccount.id,
|
|
403
|
+
room_id: roomId,
|
|
404
|
+
limit,
|
|
405
|
+
},
|
|
406
|
+
});
|
|
423
407
|
}
|
|
424
|
-
}
|
|
425
|
-
// --- End Find latest message ID ---
|
|
426
408
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
const messagesGroupedByDate = {};
|
|
436
|
-
allDateGroups.forEach((dateGroup) => {
|
|
437
|
-
if (!messagesGroupedByDate[dateGroup.date]) {
|
|
438
|
-
// Use a Map for efficient de-duplication by message ID
|
|
439
|
-
messagesGroupedByDate[dateGroup.date] = {
|
|
440
|
-
date: dateGroup.date, // Store the date string
|
|
441
|
-
messagesMap: new Map(),
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
dateGroup.messages.forEach((message) => {
|
|
445
|
-
messagesGroupedByDate[dateGroup.date].messagesMap.set(
|
|
446
|
-
message.id,
|
|
447
|
-
message
|
|
448
|
-
);
|
|
409
|
+
if (String(selectedAccount.type).toLowerCase() === "personal") {
|
|
410
|
+
response = await request({
|
|
411
|
+
url: nextPage || `/notify/v1/chats/`,
|
|
412
|
+
params: {
|
|
413
|
+
_account: selectedAccount.id,
|
|
414
|
+
room_id: roomId,
|
|
415
|
+
limit,
|
|
416
|
+
},
|
|
449
417
|
});
|
|
450
|
-
}
|
|
418
|
+
}
|
|
419
|
+
if (!response?.data) return;
|
|
420
|
+
|
|
421
|
+
/** @type {MessageApiResponseData} */
|
|
422
|
+
const responseData = response.data;
|
|
423
|
+
const { results: newDateGroups } = responseData;
|
|
424
|
+
|
|
425
|
+
// --- Find the latest message ID from the current fetch ---
|
|
426
|
+
let latestMessageIdFromFetch = null;
|
|
427
|
+
if (newDateGroups && newDateGroups.length > 0) {
|
|
428
|
+
const allNewMessages = newDateGroups.flatMap((group) => group.messages);
|
|
429
|
+
if (allNewMessages.length > 0) {
|
|
430
|
+
allNewMessages.sort(
|
|
431
|
+
(a, b) => new Date(b.created_at) - new Date(a.created_at)
|
|
432
|
+
);
|
|
433
|
+
latestMessageIdFromFetch = allNewMessages[0];
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// --- End Find latest message ID ---
|
|
451
437
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
}
|
|
461
|
-
|
|
438
|
+
setState((prevState) => {
|
|
439
|
+
// --- Update chats ONLY ---
|
|
440
|
+
// Retrieve existing date groups directly from the array mapped to roomId
|
|
441
|
+
// Count updates are now handled by readRoomMessages
|
|
442
|
+
const existingDateGroups = prevState.chats[roomId] || [];
|
|
443
|
+
const allDateGroups = [...existingDateGroups, ...newDateGroups];
|
|
444
|
+
|
|
445
|
+
// Group messages by date and de-duplicate messages within each date using their ID (existing logic)
|
|
446
|
+
const messagesGroupedByDate = {};
|
|
447
|
+
allDateGroups.forEach((dateGroup) => {
|
|
448
|
+
if (!messagesGroupedByDate[dateGroup.date]) {
|
|
449
|
+
// Use a Map for efficient de-duplication by message ID
|
|
450
|
+
messagesGroupedByDate[dateGroup.date] = {
|
|
451
|
+
date: dateGroup.date, // Store the date string
|
|
452
|
+
messagesMap: new Map(),
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
dateGroup.messages.forEach((message) => {
|
|
456
|
+
messagesGroupedByDate[dateGroup.date].messagesMap.set(
|
|
457
|
+
message.id,
|
|
458
|
+
message
|
|
459
|
+
);
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
462
|
|
|
463
|
-
|
|
464
|
-
|
|
463
|
+
// Reconstruct the final array of date groups with unique messages
|
|
464
|
+
const finalDateGroups = Object.values(messagesGroupedByDate).map(
|
|
465
|
+
(group) => ({
|
|
466
|
+
date: group.date,
|
|
467
|
+
// Convert Map values back to an array and sort messages by creation time
|
|
468
|
+
messages: Array.from(group.messagesMap.values()).sort(
|
|
469
|
+
(a, b) => new Date(a.created_at) - new Date(b.created_at)
|
|
470
|
+
),
|
|
471
|
+
})
|
|
472
|
+
);
|
|
465
473
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
...prevState,
|
|
469
|
-
// roomsByCourses is NOT updated here anymore
|
|
470
|
-
chats: {
|
|
471
|
-
...prevState.chats,
|
|
472
|
-
// Update the structure: Map roomId directly to the array of finalDateGroups
|
|
473
|
-
[roomId]: finalDateGroups,
|
|
474
|
-
},
|
|
475
|
-
};
|
|
476
|
-
});
|
|
474
|
+
// Optional: Sort the date groups themselves if needed (e.g., chronologically)
|
|
475
|
+
// finalDateGroups.sort((a, b) => /* comparison logic based on date */);
|
|
477
476
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
477
|
+
// --- Return combined state update (only updating chats here) ---
|
|
478
|
+
return {
|
|
479
|
+
...prevState,
|
|
480
|
+
// roomsByCourses is NOT updated here anymore
|
|
481
|
+
chats: {
|
|
482
|
+
...prevState.chats,
|
|
483
|
+
// Update the structure: Map roomId directly to the array of finalDateGroups
|
|
484
|
+
[roomId]: finalDateGroups,
|
|
485
|
+
},
|
|
486
|
+
};
|
|
488
487
|
});
|
|
489
|
-
}
|
|
490
488
|
|
|
491
|
-
|
|
492
|
-
|
|
489
|
+
// Call readRoomMessages only on the first page fetch, if a latest message ID was found and is its not read
|
|
490
|
+
if (
|
|
491
|
+
!nextPage &&
|
|
492
|
+
latestMessageIdFromFetch?.id && // Optional chaining for safety
|
|
493
|
+
!latestMessageIdFromFetch?.is_read // Optional chaining for safety
|
|
494
|
+
) {
|
|
495
|
+
// Pass both message ID and room ID to readRoomMessages
|
|
496
|
+
await readRoomMessages({
|
|
497
|
+
latestMessageId: latestMessageIdFromFetch.id,
|
|
498
|
+
roomId: roomId,
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Only remove from loading state if this is the last page or there was an error
|
|
503
|
+
if (!responseData.next) {
|
|
504
|
+
setState((prevState) => {
|
|
505
|
+
const updatedLoadingRoomIds = new Set(prevState.loadingRoomIds);
|
|
506
|
+
updatedLoadingRoomIds.delete(roomId);
|
|
507
|
+
return {
|
|
508
|
+
...prevState,
|
|
509
|
+
loadingRoomIds: updatedLoadingRoomIds,
|
|
510
|
+
};
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Pass roomId correctly in the recursive call
|
|
515
|
+
if (responseData.next) getIndividualChats(responseData.next, roomId);
|
|
516
|
+
return responseData;
|
|
517
|
+
} catch (error) {
|
|
518
|
+
// Remove from loading state in case of error
|
|
493
519
|
setState((prevState) => {
|
|
494
520
|
const updatedLoadingRoomIds = new Set(prevState.loadingRoomIds);
|
|
495
521
|
updatedLoadingRoomIds.delete(roomId);
|
|
496
522
|
return {
|
|
497
523
|
...prevState,
|
|
498
|
-
loadingRoomIds: updatedLoadingRoomIds
|
|
524
|
+
loadingRoomIds: updatedLoadingRoomIds,
|
|
499
525
|
};
|
|
500
526
|
});
|
|
527
|
+
throw error; // Re-throw to allow error handling upstream
|
|
501
528
|
}
|
|
502
|
-
|
|
503
|
-
// Pass roomId correctly in the recursive call
|
|
504
|
-
if (responseData.next) getIndividualChats(responseData.next, roomId);
|
|
505
|
-
return responseData;
|
|
506
|
-
} catch (error) {
|
|
507
|
-
// Remove from loading state in case of error
|
|
508
|
-
setState((prevState) => {
|
|
509
|
-
const updatedLoadingRoomIds = new Set(prevState.loadingRoomIds);
|
|
510
|
-
updatedLoadingRoomIds.delete(roomId);
|
|
511
|
-
return {
|
|
512
|
-
...prevState,
|
|
513
|
-
loadingRoomIds: updatedLoadingRoomIds
|
|
514
|
-
};
|
|
515
|
-
});
|
|
516
|
-
throw error; // Re-throw to allow error handling upstream
|
|
517
|
-
}
|
|
518
|
-
};
|
|
529
|
+
};
|
|
519
530
|
|
|
520
531
|
// read messages (using the latest message ID passed) and update counts
|
|
521
532
|
const readRoomMessages = async ({ latestMessageId, roomId }) => {
|
|
@@ -529,13 +540,16 @@ const useMessageKit = (/*affiliatesActive*/) => {
|
|
|
529
540
|
if (
|
|
530
541
|
String(selectedAccount.type).toLowerCase() === "instructor" &&
|
|
531
542
|
affiliateAccount
|
|
532
|
-
// &&
|
|
533
|
-
// affiliatesActive
|
|
534
543
|
) {
|
|
535
544
|
await request({
|
|
536
545
|
url: `/notify/v1/instructor/${affiliateAccount}/chats/${latestMessageId}/read_message/`,
|
|
537
546
|
});
|
|
538
547
|
}
|
|
548
|
+
if (String(selectedAccount.type).toLowerCase() === "personal") {
|
|
549
|
+
await request({
|
|
550
|
+
url: `/notify/v1/chats/${latestMessageId}/read_message/`,
|
|
551
|
+
});
|
|
552
|
+
}
|
|
539
553
|
// 2. If API call is successful, update the local state counts
|
|
540
554
|
setState((prevState) => {
|
|
541
555
|
let unreadCountToSubtract = 0;
|
|
@@ -650,6 +664,21 @@ const useMessageKit = (/*affiliatesActive*/) => {
|
|
|
650
664
|
method: "Post",
|
|
651
665
|
});
|
|
652
666
|
}
|
|
667
|
+
if (selectedAccount.type.toLowerCase() === "personal") {
|
|
668
|
+
response = await request({
|
|
669
|
+
url: `/notify/v1/chats/`,
|
|
670
|
+
params: {
|
|
671
|
+
_account: selectedAccount.id,
|
|
672
|
+
course_id: courseId,
|
|
673
|
+
account_id: accountId,
|
|
674
|
+
},
|
|
675
|
+
data: {
|
|
676
|
+
text,
|
|
677
|
+
media,
|
|
678
|
+
},
|
|
679
|
+
method: "Post",
|
|
680
|
+
});
|
|
681
|
+
}
|
|
653
682
|
|
|
654
683
|
if (!response?.data) return; // Added optional chaining for safety
|
|
655
684
|
|
|
@@ -879,6 +908,13 @@ const useMessageKit = (/*affiliatesActive*/) => {
|
|
|
879
908
|
method: "patch",
|
|
880
909
|
});
|
|
881
910
|
} // incomplete
|
|
911
|
+
|
|
912
|
+
if (selectedAccount.type.toLowerCase() === "personal") {
|
|
913
|
+
response = await request({
|
|
914
|
+
url: `/notify/v1/chats/${roomId}/change_pin_status/`,
|
|
915
|
+
method: "patch",
|
|
916
|
+
});
|
|
917
|
+
}
|
|
882
918
|
} catch (err) {
|
|
883
919
|
// Revert optimistic update on failure (optional, depends on desired UX)
|
|
884
920
|
// You might want to implement more specific error handling here
|
|
@@ -952,6 +988,12 @@ const useMessageKit = (/*affiliatesActive*/) => {
|
|
|
952
988
|
method: "delete",
|
|
953
989
|
});
|
|
954
990
|
} // incomplete
|
|
991
|
+
if (selectedAccount.type.toLowerCase() === "personal") {
|
|
992
|
+
response = await request({
|
|
993
|
+
url: `/notify/v1/chats/${roomId}/`,
|
|
994
|
+
method: "delete",
|
|
995
|
+
});
|
|
996
|
+
}
|
|
955
997
|
} catch (err) {
|
|
956
998
|
console.error("Error deleting room:", err);
|
|
957
999
|
// Revert the optimistic update on error
|
|
@@ -989,6 +1031,13 @@ const useMessageKit = (/*affiliatesActive*/) => {
|
|
|
989
1031
|
data,
|
|
990
1032
|
});
|
|
991
1033
|
}
|
|
1034
|
+
if (selectedAccount.type.toLowerCase() === "personal") {
|
|
1035
|
+
response = await request({
|
|
1036
|
+
url: `/notify/v1/chats/reports/`,
|
|
1037
|
+
method: "post",
|
|
1038
|
+
data,
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
992
1041
|
return response;
|
|
993
1042
|
};
|
|
994
1043
|
// Initial data fetch effect
|