1- import React , { useContext , useEffect , useState } from 'react' ;
1+ import React , { useContext , useEffect , useRef , useState } from 'react' ;
22
33import type { User } from '@sendbird/chat' ;
44import type { GroupChannel , GroupChannelCreateParams , GroupChannelFilterParams } from '@sendbird/chat/groupChannel' ;
@@ -14,6 +14,8 @@ import useOnlineStatus from '../../../lib/hooks/useOnlineStatus';
1414import { noop } from '../../../utils/utils' ;
1515import type { SdkStore } from '../../../lib/types' ;
1616import { PartialRequired } from '../../../utils/typeHelpers/partialRequired' ;
17+ import { createStore } from '../../../utils/storeManager' ;
18+ import { useStore } from '../../../hooks/useStore' ;
1719
1820type OnCreateChannelClickParams = { users : Array < string > ; onClose : ( ) => void ; channelType : CHANNEL_TYPE } ;
1921type ChannelListDataSource = ReturnType < typeof useGroupChannelList > ;
@@ -52,10 +54,41 @@ export interface GroupChannelListProviderProps extends
5254 children ?: React . ReactNode ;
5355}
5456
55- export const GroupChannelListContext = React . createContext < GroupChannelListContextType | null > ( null ) ;
56- export const GroupChannelListProvider = ( props : GroupChannelListProviderProps ) => {
57+ export const GroupChannelListContext = React . createContext < ReturnType < typeof createStore < GroupChannelListState > > | null > ( null ) ;
58+
59+ export interface GroupChannelListState extends GroupChannelListContextType {
60+ }
61+
62+ const initialState : GroupChannelListState = {
63+ className : '' ,
64+ selectedChannelUrl : '' ,
65+ disableAutoSelect : false ,
66+ allowProfileEdit : false ,
67+ isTypingIndicatorEnabled : false ,
68+ isMessageReceiptStatusEnabled : false ,
69+ onChannelSelect : noop ,
70+ onChannelCreated : noop ,
71+ onThemeChange : noop ,
72+ onCreateChannelClick : noop ,
73+ onBeforeCreateChannel : null ,
74+ onUserProfileUpdated : noop ,
75+ typingChannelUrls : [ ] ,
76+ refreshing : false ,
77+ initialized : false ,
78+ groupChannels : [ ] ,
79+ refresh : null ,
80+ loadMore : null ,
81+ } ;
82+
83+ /**
84+ * @returns {ReturnType<typeof createStore<GroupChannelListState>> }
85+ */
86+ const useGroupChannelListStore = ( ) => {
87+ return useStore ( GroupChannelListContext , state => state , initialState ) ;
88+ } ;
89+
90+ export const GroupChannelListManager : React . FC < GroupChannelListProviderProps > = ( props : GroupChannelListProviderProps ) => {
5791 const {
58- children,
5992 className = '' ,
6093 selectedChannelUrl,
6194
@@ -72,8 +105,9 @@ export const GroupChannelListProvider = (props: GroupChannelListProviderProps) =
72105 onBeforeCreateChannel,
73106 onUserProfileUpdated,
74107 } = props ;
75- const globalStore = useSendbirdStateContext ( ) ;
76- const { config, stores } = globalStore ;
108+
109+ const { config, stores } = useSendbirdStateContext ( ) ;
110+ const { updateState } = useGroupChannelListStore ( ) ;
77111 const { sdkStore } = stores ;
78112
79113 const sdk = sdkStore . sdk ;
@@ -120,33 +154,71 @@ export const GroupChannelListProvider = (props: GroupChannelListProviderProps) =
120154 } ,
121155 } ) ;
122156
157+ useEffect ( ( ) => {
158+ updateState ( {
159+ className,
160+ selectedChannelUrl,
161+ disableAutoSelect,
162+ allowProfileEdit,
163+ isTypingIndicatorEnabled,
164+ isMessageReceiptStatusEnabled,
165+ onChannelSelect,
166+ onChannelCreated,
167+ onThemeChange,
168+ onCreateChannelClick,
169+ onBeforeCreateChannel,
170+ onUserProfileUpdated,
171+ typingChannelUrls,
172+ refreshing,
173+ initialized,
174+ groupChannels,
175+ refresh,
176+ loadMore,
177+ } ) ;
178+ } , [
179+ className ,
180+ selectedChannelUrl ,
181+ disableAutoSelect ,
182+ allowProfileEdit ,
183+ isTypingIndicatorEnabled ,
184+ isMessageReceiptStatusEnabled ,
185+ onChannelSelect ,
186+ onChannelCreated ,
187+ onThemeChange ,
188+ onCreateChannelClick ,
189+ onBeforeCreateChannel ,
190+ onUserProfileUpdated ,
191+ typingChannelUrls ,
192+ refreshing ,
193+ initialized ,
194+ groupChannels ,
195+ refresh ,
196+ loadMore ,
197+ ] ) ;
198+
199+ return null ;
200+ } ;
201+
202+ const createGroupChannelListStore = ( ) => createStore ( initialState ) ;
203+ const InternalGroupChannelListStoreProvider = ( { children } ) => {
204+ const storeRef = useRef ( createGroupChannelListStore ( ) ) ;
123205 return (
124- < GroupChannelListContext . Provider
125- value = { {
126- className,
127- selectedChannelUrl,
128- disableAutoSelect,
129- allowProfileEdit : allowProfileEdit ?? config . allowProfileEdit ?? true ,
130- isTypingIndicatorEnabled : isTypingIndicatorEnabled ?? config . groupChannelList . enableTypingIndicator ?? false ,
131- isMessageReceiptStatusEnabled : isMessageReceiptStatusEnabled ?? config . groupChannelList . enableMessageReceiptStatus ?? false ,
132- onChannelSelect,
133- onChannelCreated,
134- onThemeChange,
135- onCreateChannelClick,
136- onBeforeCreateChannel,
137- onUserProfileUpdated,
138- typingChannelUrls,
139- refreshing,
140- initialized,
141- groupChannels,
142- refresh,
143- loadMore,
144- } }
145- >
206+ < GroupChannelListContext . Provider value = { storeRef . current } >
207+ { children }
208+ </ GroupChannelListContext . Provider >
209+ ) ;
210+ } ;
211+
212+ export const GroupChannelListProvider = ( props : GroupChannelListProviderProps ) => {
213+ const { children, className } = props ;
214+
215+ return (
216+ < InternalGroupChannelListStoreProvider >
217+ < GroupChannelListManager { ...props } />
146218 < UserProfileProvider { ...props } >
147219 < div className = { `sendbird-channel-list ${ className } ` } > { children } </ div >
148220 </ UserProfileProvider >
149- </ GroupChannelListContext . Provider >
221+ </ InternalGroupChannelListStoreProvider >
150222 ) ;
151223} ;
152224
0 commit comments