From ba2d11ff3d536f3ddabae00e4639834bed4be075 Mon Sep 17 00:00:00 2001 From: Andrea <65359340+andriumm@users.noreply.github.com> Date: Mon, 25 Mar 2024 10:00:27 +0100 Subject: [PATCH] migrate post_image.tsx to function component (#26395) --- .../post_view/post_image/post_image.tsx | 88 +++++++++++-------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/webapp/channels/src/components/post_view/post_image/post_image.tsx b/webapp/channels/src/components/post_view/post_image/post_image.tsx index 17c1791837..8f5a945847 100644 --- a/webapp/channels/src/components/post_view/post_image/post_image.tsx +++ b/webapp/channels/src/components/post_view/post_image/post_image.tsx @@ -1,10 +1,13 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React from 'react'; +import React, {useCallback} from 'react'; import type {KeyboardEvent, MouseEvent} from 'react'; -import type {Post, PostImage as PostImageMetadata} from '@mattermost/types/posts'; +import type { + Post, + PostImage as PostImageMetadata, +} from '@mattermost/types/posts'; import ExternalImage from 'components/external_image'; import FilePreviewModal from 'components/file_preview_modal'; @@ -14,7 +17,7 @@ import {ModalIdentifiers} from 'utils/constants'; import type {ModalData} from 'types/actions'; -interface Props { +type PostImageProps = { imageMetadata: PostImageMetadata; link: string; post: Post; @@ -23,46 +26,57 @@ interface Props { }; } -export default class PostImage extends React.PureComponent { - showModal = (e: (KeyboardEvent | MouseEvent), link = '') => { +const PostImage = ({ + imageMetadata, + link, + post, + actions, +}: PostImageProps) => { + const {openModal} = actions; + const showModal = useCallback(( + e: KeyboardEvent | MouseEvent, + link = '', + ) => { e.preventDefault(); - this.props.actions.openModal({ + openModal({ modalId: ModalIdentifiers.FILE_PREVIEW_MODAL, dialogType: FilePreviewModal, dialogProps: { - post: this.props.post, + post, startIndex: 0, - fileInfos: [{ - has_preview_image: false, - link, - extension: this.props.imageMetadata.format, - name: this.props.link, - }], + fileInfos: [ + { + has_preview_image: false, + link, + extension: imageMetadata.format, + name: link, + }, + ], }, }); - }; + }, [openModal, imageMetadata.format, post]); - render() { - return ( -
- - {(safeLink) => ( - - - - )} - -
- ); - } -} + return ( +
+ + {(safeLink) => ( + + + + )} + +
+ ); +}; + +export default PostImage;