feat: use context to pass message id to image component in an attachment

pull/3170/head
yougotwill 8 months ago
parent 8bf711a2e2
commit 3fd37bbb06

@ -6,9 +6,14 @@ import { isNumber } from 'lodash';
import { useDisableDrag } from '../../hooks/useDisableDrag'; import { useDisableDrag } from '../../hooks/useDisableDrag';
import { AttachmentType, AttachmentTypeWithPath } from '../../types/Attachment'; import { AttachmentType, AttachmentTypeWithPath } from '../../types/Attachment';
import { Spinner } from '../loading'; import { Spinner } from '../loading';
import { MessageModelType } from '../../models/messageType';
import { MessageGenericAttachment } from './message/message-content/MessageGenericAttachment'; import { MessageGenericAttachment } from './message/message-content/MessageGenericAttachment';
import { useEncryptedFileFetch } from '../../hooks/useEncryptedFileFetch'; import { useEncryptedFileFetch } from '../../hooks/useEncryptedFileFetch';
import { useMessageIdFromContext } from '../../contexts/MessageIdContext';
import {
useMessageDirection,
useMessageSelected,
useMessageTimestamp,
} from '../../state/selectors';
type Props = { type Props = {
alt: string; alt: string;
@ -28,17 +33,12 @@ type Props = {
playIconOverlay?: boolean; playIconOverlay?: boolean;
softCorners: boolean; softCorners: boolean;
forceSquare?: boolean; forceSquare?: boolean;
dropShadow?: boolean;
attachmentIndex?: number; attachmentIndex?: number;
direction?: MessageModelType;
highlight?: boolean; highlight?: boolean;
onClick?: (attachment: AttachmentTypeWithPath | AttachmentType) => void; onClick?: (attachment: AttachmentTypeWithPath | AttachmentType) => void;
onClickClose?: (attachment: AttachmentTypeWithPath | AttachmentType) => void; onClickClose?: (attachment: AttachmentTypeWithPath | AttachmentType) => void;
onError?: () => void; onError?: () => void;
/** used for debugging */
timestamp?: number;
}; };
const StyledOverlay = styled.div<Pick<Props, 'darkOverlay' | 'softCorners'>>` const StyledOverlay = styled.div<Pick<Props, 'darkOverlay' | 'softCorners'>>`
@ -66,15 +66,18 @@ export const Image = (props: Props) => {
playIconOverlay, playIconOverlay,
softCorners, softCorners,
forceSquare, forceSquare,
dropShadow,
attachmentIndex, attachmentIndex,
direction,
highlight, highlight,
url, url,
width: _width, width: _width,
timestamp,
} = props; } = props;
const messageId = useMessageIdFromContext();
const dropShadow = useMessageSelected(messageId);
const direction = useMessageDirection(messageId);
/** used for debugging */
const timestamp = useMessageTimestamp(messageId);
const disableDrag = useDisableDrag(); const disableDrag = useDisableDrag();
const { loading, urlToLoad } = useEncryptedFileFetch( const { loading, urlToLoad } = useEncryptedFileFetch(
url, url,

@ -10,11 +10,6 @@ import {
} from '../../types/Attachment'; } from '../../types/Attachment';
import { useIsMessageVisible } from '../../contexts/isMessageVisibleContext'; import { useIsMessageVisible } from '../../contexts/isMessageVisibleContext';
import {
useMessageDirection,
useMessageSelected,
useMessageTimestamp,
} from '../../state/selectors';
import { THUMBNAIL_SIDE } from '../../types/attachments/VisualAttachment'; import { THUMBNAIL_SIDE } from '../../types/attachments/VisualAttachment';
import { Image } from './Image'; import { Image } from './Image';
@ -24,7 +19,6 @@ type Props = {
imageBroken: boolean; imageBroken: boolean;
highlight: boolean; highlight: boolean;
onClickAttachment?: (attachment: AttachmentTypeWithPath | AttachmentType) => void; onClickAttachment?: (attachment: AttachmentTypeWithPath | AttachmentType) => void;
messageId?: string;
}; };
const StyledImageGrid = styled.div<{ flexDirection: 'row' | 'column' }>` const StyledImageGrid = styled.div<{ flexDirection: 'row' | 'column' }>`
@ -50,16 +44,11 @@ const Row = (
startIndex, startIndex,
onClickAttachment, onClickAttachment,
totalAttachmentsCount, totalAttachmentsCount,
messageId,
} = props; } = props;
const isMessageVisible = useIsMessageVisible(); const isMessageVisible = useIsMessageVisible();
const moreMessagesOverlay = totalAttachmentsCount > 3; const moreMessagesOverlay = totalAttachmentsCount > 3;
const moreMessagesOverlayText = moreMessagesOverlay ? `+${totalAttachmentsCount - 3}` : undefined; const moreMessagesOverlayText = moreMessagesOverlay ? `+${totalAttachmentsCount - 3}` : undefined;
const selected = useMessageSelected(messageId);
const direction = useMessageDirection(messageId);
const timestamp = useMessageTimestamp(messageId);
return ( return (
<> <>
{attachments.map((attachment, index) => { {attachments.map((attachment, index) => {
@ -81,9 +70,6 @@ const Row = (
softCorners={true} softCorners={true}
darkOverlay={showOverlay} darkOverlay={showOverlay}
overlayText={showOverlay ? moreMessagesOverlayText : undefined} overlayText={showOverlay ? moreMessagesOverlayText : undefined}
dropShadow={selected}
direction={direction}
timestamp={timestamp}
/> />
); );
})} })}
@ -92,7 +78,7 @@ const Row = (
}; };
export const ImageGrid = (props: Props) => { export const ImageGrid = (props: Props) => {
const { attachments, imageBroken, highlight, onError, onClickAttachment, messageId } = props; const { attachments, imageBroken, highlight, onError, onClickAttachment } = props;
if (!attachments || !attachments.length) { if (!attachments || !attachments.length) {
return null; return null;
@ -110,7 +96,6 @@ export const ImageGrid = (props: Props) => {
renderedSize={THUMBNAIL_SIDE} renderedSize={THUMBNAIL_SIDE}
startIndex={0} startIndex={0}
totalAttachmentsCount={attachments.length} totalAttachmentsCount={attachments.length}
messageId={messageId}
/> />
</StyledImageGrid> </StyledImageGrid>
); );
@ -129,7 +114,6 @@ export const ImageGrid = (props: Props) => {
renderedSize={THUMBNAIL_SIDE} renderedSize={THUMBNAIL_SIDE}
startIndex={0} startIndex={0}
totalAttachmentsCount={attachments.length} totalAttachmentsCount={attachments.length}
messageId={messageId}
/> />
</StyledImageGrid> </StyledImageGrid>
); );
@ -149,7 +133,6 @@ export const ImageGrid = (props: Props) => {
renderedSize={THUMBNAIL_SIDE} renderedSize={THUMBNAIL_SIDE}
startIndex={0} startIndex={0}
totalAttachmentsCount={attachments.length} totalAttachmentsCount={attachments.length}
messageId={messageId}
/> />
<StyledImageGrid flexDirection={'column'}> <StyledImageGrid flexDirection={'column'}>
@ -162,7 +145,6 @@ export const ImageGrid = (props: Props) => {
renderedSize={columnImageSide} renderedSize={columnImageSide}
startIndex={1} startIndex={1}
totalAttachmentsCount={attachments.length} totalAttachmentsCount={attachments.length}
messageId={messageId}
/> />
</StyledImageGrid> </StyledImageGrid>
</StyledImageGrid> </StyledImageGrid>

@ -27,6 +27,7 @@ import { ClickToTrustSender } from './ClickToTrustSender';
import { MessageHighlighter } from './MessageHighlighter'; import { MessageHighlighter } from './MessageHighlighter';
import { useIsDetailMessageView } from '../../../../contexts/isDetailViewContext'; import { useIsDetailMessageView } from '../../../../contexts/isDetailViewContext';
import { MessageGenericAttachment } from './MessageGenericAttachment'; import { MessageGenericAttachment } from './MessageGenericAttachment';
import { ContextMessageProvider } from '../../../../contexts/MessageIdContext';
export type MessageAttachmentSelectorProps = Pick< export type MessageAttachmentSelectorProps = Pick<
MessageRenderingProps, MessageRenderingProps,
@ -133,18 +134,19 @@ export const MessageAttachment = (props: Props) => {
} }
return ( return (
<MessageHighlighter highlight={highlight}> <ContextMessageProvider value={messageId}>
<StyledImageGridContainer messageDirection={direction}> <MessageHighlighter highlight={highlight}>
<ImageGrid <StyledImageGridContainer messageDirection={direction}>
messageId={messageId} <ImageGrid
attachments={attachments} attachments={attachments}
imageBroken={imageBroken} imageBroken={imageBroken}
highlight={highlight} highlight={highlight}
onError={handleImageError} onError={handleImageError}
onClickAttachment={onClickOnImageGrid} onClickAttachment={onClickOnImageGrid}
/> />
</StyledImageGridContainer> </StyledImageGridContainer>
</MessageHighlighter> </MessageHighlighter>
</ContextMessageProvider>
); );
} }

@ -0,0 +1,14 @@
import { createContext, useContext } from 'react';
/**
* This React context is used to share deep into a node tree the message ID we are currently rendering.
* This is to avoid passing the prop to all the subtree component
*/
const ContextMessageId = createContext<string | undefined>(undefined);
export const ContextMessageProvider = ContextMessageId.Provider;
export function useMessageIdFromContext() {
const messageId = useContext(ContextMessageId);
return messageId;
}
Loading…
Cancel
Save