import { MutableRefObject, useMemo, useState } from 'react'; import { Item } from 'react-photoswipe-gallery'; import 'photoswipe/dist/photoswipe.css'; import { Flex, Text } from '../../../general'; import { BlockProps, Block } from '../Block/Block'; import { FragmentImage } from '../Bubble/fragment-lib'; type ImageBlockProps = { showLoader?: boolean; image: string; by: string; } & BlockProps; export const ImageBlock = ({ id, showLoader, image, by, variant, width = 'inherit', height, ...rest }: ImageBlockProps) => { const [isLoaded, setIsLoaded] = useState(false); const [naturalWidth, setNaturalWidth] = useState(320); const [naturalHeight, setNaturalHeight] = useState(427); const parsedHeight = useMemo( () => (height ? typeof height === 'number' ? `${height}px` : height : '100%') as string, [height] ); const parsedWidth = useMemo( () => (width ? typeof width === 'number' ? `${width}px` : width : 'fit-content') as string, [width] ); return ( {({ ref, open }) => ( } loading="eager" {...(showLoader && { isSkeleton: !isLoaded })} src={image} height={parsedHeight} width={parsedWidth} draggable={false} onLoad={() => { if (showLoader) { setIsLoaded(true); } const curr = ref && (ref.current as HTMLImageElement); if (curr && curr.naturalWidth) setNaturalWidth(curr.naturalWidth); if (curr && curr.naturalHeight) setNaturalHeight(curr.naturalHeight); }} onClick={open} /> )} {by && ( {by} )} ); };