/* eslint-disable import/no-unresolved */ import React, { useRef, useEffect, useState } from 'react'; import LazyLoad, { forceCheck } from 'react-lazyload'; import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch'; export const Image = function Image({ component, height, properties, styles, fireEvent, width, parentId = null, dataCy, }) { const { source, loadingState, alternativeText, zoomButtons, rotateButton } = properties; const { visibility, disabledState, borderType, backgroundColor, padding, imageFit, boxShadow } = styles; const { definition: { events }, } = component; const hasOnClickEvent = events.some((event) => event.eventId === 'onClick'); const widgetVisibility = visibility ?? true; const imageRef = useRef(null); const [imageOffset, setImageOffset] = useState(0); const [rotation, setRotation] = useState(0); const [zoomReset, setZoomReset] = useState(false); function Placeholder() { return
; } useEffect(() => { if (parentId === null) { setImageOffset(computeOffset()); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [imageRef]); useEffect(() => { setRotation(0); setZoomReset(true); }, [source]); function computeOffset() { if (imageRef.current) { const clientRect = imageRef.current.getBoundingClientRect(); const layoutHeightWithOffset = clientRect.top + clientRect.height; return layoutHeightWithOffset - document.documentElement.clientHeight; } return 0; } useEffect(() => { forceCheck(); }, [visibility]); const rotateImage = () => setRotation((prevValue) => (prevValue === 270 ? 0 : prevValue + 90)); const renderImage = () => ( fireEvent('onClick')} alt={alternativeText} width={width} /> ); const resetZoom = (resetTransform) => { setZoomReset(false); resetTransform(); }; const renderImageContainer = () => { return ( <> {loadingState === true ? (
) : zoomButtons ? ( {({ zoomIn, zoomOut, resetTransform }) => ( <> {zoomReset && resetZoom(resetTransform)} {renderImage()} {zoomButtons && (
{rotateButton && ( )}
)} )}
) : ( <> {rotateButton && (
)} {renderImage()} )} ); }; return (
{imageRef.current && parentId === null ? ( 0 ? imageOffset : 0} height={height} placeholder={} debounce={500} scrollContainer={'.canvas-container'} > {renderImageContainer()} ) : ( imageRef.current && renderImageContainer() )}
); };