imageElement.tsx
TLDR
This file contains a React component called ImageElement that displays an image. The component receives a block prop and renders the image associated with that block. It also includes methods related to loading and displaying the image.
Methods
ImageElement
This method is the main React component that renders an image based on the block prop received. It uses the useState and useEffect hooks to load the image data URL from the server and set it in the component's state. It also checks if the image is archived and renders an ArchivedFile component in that case. If the image data URL is not available, it returns null. Otherwise, it renders an img
element with the class name "ImageElement" and the image data URL as the source.
registerContentType
This method is called to register the image content type with the contentRegistry. It provides methods for displaying the content type's text and icon, creating a new ImageBlock, and creating the ImageElement component.
Classes
None
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useEffect, useState} from 'react'
import {IntlShape} from 'react-intl'
import {ContentBlock} from '../../blocks/contentBlock'
import {ImageBlock, createImageBlock} from '../../blocks/imageBlock'
import octoClient from '../../octoClient'
import {Utils} from '../../utils'
import ImageIcon from '../../widgets/icons/image'
import {sendFlashMessage} from '../../components/flashMessages'
import {FileInfo} from '../../blocks/block'
import {contentRegistry} from './contentRegistry'
import ArchivedFile from './archivedFile/archivedFile'
type Props = {
block: ContentBlock
}
const ImageElement = (props: Props): JSX.Element|null => {
const [imageDataUrl, setImageDataUrl] = useState<string|null>(null)
const [fileInfo, setFileInfo] = useState<FileInfo>({})
const {block} = props
useEffect(() => {
if (!imageDataUrl) {
const loadImage = async () => {
const fileURL = await octoClient.getFileAsDataUrl(block.boardId, props.block.fields.fileId)
setImageDataUrl(fileURL.url || '')
setFileInfo(fileURL)
}
loadImage()
}
}, [])
if (fileInfo.archived) {
return (
<ArchivedFile fileInfo={fileInfo}/>
)
}
if (!imageDataUrl) {
return null
}
return (
<img
className='ImageElement'
src={imageDataUrl}
alt={block.title}
/>
)
}
contentRegistry.registerContentType({
type: 'image',
getDisplayText: (intl: IntlShape) => intl.formatMessage({id: 'ContentBlock.image', defaultMessage: 'image'}),
getIcon: () => <ImageIcon/>,
createBlock: async (boardId: string, intl: IntlShape) => {
return new Promise<ImageBlock>(
(resolve) => {
Utils.selectLocalFile(async (file) => {
const fileId = await octoClient.uploadFile(boardId, file)
if (fileId) {
const block = createImageBlock()
block.fields.fileId = fileId || ''
resolve(block)
} else {
sendFlashMessage({content: intl.formatMessage({id: 'createImageBlock.failed', defaultMessage: 'Unable to upload the file. File size limit reached.'}), severity: 'normal'})
}
},
'.jpg,.jpeg,.png,.gif')
},
)
// return new ImageBlock()
},
createComponent: (block) => <ImageElement block={block}/>,
})
export default React.memo(ImageElement)