|
1 | | -import { useCallback, useContext, useEffect, useRef, useState } from "react"; |
| 1 | +import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"; |
2 | 2 | import blockIcon from "../../assets/block.svg"; |
3 | 3 | import deleteIcon from "../../assets/delete.svg"; |
4 | 4 | import { COMETCHAT_CONSTANTS } from "../../AppConstants"; |
@@ -480,6 +480,69 @@ function CometChatHome(props: { theme?: string }) { |
480 | 480 | } |
481 | 481 | }, [newChat]); |
482 | 482 |
|
| 483 | + const updateGroupDetails = (eventGroup: CometChat.Group) => { |
| 484 | + if (eventGroup.getGuid() === group?.getGuid()) { |
| 485 | + setGroup(eventGroup); |
| 486 | + } |
| 487 | + } |
| 488 | + |
| 489 | + const attachSDKGroupListenerForDetails = () => { |
| 490 | + const listenerId = "GroupDetailsListener_" + String(Date.now()); |
| 491 | + CometChat.addGroupListener( |
| 492 | + listenerId, |
| 493 | + new CometChat.GroupListener({ |
| 494 | + onGroupMemberBanned: ( |
| 495 | + message: CometChat.Action, |
| 496 | + bannedUser: CometChat.User, |
| 497 | + bannedBy: CometChat.User, |
| 498 | + bannedFrom: CometChat.Group |
| 499 | + ) => { |
| 500 | + updateGroupDetails(bannedFrom); |
| 501 | + }, |
| 502 | + onGroupMemberKicked: ( |
| 503 | + message: CometChat.Action, |
| 504 | + kickedUser: CometChat.User, |
| 505 | + kickedBy: CometChat.User, |
| 506 | + kickedFrom: CometChat.Group |
| 507 | + ) => { |
| 508 | + updateGroupDetails(kickedFrom); |
| 509 | + }, |
| 510 | + onMemberAddedToGroup: ( |
| 511 | + message: CometChat.Action, |
| 512 | + userAdded: CometChat.User, |
| 513 | + userAddedBy: CometChat.User, |
| 514 | + userAddedIn: CometChat.Group |
| 515 | + ) => { |
| 516 | + updateGroupDetails(userAddedIn); |
| 517 | + }, |
| 518 | + onGroupMemberJoined: ( |
| 519 | + message: CometChat.Action, |
| 520 | + joinedUser: CometChat.User, |
| 521 | + joinedGroup: CometChat.Group |
| 522 | + ) => { |
| 523 | + updateGroupDetails(joinedGroup); |
| 524 | + }, |
| 525 | + onGroupMemberLeft: ( |
| 526 | + message: CometChat.Action, |
| 527 | + leavingUser: CometChat.User, |
| 528 | + group: CometChat.Group |
| 529 | + ) => { |
| 530 | + updateGroupDetails(group); |
| 531 | + }, |
| 532 | + }) |
| 533 | + ); |
| 534 | + return () => CometChat.removeGroupListener(listenerId); |
| 535 | + } |
| 536 | + |
| 537 | + useEffect(() => { |
| 538 | + if (loggedInUser) { |
| 539 | + const unsubscribeFromGroupEvents = attachSDKGroupListenerForDetails(); |
| 540 | + return () => { |
| 541 | + unsubscribeFromGroupEvents(); |
| 542 | + }; |
| 543 | + } |
| 544 | + }, [loggedInUser, attachSDKGroupListenerForDetails]); |
| 545 | + |
483 | 546 | return ( |
484 | 547 | <> |
485 | 548 | {appState.sideComponent.visible && ( |
@@ -1245,6 +1308,12 @@ function CometChatHome(props: { theme?: string }) { |
1245 | 1308 | } |
1246 | 1309 | } |
1247 | 1310 |
|
| 1311 | + const SideComponentWrapper = useMemo(() => { |
| 1312 | + return ( |
| 1313 | + <SideComponent /> |
| 1314 | + ) |
| 1315 | + }, [appState.sideComponent]); |
| 1316 | + |
1248 | 1317 | return ( |
1249 | 1318 | loggedInUser && <div className='cometchat-root' data-theme={theme}> |
1250 | 1319 | {showAlertPopup.visible && |
@@ -1278,7 +1347,7 @@ function CometChatHome(props: { theme?: string }) { |
1278 | 1347 | <div className='messages-wrapper'> |
1279 | 1348 | <InformationComponent /> |
1280 | 1349 | </div> |
1281 | | - <SideComponent /> |
| 1350 | + {SideComponentWrapper} |
1282 | 1351 | <CometChatIncomingCall /> |
1283 | 1352 | {showToast ? <CometChatToast text={toastTextRef.current} onClose={closeToast} /> : null} |
1284 | 1353 | </div> |
|
0 commit comments