1- import React , { useState } from 'react' ;
1+ import React , { useEffect , useRef } from 'react' ;
22import { User } from '@sendbird/chat' ;
33import type {
44 GroupChannel ,
55 GroupChannelCreateParams ,
66} from '@sendbird/chat/groupChannel' ;
77
8- import { getCreateGroupChannel } from '../../../lib/selectors' ;
98import useSendbirdStateContext from '../../../hooks/useSendbirdStateContext' ;
109import { CHANNEL_TYPE } from '../types' ;
1110import { SendbirdChatType } from '../../../lib/types' ;
12-
13- const CreateChannelContext = React . createContext < CreateChannelContextInterface | null > ( null ) ;
11+ import { createStore } from '../../../utils/storeManager' ;
12+ import { useStore } from '../../../hooks/useStore' ;
13+
14+ const CreateChannelContext = React . createContext < ReturnType < typeof createStore < CreateChannelState > > | null > ( null ) ;
15+
16+ const initialState = {
17+ sdk : undefined ,
18+ createChannel : undefined ,
19+ userListQuery : undefined ,
20+ onCreateChannelClick : undefined ,
21+ onChannelCreated : undefined ,
22+ onBeforeCreateChannel : undefined ,
23+ step : 0 ,
24+ setStep : ( ) => { } ,
25+ type : CHANNEL_TYPE . GROUP ,
26+ setType : ( ) => { } ,
27+ onCreateChannel : undefined ,
28+ overrideInviteUser : undefined ,
29+ } ;
1430
1531export interface UserListQuery {
1632 hasNext ?: boolean ;
@@ -56,7 +72,7 @@ export interface CreateChannelProviderProps {
5672
5773type CreateChannel = ( channelParams : GroupChannelCreateParams ) => Promise < GroupChannel > ;
5874
59- export interface CreateChannelContextInterface {
75+ export interface CreateChannelState {
6076 sdk : SendbirdChatType ;
6177 createChannel : CreateChannel ;
6278 userListQuery ?( ) : UserListQuery ;
@@ -76,9 +92,7 @@ export interface CreateChannelContextInterface {
7692 onBeforeCreateChannel ?( users : Array < string > ) : GroupChannelCreateParams ;
7793
7894 step : number ,
79- setStep : React . Dispatch < React . SetStateAction < number > > ,
8095 type : CHANNEL_TYPE ,
81- setType : React . Dispatch < React . SetStateAction < CHANNEL_TYPE > > ,
8296 /**
8397 * @deprecated
8498 * Use the onChannelCreated instead
@@ -91,9 +105,8 @@ export interface CreateChannelContextInterface {
91105 overrideInviteUser ?( params : OverrideInviteUserType ) : void ;
92106}
93107
94- const CreateChannelProvider : React . FC < CreateChannelProviderProps > = ( props : CreateChannelProviderProps ) => {
108+ const CreateChannelManager : React . FC < CreateChannelProviderProps > = ( props : CreateChannelProviderProps ) => {
95109 const {
96- children,
97110 onCreateChannelClick,
98111 onBeforeCreateChannel,
99112 onChannelCreated,
@@ -102,32 +115,57 @@ const CreateChannelProvider: React.FC<CreateChannelProviderProps> = (props: Crea
102115 overrideInviteUser,
103116 } = props ;
104117
118+ const { updateState } = useCreateChannelStore ( ) ;
105119 const store = useSendbirdStateContext ( ) ;
106120 const _userListQuery = userListQuery ?? store ?. config ?. userListQuery ;
107121
108- const [ step , setStep ] = useState ( 0 ) ;
109- const [ type , setType ] = useState ( CHANNEL_TYPE . GROUP ) ;
110-
111- return (
112- < CreateChannelContext . Provider value = { {
113- sdk : store . stores . sdkStore . sdk ,
114- createChannel : getCreateGroupChannel ( store ) ,
122+ useEffect ( ( ) => {
123+ updateState ( {
115124 onCreateChannelClick,
116125 onBeforeCreateChannel,
117126 onChannelCreated,
118127 userListQuery : _userListQuery ,
119- step,
120- setStep,
121- type,
122- setType,
123128 onCreateChannel,
124129 overrideInviteUser,
125- } } >
130+ } ) ;
131+ } , [
132+ onCreateChannelClick ,
133+ onBeforeCreateChannel ,
134+ onChannelCreated ,
135+ userListQuery ,
136+ onCreateChannel ,
137+ overrideInviteUser ,
138+ _userListQuery ,
139+ ] ) ;
140+
141+ return null ;
142+ } ;
143+ const CreateChannelProvider : React . FC < CreateChannelProviderProps > = ( props : CreateChannelProviderProps ) => {
144+ const { children } = props ;
145+
146+ return (
147+ < InternalCreateChannelProvider >
148+ < CreateChannelManager { ...props } />
149+ { children }
150+ </ InternalCreateChannelProvider >
151+ ) ;
152+ } ;
153+
154+ const createCreateChannelStore = ( ) => createStore ( initialState ) ;
155+ const InternalCreateChannelProvider : React . FC < React . PropsWithChildren < unknown > > = ( { children } ) => {
156+ const storeRef = useRef ( createCreateChannelStore ( ) ) ;
157+
158+ return (
159+ < CreateChannelContext . Provider value = { storeRef . current } >
126160 { children }
127161 </ CreateChannelContext . Provider >
128162 ) ;
129163} ;
130164
165+ const useCreateChannelStore = ( ) => {
166+ return useStore ( CreateChannelContext , state => state , initialState ) ;
167+ } ;
168+
131169const useCreateChannelContext = ( ) => {
132170 const context = React . useContext ( CreateChannelContext ) ;
133171 if ( ! context ) throw new Error ( 'CreateChannelContext not found. Use within the CreateChannel module.' ) ;
0 commit comments