Skip to content

Commit

Permalink
fix: improve scrollToBottom with image attachments
Browse files Browse the repository at this point in the history
- Removed keys that caused remounting of images when re-rendering
- Made ResizeObserver less-trigger happy
- Avoided conflict between quoted images jumping and gallery
  • Loading branch information
petyosi committed Aug 5, 2022
1 parent 34082a4 commit be8bb7a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
12 changes: 1 addition & 11 deletions src/components/Attachment/AttachmentContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { PropsWithChildren } from 'react';
import ReactPlayer from 'react-player';
import clsx from 'clsx';
import { nanoid } from 'nanoid';

import { AttachmentActions as DefaultAttachmentActions } from './AttachmentActions';
import { Audio as DefaultAudio } from './Audio';
Expand Down Expand Up @@ -43,16 +42,7 @@ export const AttachmentWithinContainer = <
'str-chat__message-attachment-with-actions': extra === 'actions', // added for theme V2 (better readability)
});

return (
<div
className={classNames}
key={`${isGAT ? '' : attachment?.id || attachment?.fallback || nanoid()}-${
attachment?.type || 'none'
}`}
>
{children}
</div>
);
return <div className={classNames}>{children}</div>;
};

export const AttachmentActionsContainer = <
Expand Down
10 changes: 1 addition & 9 deletions src/components/Attachment/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { PropsWithChildren, ReactNode } from 'react';
import ReactPlayer from 'react-player';
import { nanoid } from 'nanoid';
import clsx from 'clsx';

import type { ATTACHMENT_GROUPS_ORDER } from './Attachment';
Expand Down Expand Up @@ -117,14 +116,7 @@ export const renderAttachmentWithinContainer = <
'str-chat__message-attachment-with-actions': extra === 'actions', // added for theme V2 (better readability)
});

return (
<div
className={classNames}
key={`${isGAT ? '' : attachment?.id || nanoid()}-${attachment?.type || 'none'}`}
>
{children}
</div>
);
return <div className={classNames}>{children}</div>;
};

/**
Expand Down
1 change: 0 additions & 1 deletion src/components/Gallery/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const ImageComponent = <
className='str-chat__message-attachment--img'
data-testid='image-test'
onClick={toggleModal}
onKeyDown={toggleModal}
src={imageSrc}
tabIndex={0}
/>
Expand Down
6 changes: 5 additions & 1 deletion src/components/Message/QuotedMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export const QuotedMessage = <
<>
<div
className={clsx('str-chat__quoted-message-preview quoted-message', { mine: isMyMessage() })}
onClick={() => jumpToMessage(quoted_message.id)}
onClickCapture={(e) => {
e.stopPropagation();
e.preventDefault();
jumpToMessage(quoted_message.id);
}}
>
{quoted_message.user && (
<Avatar
Expand Down
10 changes: 9 additions & 1 deletion src/components/MessageList/hooks/useScrollLocationLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const useScrollLocationLogic = <
const closeToBottom = useRef(false);
const closeToTop = useRef(false);
const scrollCounter = useRef({ autoScroll: 0, scroll: 0 });
const ulElementHeight = useRef(0);

const scrollToBottom = useCallback(() => {
if (!listElement?.scrollTo || hasMoreNewer || suppressAutoscroll) {
Expand All @@ -57,7 +58,14 @@ export const useScrollLocationLogic = <

useEffect(() => {
if (!listElement) return;
const observer = new ResizeObserver(scrollToBottom);
ulElementHeight.current = ulElement?.getBoundingClientRect().height || 0;

const observer = new ResizeObserver(([entry]) => {
if (ulElementHeight.current !== entry.contentRect.height) {
ulElementHeight.current = entry.contentRect.height;
scrollToBottom();
}
});

const cancelObserverOnUserScroll = () => {
scrollCounter.current.scroll += 1;
Expand Down

0 comments on commit be8bb7a

Please sign in to comment.