Skip to content

Commit b9d524d

Browse files
author
Sravan S
authored
fix: Clear scrollToMessage on loading (#637)
Clear `scrollBottom` on channel state loading Fixes: https://sendbird.atlassian.net/browse/UIKIT-4137
1 parent aebea34 commit b9d524d

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/modules/Channel/components/MessageList/hooks/__test__/useSetScrollToBottom.spec.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jest.mock('../../../../../../hooks/useDebounce', () => ({
88
}));
99

1010
const ScrollComponent = () => {
11-
const { scrollBottom, scrollToBottomHandler } = useSetScrollToBottom();
11+
const { scrollBottom, scrollToBottomHandler } = useSetScrollToBottom({ loading: true });
1212

1313
return (
1414
<div style={{ height: '100px', overflowY: 'scroll' }} onScroll={scrollToBottomHandler}>
@@ -20,7 +20,7 @@ const ScrollComponent = () => {
2020
describe('Channel/useSetScrollToBottom ', () => {
2121
it('should set scrollBottom as zero on initial render', () => {
2222
const { result } = renderHook(() => {
23-
const myGreeting = useSetScrollToBottom();
23+
const myGreeting = useSetScrollToBottom({ loading: true });
2424
return myGreeting;
2525
}, {
2626
initialProps: { a: 'Alice', b: 'Bob' },
@@ -29,6 +29,18 @@ describe('Channel/useSetScrollToBottom ', () => {
2929
expect(scrollBottom).toBe(0);
3030
});
3131

32+
it('should reset scrollToBottom when loading state changes', () => {
33+
const { result, rerender } = renderHook(({ loading }) => {
34+
const myGreeting = useSetScrollToBottom({ loading });
35+
return myGreeting;
36+
}, {
37+
initialProps: { loading: true },
38+
});
39+
rerender({ loading: false });
40+
const { scrollBottom } = result.current;
41+
expect(scrollBottom).toBe(0);
42+
});
43+
3244
it('should calculate correct value on scrollToBottom on execution', () => {
3345
const { container } = render(<ScrollComponent />);
3446
const scrollElement = container.firstChild as Element;

src/modules/Channel/components/MessageList/hooks/useSetScrollToBottom.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { useDebounce } from '../../../../../hooks/useDebounce';
33

44
const DELAY = 500;
55

6-
export function useSetScrollToBottom(): ({
6+
export function useSetScrollToBottom({
7+
loading,
8+
}: {
9+
loading: boolean;
10+
}): ({
711
scrollBottom: number;
812
scrollToBottomHandler: (e: React.UIEvent<HTMLDivElement, UIEvent>) => void;
913
}) {
1014
const [scrollBottom, setScrollBottom] = useState(0);
15+
useEffect(() => {
16+
if (loading) {
17+
setScrollBottom(0);
18+
}
19+
}, [loading]);
1120
const scrollCb = (e: React.UIEvent<HTMLDivElement, UIEvent>) => {
1221
const element = e.target as HTMLDivElement;
1322
try {

src/modules/Channel/components/MessageList/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const MessageList: React.FC<MessageListProps> = ({
147147
scrollRef,
148148
});
149149

150-
const { scrollToBottomHandler, scrollBottom } = useSetScrollToBottom();
150+
const { scrollToBottomHandler, scrollBottom } = useSetScrollToBottom({ loading });
151151

152152
if (loading) {
153153
return (typeof renderPlaceholderLoader === 'function')

0 commit comments

Comments
 (0)