Skip to content

Commit cf3c084

Browse files
HoonBaekAhyoungRyu
andauthored
fix: Move message list scroll when the last message is edited (#674)
* remove the duplicated functions: handleMessageHeightChange, and handleMessageListHeightChange AND combine them into one function: moveScroll * add optional params to the moveScroll for moving the scroll only when the last message is reached the bottom * move scroll only when the last message's updatedAt is changed --------- Co-authored-by: Ahyoung Ryu <[email protected]>
1 parent a379d6d commit cf3c084

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ type MessageUIProps = {
3333
hasSeparator?: boolean;
3434
chainTop?: boolean;
3535
chainBottom?: boolean;
36-
handleScroll?: () => void;
37-
handleMessageListHeightChange?: () => void;
36+
handleScroll?: (isBottomMessageAffected?: boolean) => void;
3837
// for extending
3938
renderMessage?: (props: RenderMessageProps) => React.ReactElement;
4039
renderCustomSeparator?: (props: RenderCustomSeparatorProps) => React.ReactElement;
@@ -49,7 +48,6 @@ const Message = ({
4948
chainTop,
5049
chainBottom,
5150
handleScroll,
52-
handleMessageListHeightChange,
5351
renderCustomSeparator,
5452
renderEditInput,
5553
renderMessage,
@@ -136,16 +134,20 @@ const Message = ({
136134
}));
137135
}, [mentionedUserIds]);
138136

137+
useLayoutEffect(() => {
138+
// Keep the scrollBottom value after fetching new message list
139+
handleScroll?.();
140+
}, []);
139141
/**
140142
* Move the messsage list scroll
141143
* when the message's height is changed by `showEdit` OR `message.reactions`
142144
*/
143145
useDidMountEffect(() => {
144146
handleScroll?.();
145147
}, [showEdit, message?.reactions?.length]);
146-
useLayoutEffect(() => {
147-
handleMessageListHeightChange?.();
148-
}, []);
148+
useDidMountEffect(() => {
149+
handleScroll?.(true);
150+
}, [message?.updatedAt]);
149151

150152
useLayoutEffect(() => {
151153
let animationTimeout = null;

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,21 @@ const MessageList: React.FC<MessageListProps> = ({
120120
};
121121

122122
/**
123-
* Move the messsage list scroll
124-
* when each message's height is changed by `reactions` OR `showEdit`
123+
* 1. Move the messsage list scroll
124+
* when each message's height is changed by `reactions` OR `showEdit`
125+
* 2. Keep the scrollBottom value after fetching new message list
125126
*/
126-
const handleMessageHeightChange = () => {
127+
const moveScroll = (isBottomMessageAffected = false): void => {
127128
const current = scrollRef?.current;
128129
if (current) {
129130
const bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;
130-
if (scrollBottom < bottom) {
131+
if (scrollBottom < bottom
132+
&& (!isBottomMessageAffected || scrollBottom < SCROLL_BUFFER)) {
131133
// Move the scroll as much as the height of the message has changed
132134
current.scrollTop += bottom - scrollBottom;
133135
}
134136
}
135137
};
136-
// Keep the scrollBottom value after fetching new message list
137-
const handleMessageListHeightChange = () => {
138-
const current = scrollRef?.current;
139-
if (current) {
140-
const bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;
141-
if (scrollBottom < bottom) {
142-
current.scrollTop += bottom - scrollBottom;
143-
}
144-
}
145-
};
146138

147139
const handleOnScroll = useHandleOnScrollCallback({
148140
hasMore: hasMorePrev,
@@ -193,8 +185,7 @@ const MessageList: React.FC<MessageListProps> = ({
193185
return (
194186
<MessageProvider message={m} key={m?.messageId} isByMe={isByMe}>
195187
<Message
196-
handleScroll={handleMessageHeightChange}
197-
handleMessageListHeightChange={handleMessageListHeightChange}
188+
handleScroll={moveScroll}
198189
renderMessage={renderMessage}
199190
message={m}
200191
hasSeparator={hasSeparator}

0 commit comments

Comments
 (0)