import * as React from "react"; import * as Constants from "~/common/constants"; import * as Validations from "~/common/validations"; import * as Utilities from "~/common/utilities"; import * as SVG from "~/common/svg"; import * as Strings from "~/common/strings"; import { css } from "@emotion/react"; import { FileTypeIcon } from "~/components/core/FileTypeIcon"; import { Blurhash } from "react-blurhash"; import { isBlurhashValid } from "blurhash"; import FontObjectPreview from "~/components/core/FontFrame/Views/FontObjectPreview"; const STYLES_IMAGE_CONTAINER = css` background-color: ${Constants.system.white}; width: 100%; height: 100%; background-size: cover; background-position: 50% 50%; @media (max-width: ${Constants.sizes.mobile}px) { border-radius: 8px; } `; const STYLES_IMAGE = css` background-color: ${Constants.system.white}; display: block; pointer-events: none; transition: 200ms ease all; max-width: 100%; max-height: 100%; `; const STYLES_ENTITY = css` position: relative; height: 100%; width: 100%; border: 1px solid ${Constants.semantic.bgLight}; background-color: ${Constants.system.white}; font-size: 24px; display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; pointer-events: none; padding: 8px; font-size: 0.9rem; `; const STYLES_TITLE = css` width: calc(100% - 32px); margin-top: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: ${Constants.semantic.textGray}; font-size: 16px; font-family: ${Constants.font.medium}; `; const STYLES_BLUR_CONTAINER = css` width: 100%; height: 100%; overflow: hidden; `; export default class SlateMediaObjectPreview extends React.Component { static defaultProps = { charCap: 70, }; state = { showImage: false, error: false, }; componentDidMount = () => { this.setImage(); }; componentDidUpdate = (prevProps) => { if (prevProps.coverImage?.cid !== this.props.coverImage?.cid) { this.setImage(); } }; setImage = () => { let type = this.props.file.type; let coverImage = this.props.file.coverImage; let url; if (type && Validations.isPreviewableImage(type)) { url = Strings.getURLfromCID(this.props.file.cid); } else if (coverImage) { url = Strings.getURLfromCID(coverImage.cid); } if (url) { const img = new Image(); img.onload = () => this.setState({ showImage: true }); img.src = url; } }; render() { const file = this.props.file; const coverImage = this.props.file?.coverImage; let url = Utilities.getImageUrlIfExists(file); if (url) { const blurhash = file.blurhash && isBlurhashValid(file.blurhash).result ? file.blurhash : coverImage?.blurhash && isBlurhashValid(coverImage?.blurhash).result ? coverImage?.blurhash : null; if (this.state.error) { return (
{this.props.iconOnly ? null :
File not found
}
); } if (this.props.centeredImage) { return ( {this.state.showImage || !blurhash ? (
) : (
)} ); } return ( {this.state.showImage || !blurhash ? ( ) : ( )} ); } let name = (file.name || file.filename || "").substring(0, this.charCap); let extension = Strings.getFileExtension(file.filename); if (extension && extension.length) { extension = extension.toUpperCase(); } let element = ( ); if (Validations.isFontFile(file.filename)) { return (
{element}
} /> {name && !this.props.iconOnly && !this.props.previewPanel ? (
{name}
{extension ? (
{extension}
) : null}
) : null} ); } return (
{element}
{!this.props.iconOnly && !this.props.previewPanel ? (
{name}
{extension ? (
{extension}
) : null}
) : null}
); } }