Skip to content

Commit d714b61

Browse files
authored
[UK-983] Channel instance check before reconnect (#37)
* Add a condition to check if current group channel exists before reconnecting * Change interfaces naming to Dynamic and Static Params in the hook functions
1 parent df109b5 commit d714b61

File tree

13 files changed

+80
-62
lines changed

13 files changed

+80
-62
lines changed

src/legacy/message-refactor/FileMessage/stories/FileMessage.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { OutgoingFileMessage, IncomingFileMessage } from '../index.jsx';
33
import { dummyFileMessageImage, dummyFileMessageAudio } from '../dummyFileMessage.mock';
44

5-
import { MessageStatusTypes } from '../../../../utils';
5+
import { MessageStatusTypes } from '../../../../ui/MessageStatus';
66

77
export default { title: 'Legacy/FileMessage' };
88

src/smart-components/Conversation/hooks/useHandleReconnect.js renamed to src/smart-components/Conversation/hooks/useHandleReconnect.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@ import { useEffect } from 'react';
22

33
import * as utils from '../utils';
44
import * as messageActionTypes from '../dux/actionTypes';
5+
import { Logger, Sdk } from '../../..';
6+
import { GroupChannel } from 'sendbird';
57

6-
function useHandleReconnect({ isOnline }, {
7-
logger,
8-
sdk,
9-
currentGroupChannel,
10-
messagesDispatcher,
11-
userFilledMessageListQuery,
12-
}) {
8+
interface DynamicParams {
9+
isOnline: boolean;
10+
}
11+
12+
interface StaticParams {
13+
logger: Logger;
14+
sdk: Sdk;
15+
currentGroupChannel: GroupChannel;
16+
messagesDispatcher: ({ type: string, payload: any }) => void;
17+
userFilledMessageListQuery?: Record<string, any>;
18+
}
19+
20+
function useHandleReconnect(
21+
{ isOnline }: DynamicParams,
22+
{
23+
logger,
24+
sdk,
25+
currentGroupChannel,
26+
messagesDispatcher,
27+
userFilledMessageListQuery,
28+
}: StaticParams,
29+
): void {
1330
useEffect(() => {
1431
const wasOffline = !isOnline;
1532
return () => {
1633
// state changed from offline to online
17-
if (wasOffline) {
34+
if (wasOffline && currentGroupChannel?.url) {
1835
logger.info('Refreshing conversation state');
19-
const { appInfo = {} } = sdk;
20-
const useReaction = appInfo.isUsingReaction || false;
36+
const useReaction = sdk?.appInfo?.isUsingReaction || false;
2137

2238
const messageListParams = new sdk.MessageListParams();
2339
messageListParams.prevResultSize = 30;
@@ -32,6 +48,7 @@ function useHandleReconnect({ isOnline }, {
3248
logger.info('Channel: Fetching messages', { currentGroupChannel, userFilledMessageListQuery });
3349
messagesDispatcher({
3450
type: messageActionTypes.GET_PREV_MESSAGES_START,
51+
payload: null,
3552
});
3653

3754
sdk.GroupChannel.getChannel(currentGroupChannel.url)
@@ -45,9 +62,10 @@ function useHandleReconnect({ isOnline }, {
4562
.then((messages) => {
4663
messagesDispatcher({
4764
type: messageActionTypes.CLEAR_SENT_MESSAGES,
65+
payload: null,
4866
});
4967

50-
const hasMore = (messages && messages.length > 0);
68+
const hasMore = messages?.length > 0;
5169
const lastMessageTimeStamp = hasMore
5270
? messages[0].createdAt
5371
: null;
@@ -66,7 +84,7 @@ function useHandleReconnect({ isOnline }, {
6684
logger.error('Channel: Fetching messages failed', error);
6785
})
6886
.finally(() => {
69-
currentGroupChannel.markAsRead();
87+
currentGroupChannel.markAsRead?.();
7088
});
7189
});
7290
}

src/smart-components/Conversation/hooks/useScrollToMessage.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { useCallback } from 'react';
22
import { SendbirdTypes } from '../../../types';
33
import { Logger } from '../../../index';
44

5-
interface useScrollToMessageDynamicParams {
5+
interface DynamicParams {
66
setIntialTimeStamp(ts: number): null;
77
setHighLightedMessageId(id: number): null;
88
allMessages: SendbirdTypes['BaseMessageInstance'][];
99
}
1010

11-
interface useScrollToMessageStaticParams {
11+
interface StaticParams {
1212
logger: Logger;
1313
}
1414

1515
function useScrollToMessage({
1616
setIntialTimeStamp,
1717
setHighLightedMessageId,
1818
allMessages,
19-
}: useScrollToMessageDynamicParams, {
20-
logger,
21-
}: useScrollToMessageStaticParams): (createdAt: number, messageId: number) => void {
19+
}: DynamicParams,
20+
{ logger }: StaticParams,
21+
): (createdAt: number, messageId: number) => void {
2222
return useCallback(
2323
(createdAt: number, messageId: number) => {
2424
const isPresent = allMessages.find((m) => (
@@ -37,10 +37,10 @@ function useScrollToMessage({
3737
}
3838
});
3939
}, [
40-
setIntialTimeStamp,
41-
setHighLightedMessageId,
42-
allMessages,
43-
],
40+
setIntialTimeStamp,
41+
setHighLightedMessageId,
42+
allMessages,
43+
],
4444
);
4545
}
4646

src/smart-components/OpenchannelConversation/hooks/useCheckScrollBottom.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { useCallback } from 'react';
22

3-
interface MainProps {
3+
interface DynamicParams {
44
conversationScrollRef: React.RefObject<HTMLDivElement>;
55
}
6-
interface ToolProps {
6+
interface StaticParams {
77
logger: SendbirdUIKit.Logger;
88
}
99

1010
function useCheckScrollBottom(
11-
{ conversationScrollRef }: MainProps,
12-
{ logger }: ToolProps,
11+
{ conversationScrollRef }: DynamicParams,
12+
{ logger }: StaticParams,
1313
): () => boolean {
1414
return useCallback(() => {
1515
let isBottom = true;

src/smart-components/OpenchannelConversation/hooks/useDeleteMessageCallback.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useCallback } from 'react';
22
import * as messageActionTypes from '../dux/actionTypes';
33

4-
interface MainProps {
4+
interface DynamicParams {
55
currentOpenChannel: SendbirdUIKit.OpenChannelType;
66
}
7-
interface ToolProps {
7+
interface StaticParams {
88
logger: SendbirdUIKit.Logger;
99
messagesDispatcher: ({ type: string, payload: any }) => void;
1010
}
@@ -15,8 +15,8 @@ type CallbackReturn = (
1515
) => void;
1616

1717
function useDeleteMessageCallback(
18-
{ currentOpenChannel }: MainProps,
19-
{ logger, messagesDispatcher }: ToolProps,
18+
{ currentOpenChannel }: DynamicParams,
19+
{ logger, messagesDispatcher }: StaticParams,
2020
): CallbackReturn {
2121
return useCallback((message, callback) => {
2222
logger.info('OpenChannel | useDeleteMessageCallback: Deleting message', message);

src/smart-components/OpenchannelConversation/hooks/useFileUploadCallback.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useCallback } from 'react';
33
import * as messageActionTypes from '../dux/actionTypes';
44
import * as utils from '../utils';
55

6-
interface MainProps {
6+
interface DynamicParams {
77
currentOpenChannel: SendbirdUIKit.OpenChannelType;
88
onBeforeSendFileMessage: (file: File) => Sendbird.FileMessageParams;
99
checkScrollBottom: () => boolean;
@@ -13,7 +13,7 @@ interface MainProps {
1313
resizingHeight?: number | string,
1414
};
1515
}
16-
interface ToolProps {
16+
interface StaticParams {
1717
sdk: SendbirdUIKit.Sdk;
1818
logger: SendbirdUIKit.Logger;
1919
messagesDispatcher: ({ type: string, payload: any }) => void;
@@ -27,8 +27,8 @@ function useFileUploadCallback(
2727
checkScrollBottom,
2828
imageCompression = {},
2929
onBeforeSendFileMessage,
30-
}: MainProps,
31-
{ sdk, logger, messagesDispatcher }: ToolProps,
30+
}: DynamicParams,
31+
{ sdk, logger, messagesDispatcher }: StaticParams,
3232
): CallbackReturn {
3333
return useCallback((file) => {
3434
if (sdk && sdk.FileMessageParams) {

src/smart-components/OpenchannelConversation/hooks/useHandleChannelEvents.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import * as messageActionTypes from '../dux/actionTypes';
33
import uuidv4 from '../../../utils/uuid';
44
import { scrollIntoLast } from '../utils';
55

6-
interface MainProps {
6+
interface DynamicParams {
77
currentOpenChannel: SendbirdUIKit.OpenChannelType;
88
checkScrollBottom: () => boolean;
99
}
10-
interface ToolProps {
10+
interface StaticParams {
1111
sdk: SendbirdUIKit.Sdk;
1212
logger: SendbirdUIKit.Logger;
1313
messagesDispatcher: ({ type: string, payload: any }) => void;
1414
}
1515

1616
function useHandleChannelEvents(
17-
{ currentOpenChannel, checkScrollBottom }: MainProps,
18-
{ sdk, logger, messagesDispatcher }: ToolProps,
17+
{ currentOpenChannel, checkScrollBottom }: DynamicParams,
18+
{ sdk, logger, messagesDispatcher }: StaticParams,
1919
): void {
2020
useEffect(() => {
2121
const messageReceiverId = uuidv4();

src/smart-components/OpenchannelConversation/hooks/useInitialMessagesFetch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { useEffect } from 'react';
22
import * as messageActionTypes from '../dux/actionTypes';
33
import { scrollIntoLast } from '../utils';
44

5-
interface MainProps {
5+
interface DynamicParams {
66
currentOpenChannel: SendbirdUIKit.OpenChannelType;
77
/* eslint-disable @typescript-eslint/no-explicit-any*/
88
userFilledMessageListParams?: Record<string, any>;
99
}
10-
interface ToolProps {
10+
interface StaticParams {
1111
sdk: SendbirdUIKit.Sdk;
1212
logger: SendbirdUIKit.Logger;
1313
messagesDispatcher: ({ type: string, payload: any }) => void;
1414
}
1515

1616
function useInitialMessagesFetch(
17-
{ currentOpenChannel, userFilledMessageListParams }: MainProps,
18-
{ sdk, logger, messagesDispatcher }: ToolProps,
17+
{ currentOpenChannel, userFilledMessageListParams }: DynamicParams,
18+
{ sdk, logger, messagesDispatcher }: StaticParams,
1919
): void {
2020
useEffect(() => {
2121
logger.info('OpenChannel | useInitialMessagesFetch: Setup started', currentOpenChannel);

src/smart-components/OpenchannelConversation/hooks/useResendMessageCallback.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { useCallback } from 'react';
22
import * as messageActionTypes from '../dux/actionTypes';
33

4-
interface MainProps {
4+
interface StaticParams {
55
currentOpenChannel: SendbirdUIKit.OpenChannelType;
66
}
7-
interface ToolProps {
7+
interface StaticParams {
88
logger: SendbirdUIKit.Logger;
99
messagesDispatcher: ({ type: string, payload: any }) => void;
1010
}
1111
type CallbackReturn = (failedMessage: SendbirdUIKit.ClientUserMessage | SendbirdUIKit.ClientFileMessage) => void;
1212

1313
function useResendMessageCallback(
14-
{ currentOpenChannel }: MainProps,
15-
{ logger, messagesDispatcher }: ToolProps,
14+
{ currentOpenChannel }: StaticParams,
15+
{ logger, messagesDispatcher }: StaticParams,
1616
): CallbackReturn {
1717
return useCallback((failedMessage) => {
1818
logger.info('OpenChannel | useResendMessageCallback: Resending message has started', failedMessage);

src/smart-components/OpenchannelConversation/hooks/useScrollCallback.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useCallback } from 'react';
22
import * as messageActionTypes from '../dux/actionTypes';
33

4-
interface MainProps {
4+
interface DynamicParams {
55
currentOpenChannel: SendbirdUIKit.OpenChannelType;
66
lastMessageTimestamp: number;
77
}
8-
interface ToolProps {
8+
interface StaticParams {
99
sdk: SendbirdUIKit.Sdk;
1010
logger: SendbirdUIKit.Logger;
1111
messagesDispatcher: ({ type: string, payload: any }) => void;
@@ -16,8 +16,8 @@ interface ToolProps {
1616
type CallbackReturn = (callback: () => void) => void;
1717

1818
function useScrollCallback(
19-
{ currentOpenChannel, lastMessageTimestamp }: MainProps,
20-
{ sdk, logger, messagesDispatcher, hasMore, userFilledMessageListParams }: ToolProps,
19+
{ currentOpenChannel, lastMessageTimestamp }: DynamicParams,
20+
{ sdk, logger, messagesDispatcher, hasMore, userFilledMessageListParams }: StaticParams,
2121
): CallbackReturn {
2222
return useCallback((callback) => {
2323
if (hasMore && sdk && sdk.MessageListParams) {

0 commit comments

Comments
 (0)