Skip to content

Commit 2a4fb81

Browse files
Remove optimizations, use data instead of totalCount
1 parent add5c85 commit 2a4fb81

File tree

1 file changed

+27
-47
lines changed

1 file changed

+27
-47
lines changed

src/components/MessageList/VirtualizedMessageList.tsx

+27-47
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import type { RefObject } from 'react';
33
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
44
import {
5-
ComputeItemKey,
65
FollowOutputScalarType,
76
ScrollSeekConfiguration,
87
ScrollSeekPlaceholderProps,
@@ -38,7 +37,6 @@ import { MessageProps, MessageSimple, MessageUIComponentProps } from '../Message
3837
import { UnreadMessagesNotification as DefaultUnreadMessagesNotification } from './UnreadMessagesNotification';
3938
import {
4039
calculateFirstItemIndex,
41-
calculateItemIndex,
4240
EmptyPlaceholder,
4341
Header,
4442
Item,
@@ -167,10 +165,6 @@ function useCaptureResizeObserverExceededError() {
167165
}, []);
168166
}
169167

170-
function fractionalItemSize(element: HTMLElement) {
171-
return element.getBoundingClientRect().height;
172-
}
173-
174168
function findMessageIndex(messages: Array<{ id: string }>, id: string) {
175169
return messages.findIndex((message) => message.id === id);
176170
}
@@ -221,8 +215,6 @@ const VirtualizedMessageListWithContext = <
221215
messages,
222216
notifications,
223217
openThread,
224-
// TODO: refactor to scrollSeekPlaceHolderConfiguration and components.ScrollSeekPlaceholder, like the Virtuoso Component
225-
overscan = 0,
226218
reactionDetailsSort,
227219
read,
228220
returnAllReadData = false,
@@ -234,7 +226,7 @@ const VirtualizedMessageListWithContext = <
234226
showUnreadNotificationAlways,
235227
sortReactionDetails,
236228
sortReactions,
237-
stickToBottomScrollBehavior = 'auto',
229+
stickToBottomScrollBehavior = 'smooth',
238230
suppressAutoscroll,
239231
threadList,
240232
} = props;
@@ -438,14 +430,6 @@ const VirtualizedMessageListWithContext = <
438430
return isAtBottom ? stickToBottomScrollBehavior : false;
439431
};
440432

441-
const computeItemKey = useCallback<
442-
ComputeItemKey<UnknownType, VirtuosoContext<StreamChatGenerics>>
443-
>(
444-
(index, _, { numItemsPrepended, processedMessages }) =>
445-
processedMessages[calculateItemIndex(index, numItemsPrepended)].id,
446-
[],
447-
);
448-
449433
atBottomRef.current = (isAtBottom) => {
450434
atBottom.current = isAtBottom;
451435
setIsMessageListScrolledToBottom(isAtBottom);
@@ -479,34 +463,34 @@ const VirtualizedMessageListWithContext = <
479463
}, [highlightedMessageId, processedMessages]);
480464

481465
// force autoscrollToBottom if user hasn't interracted yet
482-
useEffect(() => {
483-
/**
484-
* a combination of parameters paired with extra data load on Virtuoso render causes
485-
* a message list to render a set of items not at the bottom of the list as expected
486-
* but rather either in the middle or a few hundredth pixels from the bottom
487-
*
488-
* `atTopStateChange` - if at top, load previous page, changing this to `startReached` reduces the amount of errors as it is not
489-
* being triggered at Virtuoso render but does not solve the core issue
490-
* `followOutput` - function which returns "smooth" value which is somehow more error-prone for Firefox and Safari
491-
*/
466+
// useEffect(() => {
467+
/**
468+
* a combination of parameters paired with extra data load on Virtuoso render causes
469+
* a message list to render a set of items not at the bottom of the list as expected
470+
* but rather either in the middle or a few hundredth pixels from the bottom
471+
*
472+
* `atTopStateChange` - if at top, load previous page, changing this to `startReached` reduces the amount of errors as it is not
473+
* being triggered at Virtuoso render but does not solve the core issue
474+
* `followOutput` - function which returns "smooth" value which is somehow more error-prone for Firefox and Safari
475+
*/
492476

493-
if (
494-
highlightedMessageId ||
495-
userInterractedWithScrollableViewRef.current ||
496-
atBottom.current
497-
) {
498-
return;
499-
}
477+
// if (
478+
// highlightedMessageId ||
479+
// userInterractedWithScrollableViewRef.current ||
480+
// atBottom.current
481+
// ) {
482+
// return;
483+
// }
500484

501-
const timeout = setTimeout(() => {
502-
userInterractedWithScrollableViewRef.current = true;
503-
virtuoso.current?.autoscrollToBottom();
504-
}, 0);
485+
// const timeout = setTimeout(() => {
486+
// userInterractedWithScrollableViewRef.current = true;
487+
// virtuoso.current?.autoscrollToBottom();
488+
// }, 0);
505489

506-
return () => {
507-
clearTimeout(timeout);
508-
};
509-
}, [atBottom, highlightedMessageId, processedMessages]);
490+
// return () => {
491+
// clearTimeout(timeout);
492+
// };
493+
// }, [atBottom, highlightedMessageId, processedMessages]);
510494

511495
if (!processedMessages) return null;
512496

@@ -535,7 +519,6 @@ const VirtualizedMessageListWithContext = <
535519
<Virtuoso<UnknownType, VirtuosoContext<StreamChatGenerics>>
536520
atBottomStateChange={atBottomStateChange}
537521
atBottomThreshold={100}
538-
// atTopStateChange={atTopStateChange}
539522
startReached={atTopStateChange}
540523
className='str-chat__message-list-scroll'
541524
components={{
@@ -544,7 +527,6 @@ const VirtualizedMessageListWithContext = <
544527
Item,
545528
...virtuosoComponentsFromProps,
546529
}}
547-
computeItemKey={computeItemKey}
548530
context={{
549531
additionalMessageInputProps,
550532
closeReactionSelectorOnClick,
@@ -584,7 +566,6 @@ const VirtualizedMessageListWithContext = <
584566
highlightedMessageId,
585567
)}
586568
itemContent={messageRenderer}
587-
itemSize={fractionalItemSize}
588569
itemsRendered={handleItemsRendered}
589570
key={messageSetKey}
590571
onTouchMove={() => {
@@ -593,10 +574,9 @@ const VirtualizedMessageListWithContext = <
593574
onWheel={() => {
594575
userInterractedWithScrollableViewRef.current = true;
595576
}}
596-
overscan={overscan}
597577
ref={virtuoso}
598578
style={{ overflowX: 'hidden', overscrollBehavior: 'none' }}
599-
totalCount={processedMessages.length}
579+
data={processedMessages}
600580
{...extra}
601581
/>
602582
</div>

0 commit comments

Comments
 (0)