Skip to content

Commit 1463368

Browse files
committed
fix: goToMessage behaviour
1 parent 21eab51 commit 1463368

File tree

1 file changed

+9
-33
lines changed

1 file changed

+9
-33
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,15 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
376376
[processedMessageList],
377377
);
378378

379+
const [should, setShould] = useState(true);
380+
379381
const maintainVisibleContentPosition = useMemo(() => {
380382
return {
381383
animateAutoscrollToBottom: true,
382-
autoscrollToBottomThreshold: 1,
384+
autoscrollToBottomThreshold: should ? 1 : undefined,
383385
startRenderingFromBottom: true,
384386
};
385-
}, []);
387+
}, [should]);
386388

387389
useEffect(() => {
388390
if (disabled) {
@@ -403,6 +405,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
403405
(message) => message?.id === targetedMessage,
404406
);
405407

408+
setShould(false);
406409
// the message we want to scroll to has not been loaded in the state yet
407410
if (indexOfParentInMessageList === -1) {
408411
loadChannelAroundMessage({ messageId: targetedMessage, setTargetedMessage });
@@ -416,41 +419,14 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
416419
index: indexOfParentInMessageList,
417420
viewPosition: 0.5,
418421
});
422+
setShould(true);
419423
setTargetedMessage(undefined);
420-
}, WAIT_FOR_SCROLL_TIMEOUT);
424+
}, 100);
421425
}
422426
}, [loadChannelAroundMessage, processedMessageList, setTargetedMessage, targetedMessage]);
423427

424-
const goToMessage = useStableCallback(async (messageId: string) => {
425-
const indexOfParentInMessageList = processedMessageList.findIndex(
426-
(message) => message?.id === messageId,
427-
);
428-
if (indexOfParentInMessageList !== -1) {
429-
flashListRef.current?.scrollToIndex({
430-
animated: true,
431-
index: indexOfParentInMessageList,
432-
viewPosition: 0.5,
433-
});
434-
setTargetedMessage(messageId);
435-
return;
436-
}
437-
try {
438-
if (indexOfParentInMessageList === -1) {
439-
clearTimeout(scrollToDebounceTimeoutRef.current);
440-
await loadChannelAroundMessage({ messageId });
441-
setTargetedMessage(messageId);
442-
443-
// now scroll to it with animated=true
444-
flashListRef.current?.scrollToIndex({
445-
animated: true,
446-
index: indexOfParentInMessageList,
447-
viewPosition: 0.5, // try to place message in the center of the screen
448-
});
449-
return;
450-
}
451-
} catch (e) {
452-
console.warn('Error while scrolling to message', e);
453-
}
428+
const goToMessage = useStableCallback((messageId: string) => {
429+
setTargetedMessage(messageId);
454430
});
455431

456432
useEffect(() => {

0 commit comments

Comments
 (0)