Skip to content

Commit d65d16f

Browse files
authored
fix: Stay in OpenChannel when it's properties change (#284)
* Do not exit the current channel in every time when the currentOpenChannel is changed * Exit from the previous open channel when fetching a new open channel
1 parent 050be89 commit d65d16f

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

src/smart-components/OpenChannel/context/OpenChannelProvider.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const OpenChannelProvider: React.FC<OpenChannelProviderProps> = (props: OpenChan
154154

155155
// use hooks
156156
useSetChannel(
157-
{ channelUrl, sdkInit, fetchingParticipants, userId },
157+
{ channelUrl, sdkInit, fetchingParticipants, userId, currentOpenChannel },
158158
{ sdk, logger, messagesDispatcher },
159159
);
160160

@@ -270,16 +270,6 @@ const OpenChannelProvider: React.FC<OpenChannelProviderProps> = (props: OpenChan
270270
};
271271
}, [channelUrl, sdkInit]);
272272

273-
// Exit channel when unmount
274-
useEffect(() => () => {
275-
if (currentOpenChannel && currentOpenChannel.exit) {
276-
currentOpenChannel.exit()
277-
.then(() => {
278-
logger.info('OpenChannel | useSetChannel: Succeeded to exit channel');
279-
});
280-
}
281-
}, [currentOpenChannel]);
282-
283273
return (
284274
<OpenChannelContext.Provider value={{
285275
// props

src/smart-components/OpenChannel/context/dux/actionTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const SET_CURRENT_CHANNEL = 'SET_CURRENT_CHANNEL';
22
export const SET_CHANNEL_INVALID = 'SET_CHANNEL_INVALID';
33
export const RESET_MESSAGES = 'RESET_MESSAGES';
4+
export const EXIT_CURRENT_CHANNEL = 'EXIT_CURRENT_CHANNEL';
45
export const GET_PREV_MESSAGES_START = 'GET_PREV_MESSAGES_START';
56
export const GET_PREV_MESSAGES_SUCESS = 'GET_PREV_MESSAGES_SUCESS';
67
export const GET_PREV_MESSAGES_FAIL = 'GET_PREV_MESSAGES_FAIL';

src/smart-components/OpenChannel/context/dux/reducers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ export default function reducer(
2222
allMessages: [],
2323
};
2424
}
25-
25+
case actionTypes.EXIT_CURRENT_CHANNEL: {
26+
if (action.payload?.url === state.currentOpenChannel?.url) {
27+
return {
28+
...state,
29+
currentOpenChannel: null,
30+
};
31+
}
32+
return state;
33+
}
2634
case actionTypes.SET_CURRENT_CHANNEL: {
2735
const gottenChannel = action.payload;
2836
const operators = gottenChannel.operators;

src/smart-components/OpenChannel/context/hooks/useSetChannel.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SendbirdOpenChat } from '@sendbird/chat/openChannel';
1+
import type { OpenChannel, SendbirdOpenChat } from '@sendbird/chat/openChannel';
22
import { useEffect } from 'react';
33
import type { Logger } from '../../../../lib/SendbirdState';
44
import * as messageActionTypes from '../dux/actionTypes';
@@ -9,22 +9,32 @@ interface DynamicParams {
99
sdkInit: boolean;
1010
fetchingParticipants: boolean;
1111
userId: string;
12+
currentOpenChannel: OpenChannel;
1213
}
1314
interface StaticParams {
1415
sdk: SendbirdOpenChat;
1516
logger: Logger;
16-
messagesDispatcher: ({ type: string, payload: any }) => void;
17+
messagesDispatcher: ({ type, payload }: { type: string, payload: any }) => void;
1718
}
1819

1920
function useSetChannel(
20-
{ channelUrl, sdkInit, fetchingParticipants, userId }: DynamicParams,
21+
{ channelUrl, sdkInit, fetchingParticipants, userId, currentOpenChannel }: DynamicParams,
2122
{ sdk, logger, messagesDispatcher }: StaticParams,
2223
): void {
2324
useEffect(() => {
2425
if (channelUrl && sdkInit && sdk?.openChannel) {
25-
logger.info('OpenChannel | useSetChannel fetching channel', channelUrl);
26+
if (currentOpenChannel && currentOpenChannel?.exit) {
27+
currentOpenChannel.exit?.().then(() => {
28+
logger.info('OpenChannel | useSetChannel: Exit from the previous open channel', currentOpenChannel?.url);
29+
messagesDispatcher({
30+
type: messageActionTypes.EXIT_CURRENT_CHANNEL,
31+
payload: currentOpenChannel,
32+
});
33+
});
34+
}
35+
logger.info('OpenChannel | useSetChannel: Fetching channel', channelUrl);
2636
sdk.openChannel.getChannel(channelUrl).then((openChannel) => {
27-
logger.info('OpenChannel | useSetChannel fetched channel', openChannel);
37+
logger.info('OpenChannel | useSetChannel: Succeeded to fetch channel', openChannel);
2838
messagesDispatcher({
2939
type: messageActionTypes.SET_CURRENT_CHANNEL,
3040
payload: openChannel,
@@ -93,14 +103,14 @@ function useSetChannel(
93103
);
94104
}
95105
}).catch((error) => {
96-
logger.warning('OpenChannel | useSetChannel enter channel failed', { channelUrl, error });
106+
logger.warning('OpenChannel | useSetChannel: Failed to enter channel', { channelUrl, error });
97107
messagesDispatcher({
98108
type: messageActionTypes.SET_CHANNEL_INVALID,
99109
payload: null,
100110
});
101111
});
102112
}).catch((error) => {
103-
logger.warning('OpenChannel | useSetChannel fetching channel failed', { channelUrl, error });
113+
logger.warning('OpenChannel | useSetChannel: Failed to fetch channel', { channelUrl, error });
104114
messagesDispatcher({
105115
type: messageActionTypes.SET_CHANNEL_INVALID,
106116
payload: null,

src/smart-components/OpenChannelSettings/context/OpenChannelSettingsProvider.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const OpenChannelSettingsProvider: React.FC<OpenChannelSettingsContextProps> = (
4545
// fetch store from <SendbirdProvider />
4646
const globalStore = useSendbirdStateContext();
4747
const sdk = globalStore?.stores?.sdkStore?.sdk as SendbirdOpenChat;
48+
const isSDKInitialized = globalStore?.stores?.sdkStore?.initialized;
4849

4950
const logger = globalStore?.config?.logger;
5051
const currentUserId = sdk?.currentUser?.userId;
@@ -63,7 +64,7 @@ const OpenChannelSettingsProvider: React.FC<OpenChannelSettingsContextProps> = (
6364
channel.enter()
6465
.then(() => {
6566
setChannel(channel);
66-
logger.info('OpenChannelSettings | Succeeded to enter channel');
67+
logger.info('OpenChannelSettings | Succeeded to enter channel', channel?.url);
6768
})
6869
.catch((error) => {
6970
setChannel(null);
@@ -78,14 +79,14 @@ const OpenChannelSettingsProvider: React.FC<OpenChannelSettingsContextProps> = (
7879
if (currentChannel && currentChannel.exit) {
7980
currentChannel.exit()
8081
.then(() => {
81-
logger.info('OpenChannelSettings | Succeeded to exit channel');
82+
logger.info('OpenChannelSettings | Succeeded to exit channel', currentChannel?.url);
8283
})
8384
.catch((error) => {
8485
logger.warning('OpenChannelSettings | Failed to exit channel', error);
8586
});
8687
}
8788
}
88-
}, [channelUrl, sdk]);
89+
}, [channelUrl, isSDKInitialized]);
8990

9091
useEffect(() => {
9192
const channelHandlerId = uuidv4();
@@ -135,7 +136,7 @@ const OpenChannelSettingsProvider: React.FC<OpenChannelSettingsContextProps> = (
135136
sdk.openChannel.removeOpenChannelHandler?.(channelHandlerId);
136137
}
137138
}
138-
}, [currentChannel]);
139+
}, [channelUrl]);
139140

140141
return (
141142
<OpenChannelSettingsContext.Provider value={{

0 commit comments

Comments
 (0)