From 1048042f7de2b95b23f76b1397587ef57b589497 Mon Sep 17 00:00:00 2001 From: audric Date: Fri, 27 Aug 2021 17:23:46 +1000 Subject: [PATCH] fix attachment pre-decrypting when out of view --- ts/components/conversation/ImageGrid.tsx | 2 +- .../conversation/ReadableMessage.tsx | 14 +---- .../conversation/message/MessageContent.tsx | 51 ++++++++++++++----- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/ts/components/conversation/ImageGrid.tsx b/ts/components/conversation/ImageGrid.tsx index 3d8ce5f61..05b3f6d90 100644 --- a/ts/components/conversation/ImageGrid.tsx +++ b/ts/components/conversation/ImageGrid.tsx @@ -12,7 +12,7 @@ import { } from '../../types/Attachment'; import { Image } from './Image'; -import { IsMessageVisibleContext } from './ReadableMessage'; +import { IsMessageVisibleContext } from './message/MessageContent'; type Props = { attachments: Array; diff --git a/ts/components/conversation/ReadableMessage.tsx b/ts/components/conversation/ReadableMessage.tsx index 0b8ae66d4..c4f580279 100644 --- a/ts/components/conversation/ReadableMessage.tsx +++ b/ts/components/conversation/ReadableMessage.tsx @@ -1,5 +1,5 @@ import _, { noop } from 'lodash'; -import React, { createContext, useCallback, useState } from 'react'; +import React, { useCallback } from 'react'; import { InView } from 'react-intersection-observer'; import { useDispatch, useSelector } from 'react-redux'; import { getMessageById } from '../../data/data'; @@ -42,14 +42,11 @@ const debouncedTriggerLoadMore = _.debounce( 100 ); -export const IsMessageVisibleContext = createContext(false); - export const ReadableMessage = (props: ReadableMessageProps) => { const { messageId, onContextMenu, className, receivedAt, isUnread } = props; const isAppFocused = useSelector(getIsAppFocused); const dispatch = useDispatch(); - // onVisible={haveDoneFirstScrollProp ? onVisible : noop} const selectedConversationKey = useSelector(getSelectedConversationKey); const loadedMessagesLength = useSelector(getLoadedMessagesLength); @@ -59,8 +56,6 @@ export const ReadableMessage = (props: ReadableMessageProps) => { const fetchingMore = useSelector(areMoreMessagesBeingFetched); const shouldMarkReadWhenVisible = isUnread; - const [isMessageVisible, setMessageIsVisible] = useState(false); - const onVisible = useCallback( // tslint:disable-next-line: cyclomatic-complexity async (inView: boolean | Object) => { @@ -98,9 +93,6 @@ export const ReadableMessage = (props: ReadableMessageProps) => { ((inView as any).type === 'focus' && (inView as any).returnValue === true)) && isAppFocused ) { - if (isMessageVisible !== true) { - setMessageIsVisible(true); - } if (shouldMarkReadWhenVisible) { const found = await getMessageById(messageId); @@ -140,9 +132,7 @@ export const ReadableMessage = (props: ReadableMessageProps) => { triggerOnce={false} trackVisibility={true} > - - {props.children} - + {props.children} ); }; diff --git a/ts/components/conversation/message/MessageContent.tsx b/ts/components/conversation/message/MessageContent.tsx index 5bcb87430..ad4e82480 100644 --- a/ts/components/conversation/message/MessageContent.tsx +++ b/ts/components/conversation/message/MessageContent.tsx @@ -1,6 +1,7 @@ import classNames from 'classnames'; import moment from 'moment'; -import React, { useCallback, useState } from 'react'; +import React, { createContext, useCallback, useState } from 'react'; +import { InView } from 'react-intersection-observer'; import { useSelector } from 'react-redux'; import _ from 'underscore'; import { MessageRenderingProps, QuoteClickOptions } from '../../../models/messageType'; @@ -89,13 +90,27 @@ function onClickOnMessageInnerContainer(event: React.MouseEvent) } } +export const IsMessageVisibleContext = createContext(false); + export const MessageContent = (props: Props) => { const contentProps = useSelector(state => getMessageContentSelectorProps(state as any, props.messageId) ); + const [isMessageVisible, setMessageIsVisible] = useState(false); const [imageBroken, setImageBroken] = useState(false); + const onVisible = (inView: boolean | Object) => { + if ( + inView === true || + ((inView as any).type === 'focus' && (inView as any).returnValue === true) + ) { + if (isMessageVisible !== true) { + setMessageIsVisible(true); + } + } + }; + const handleImageError = useCallback(() => { setImageBroken(true); }, [setImageBroken]); @@ -147,18 +162,28 @@ export const MessageContent = (props: Props) => { onClick={onClickOnMessageInnerContainer} title={toolTipTitle} > - - - {hasContentAfterAttachmentAndQuote ? ( - - - - - ) : null} + + + + + {hasContentAfterAttachmentAndQuote ? ( + + + + + ) : null} + + ); };