mattermost-mobile-upstream/app/components/block_renderer/image_block.tsx
Daniel Espino García 84ea188366
Mattermost Blocks (#9764)
* Mattermost Blocks

* Move block renderer to its own components folder

* Sync translation layer

* UX fixes

* Refactor button to use the button component

* Code review

* Fix CI

* Add animations to collapse blocks

* Add unit tests

* Add e2e tests and related fixes

* Address coderabbit comments

* Address UX comments

* Address feedback

* Address coderabbit comments

* Fix snapshots

* Sync with webapp translation

* Add node limit to button markdown node

* Address feedback

* i18n-extract

* Fix test

* Move image utils to the utils folder

* Remove unneeded style

* Revert build script change

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-06-29 18:42:16 +02:00

57 lines
1.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useContext, useMemo} from 'react';
import {View, type ViewStyle} from 'react-native';
import {removeImageProxyForKey} from '@utils/markdown';
import {secureGetFromRecord} from '@utils/types';
import {MmBlocksRenderContext} from './context';
import MmBlocksImage from './mm_blocks_image';
import {getStyleSheet} from './styles';
import {resolveMmImageCaps} from './utils/image';
type ImageBlockProps = {
block: MmImageBlock;
theme: Theme;
};
export const ImageBlock = ({block, theme}: ImageBlockProps) => {
const style = getStyleSheet(theme);
const renderContext = useContext(MmBlocksRenderContext);
const imagesMetadata = renderContext?.imagesMetadata;
const url = block.url.trim();
const metadataKey = removeImageProxyForKey(url);
const imageMetadata = secureGetFromRecord(imagesMetadata, metadataKey) ?? secureGetFromRecord(imagesMetadata, url);
const caps = useMemo(() => resolveMmImageCaps(block), [block]);
const imageRowStyle = useMemo(() => {
const align = block.horizontal_alignment ?? 'left';
let alignStyle: ViewStyle = style.imageAlignLeft;
if (align === 'center') {
alignStyle = style.imageAlignCenter;
} else if (align === 'right') {
alignStyle = style.imageAlignRight;
}
return [style.imageRow, alignStyle];
}, [block.horizontal_alignment, style]);
if (!url) {
return null;
}
return (
<View style={imageRowStyle}>
<MmBlocksImage
key={url}
altText={block.alt_text}
caps={caps}
imageMetadata={imageMetadata}
imageStyle={block.image_style}
imageUrl={url}
theme={theme}
/>
</View>
);
};