1- import React , { useContext , useEffect , useRef } from 'react' ;
1+ import React , { useEffect , useMemo , useRef } from 'react' ;
22
33import type { User } from '@sendbird/chat' ;
44import type { GroupChannel , GroupChannelCreateParams , GroupChannelFilterParams } from '@sendbird/chat/groupChannel' ;
55import { GroupChannelCollectionParams , GroupChannelFilter } from '@sendbird/chat/groupChannel' ;
6- import { useGroupChannelList , useGroupChannelHandler } from '@sendbird/uikit-tools' ;
6+ import { useGroupChannelList as useGroupChannelListDataSource , useGroupChannelHandler } from '@sendbird/uikit-tools' ;
77
88import type { CHANNEL_TYPE } from '../../CreateChannel/types' ;
99import { UserProfileProvider } from '../../../lib/UserProfileContext' ;
@@ -16,9 +16,10 @@ import { createStore } from '../../../utils/storeManager';
1616import { useStore } from '../../../hooks/useStore' ;
1717import { noop } from '../../../utils/utils' ;
1818import useSendbird from '../../../lib/Sendbird/context/hooks/useSendbird' ;
19+ import useGroupChannelList from './useGroupChannelList' ;
1920
2021type OnCreateChannelClickParams = { users : Array < string > ; onClose : ( ) => void ; channelType : CHANNEL_TYPE } ;
21- type ChannelListDataSource = ReturnType < typeof useGroupChannelList > ;
22+ type ChannelListDataSource = ReturnType < typeof useGroupChannelListDataSource > ;
2223export type ChannelListQueryParamsType = Omit < GroupChannelCollectionParams , 'filter' > & GroupChannelFilterParams ;
2324
2425interface ContextBaseType {
@@ -83,7 +84,7 @@ const initialState: GroupChannelListState = {
8384/**
8485 * @returns {ReturnType<typeof createStore<GroupChannelListState>> }
8586 */
86- const useGroupChannelListStore = ( ) => {
87+ export const useGroupChannelListStore = ( ) => {
8788 return useStore ( GroupChannelListContext , state => state , initialState ) ;
8889} ;
8990
@@ -92,9 +93,6 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
9293 selectedChannelUrl = '' ,
9394
9495 disableAutoSelect = false ,
95- allowProfileEdit,
96- isTypingIndicatorEnabled,
97- isMessageReceiptStatusEnabled,
9896
9997 channelListQueryParams,
10098 onThemeChange,
@@ -103,17 +101,19 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
103101 onCreateChannelClick,
104102 onBeforeCreateChannel,
105103 onUserProfileUpdated,
104+ ...props
106105} : GroupChannelListProviderProps ) => {
107106 const { state : sendbirdState } = useSendbird ( ) ;
108107 const { config, stores } = sendbirdState ;
109- const { state, updateState } = useGroupChannelListStore ( ) ;
108+ const { state } = useGroupChannelList ( ) ;
109+ const { updateState } = useGroupChannelListStore ( ) ;
110110 const { sdkStore } = stores ;
111111
112112 const sdk = sdkStore . sdk ;
113113 const isConnected = useOnlineStatus ( sdk , config . logger ) ;
114114 const scheduler = useMarkAsDeliveredScheduler ( { isConnected } , config ) ;
115115
116- const channelListDataSource = useGroupChannelList ( sdk , {
116+ const channelListDataSource = useGroupChannelListDataSource ( sdk , {
117117 collectionCreator : getCollectionCreator ( sdk , channelListQueryParams ) ,
118118 markAsDelivered : ( channels ) => channels . forEach ( scheduler . push ) ,
119119 onChannelsDeleted : ( channelUrls ) => {
@@ -142,6 +142,7 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
142142 ] ) ;
143143
144144 const { typingChannelUrls } = state ;
145+
145146 useGroupChannelHandler ( sdk , {
146147 onTypingStatusUpdated : ( channel ) => {
147148 const channelList = typingChannelUrls . filter ( ( channelUrl ) => channelUrl !== channel . url ) ;
@@ -157,47 +158,62 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
157158 } ,
158159 } ) ;
159160
160- useEffect ( ( ) => {
161- updateState ( {
162- className,
163- selectedChannelUrl,
164- disableAutoSelect,
165- allowProfileEdit,
166- isTypingIndicatorEnabled,
167- isMessageReceiptStatusEnabled,
168- onChannelSelect,
169- onChannelCreated,
170- onThemeChange,
171- onCreateChannelClick,
172- onBeforeCreateChannel,
173- onUserProfileUpdated,
174- typingChannelUrls,
175- refreshing,
176- initialized,
177- groupChannels,
178- refresh,
179- loadMore,
180- } ) ;
181- } , [
182- className ,
183- selectedChannelUrl ,
184- disableAutoSelect ,
185- allowProfileEdit ,
186- isTypingIndicatorEnabled ,
187- isMessageReceiptStatusEnabled ,
161+ const allowProfileEdit = props . allowProfileEdit ?? config . allowProfileEdit ?? true ;
162+ const isTypingIndicatorEnabled = props . isTypingIndicatorEnabled ?? config . groupChannelList . enableTypingIndicator ?? false ;
163+ const isMessageReceiptStatusEnabled = props . isMessageReceiptStatusEnabled ?? config . groupChannelList . enableMessageReceiptStatus ?? false ;
164+
165+ const eventHandlers = useMemo ( ( ) => ( {
166+ onChannelSelect,
167+ onChannelCreated,
168+ onThemeChange,
169+ onCreateChannelClick,
170+ onBeforeCreateChannel,
171+ onUserProfileUpdated,
172+ } ) , [
188173 onChannelSelect ,
189174 onChannelCreated ,
190175 onThemeChange ,
191176 onCreateChannelClick ,
192177 onBeforeCreateChannel ,
193178 onUserProfileUpdated ,
179+ ] ) ;
180+ const configurations = useMemo ( ( ) => ( {
181+ className,
182+ selectedChannelUrl,
183+ disableAutoSelect,
184+ allowProfileEdit,
185+ isTypingIndicatorEnabled,
186+ isMessageReceiptStatusEnabled,
187+ typingChannelUrls,
188+ refreshing,
189+ initialized,
190+ refresh,
191+ loadMore,
192+ } ) , [
193+ className ,
194+ selectedChannelUrl ,
195+ disableAutoSelect ,
196+ allowProfileEdit ,
197+ isTypingIndicatorEnabled ,
198+ isMessageReceiptStatusEnabled ,
194199 typingChannelUrls ,
195200 refreshing ,
196201 initialized ,
197- groupChannels . map ( channel => channel . url ) . join ( ) ,
198202 refresh ,
199203 loadMore ,
200204 ] ) ;
205+ const memoizedGroupChannel = useMemo ( ( ) => groupChannels , [ groupChannels ] ) ;
206+ useEffect ( ( ) => {
207+ updateState ( {
208+ groupChannels : memoizedGroupChannel ,
209+ ...eventHandlers ,
210+ ...configurations ,
211+ } ) ;
212+ } , [
213+ configurations ,
214+ eventHandlers ,
215+ memoizedGroupChannel ,
216+ ] ) ;
201217
202218 return null ;
203219} ;
@@ -225,10 +241,10 @@ export const GroupChannelListProvider = (props: GroupChannelListProviderProps) =
225241 ) ;
226242} ;
227243
244+ // Keep this function for backward compatibility.
228245export const useGroupChannelListContext = ( ) => {
229- const context = useContext ( GroupChannelListContext ) ;
230- if ( ! context ) throw new Error ( 'GroupChannelListContext not found. Use within the GroupChannelList module.' ) ;
231- return context ;
246+ const { state, actions } = useGroupChannelList ( ) ;
247+ return { ...state , ...actions } ;
232248} ;
233249
234250function getCollectionCreator ( sdk : SdkStore [ 'sdk' ] , channelListQueryParams ?: ChannelListQueryParamsType ) {
0 commit comments