11import React , { ReactNode , useEffect , useState } from 'react' ;
2- import { GroupChannel , GroupChannelUpdateParams } from '@sendbird/chat/groupChannel' ;
2+ import { GroupChannel , GroupChannelHandler , GroupChannelUpdateParams } from '@sendbird/chat/groupChannel' ;
33
44import type { UserListItemProps } from '../../../ui/UserListItem' ;
55import type { RenderUserProfileProps } from '../../../types' ;
66import useSendbirdStateContext from '../../../hooks/useSendbirdStateContext' ;
77import { UserProfileProvider } from '../../../lib/UserProfileContext' ;
88import uuidv4 from '../../../utils/uuid' ;
99import { useAsyncRequest } from '../../../hooks/useAsyncRequest' ;
10+ import compareIds from '../../../utils/compareIds' ;
1011
1112interface ApplicationUserListQuery {
1213 limit ?: number ;
@@ -68,6 +69,7 @@ const ChannelSettingsProvider = ({
6869 const { config, stores } = useSendbirdStateContext ( ) ;
6970 const { sdkStore } = stores ;
7071 const { logger, onUserProfileMessage } = config ;
72+ const [ channelHandlerId , setChannelHandlerId ] = useState < string > ( ) ;
7173
7274 // hack to keep track of channel updates by triggering useEffect
7375 const [ channelUpdateId , setChannelUpdateId ] = useState ( ( ) => uuidv4 ( ) ) ;
@@ -94,7 +96,39 @@ const ChannelSettingsProvider = ({
9496 }
9597
9698 try {
97- return await sdkStore . sdk . groupChannel . getChannel ( channelUrl ) ;
99+ if ( channelHandlerId ) {
100+ if ( sdkStore . sdk ?. groupChannel ?. removeGroupChannelHandler ) {
101+ logger . info ( 'ChannelSettings: Removing message reciver handler' , channelHandlerId ) ;
102+ sdkStore . sdk . groupChannel . removeGroupChannelHandler ( channelHandlerId ) ;
103+ } else if ( sdkStore . sdk ?. groupChannel ) {
104+ logger . error ( 'ChannelSettings: Not found the removeGroupChannelHandler' ) ;
105+ }
106+
107+ setChannelHandlerId ( undefined ) ;
108+ }
109+
110+ // FIXME :: refactor below code by new state management protocol
111+ const channel = await sdkStore . sdk . groupChannel . getChannel ( channelUrl ) ;
112+ const channelHandler : GroupChannelHandler = {
113+ onUserLeft : ( channel , user ) => {
114+ if ( compareIds ( channel ?. url , channelUrl ) ) {
115+ logger . info ( 'ChannelSettings: onUserLeft' , { channel, user } ) ;
116+ refresh ( ) ;
117+ }
118+ } ,
119+ onUserBanned : ( channel , user ) => {
120+ if ( compareIds ( channel ?. url , channelUrl ) && channel . isGroupChannel ( ) ) {
121+ logger . info ( 'ChannelSettings: onUserBanned' , { channel, user } ) ;
122+ refresh ( ) ;
123+ }
124+ } ,
125+ } ;
126+
127+ const newChannelHandlerId = uuidv4 ( ) ;
128+ sdkStore . sdk . groupChannel ?. addGroupChannelHandler ( newChannelHandlerId , new GroupChannelHandler ( channelHandler ) ) ;
129+ setChannelHandlerId ( newChannelHandlerId ) ;
130+
131+ return channel ;
98132 } catch ( error ) {
99133 logger . error ( 'ChannelSettings: fetching channel error:' , error ) ;
100134 throw error ;
0 commit comments