Skip to content

Commit 68e0cb8

Browse files
authored
[CLNP-6022] fix scroll position issue when switching GroupChannel (#1282)
Fixes https://sendbird.atlassian.net/browse/CLNP-6022 This PR addresses an issue where the scroll position was not correctly set to the bottom when switching from a channel with few messages to one with many messages. The problem was resolved by adding a delay to ensure the scroll reference is updated before attempting to scroll to the bottom. This change ensures that users always see the latest messages when switching channels.
1 parent 766d1d7 commit 68e0cb8

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

src/modules/GroupChannel/context/hooks/useGroupChannel.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import useSendbird from '../../../../lib/Sendbird/context/hooks/useSendbird';
1919
import { GroupChannelContext } from '../GroupChannelProvider';
2020
import type { GroupChannelState, MessageActions } from '../types';
2121
import { useMessageActions } from './useMessageActions';
22+
import { delay } from '../../../../utils/utils';
2223

2324
export interface GroupChannelActions extends MessageActions {
2425
// Channel actions
@@ -69,16 +70,16 @@ export const useGroupChannel = () => {
6970

7071
const scrollToBottom = async (animated?: boolean) => {
7172
if (!state.scrollRef.current) return;
73+
// wait a bit for scroll ref to be updated
74+
await delay();
7275

7376
flagActions.setAnimatedMessageId(null);
7477
flagActions.setIsScrollBottomReached(true);
7578

7679
if (config.isOnline && state.hasNext()) {
7780
await state.resetWithStartingPoint(Number.MAX_SAFE_INTEGER);
78-
state.scrollPubSub.publish('scrollToBottom', { animated });
79-
} else {
80-
state.scrollPubSub.publish('scrollToBottom', { animated });
8181
}
82+
state.scrollPubSub.publish('scrollToBottom', { animated });
8283

8384
if (state.currentChannel && !state.hasNext()) {
8485
state.resetNewMessages();

src/modules/Thread/context/__test__/useThread.spec.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,6 @@ describe('useThread', () => {
753753
});
754754

755755
await waitFor(() => {
756-
console.log(result.current.state.localThreadMessages[0]);
757756
expect(result.current.state.localThreadMessages[0].messageParams.fileInfoList).toContain(newFileInfo);
758757
});
759758
});

src/utils/__tests__/utils.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
isMultipleFilesMessage,
77
} from '../index';
88
import { AdminMessage, FileMessage, MultipleFilesMessage, UserMessage } from '@sendbird/chat/message';
9-
import { deleteNullish } from '../utils';
9+
import { delay, deleteNullish } from '../utils';
1010
import { isMobileIOS } from '../browser';
1111

1212
describe('Global-utils: verify message type util functions', () => {
@@ -234,3 +234,40 @@ describe('deleteNullish', () => {
234234
expect(component({ a: null, b: '3', c: 4 })).toEqual({ a: 1, b: '3', c: 4 });
235235
});
236236
});
237+
238+
describe('delay', () => {
239+
it('should resolve after the specified time', async () => {
240+
const start = Date.now();
241+
const delayTime = 100;
242+
243+
await delay(delayTime);
244+
245+
const end = Date.now();
246+
const elapsed = end - start;
247+
248+
// Check if the elapsed time is at least the delay time
249+
expect(elapsed).toBeGreaterThanOrEqual(delayTime);
250+
});
251+
252+
it('should resolve immediately for 0 milliseconds', async () => {
253+
const start = Date.now();
254+
255+
await delay(0);
256+
257+
const end = Date.now();
258+
const elapsed = end - start;
259+
260+
// Check if the elapsed time is very small
261+
expect(elapsed).toBeLessThan(10);
262+
});
263+
it('should resolve immediately when no parameter is provided', async () => {
264+
const start = Date.now();
265+
266+
await delay();
267+
268+
const end = Date.now();
269+
const elapsed = end - start;
270+
271+
expect(elapsed).toBeLessThan(10);
272+
});
273+
});

src/utils/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { SendableMessageType } from './index';
22

3+
/**
4+
* @param ms - milliseconds to delay
5+
* @returns Promise that resolves after the specified time
6+
*/
7+
export const delay = (ms?: number) => new Promise((resolve) => { setTimeout(resolve, ms); });
38
export const noop = () => {
49
/** noop * */
510
};

0 commit comments

Comments
 (0)