import React,{useEffect,useRef,useState} from 'react'; import {Expand,SplitSquareHorizontal,X,ZoomIn,ZoomOut} from 'lucide-react'; import {clampPan,overlayClip,nextZoom,ZOOM_MIN,ZOOM_MAX} from './viewerMath.mjs'; // Zoom enlarges the photo; drag (mouse or finger) moves around it. In compare // mode the divider and the split always line up, at any zoom and position. export default function InspectionViewer({record,url,heatmap,onComparisonUsed}){ const [compare,setCompare]=useState(false),[position,setPosition]=useState(50),[zoom,setZoom]=useState(1),[pan,setPan]=useState({x:0,y:0}),[expanded,setExpanded]=useState(false); const dialog=useRef(null),drag=useRef(null); useEffect(()=>{setZoom(1);setPan({x:0,y:0});setPosition(50)},[record.id]); useEffect(()=>{if(expanded)dialog.current?.showModal();else dialog.current?.close()},[expanded]); const movable=zoom>1&&!compare; function changeZoom(direction){const next=nextZoom(zoom,direction);setZoom(next);setPan(value=>clampPan(value,next))} function resetView(){setZoom(1);setPan({x:0,y:0})} function start(e){ if(!movable)return; e.currentTarget.setPointerCapture?.(e.pointerId); drag.current={x:e.clientX,y:e.clientY,pan,w:e.currentTarget.clientWidth||1,h:e.currentTarget.clientHeight||1}; } function move(e){const d=drag.current;if(!d)return;setPan(clampPan({x:d.pan.x+(e.clientX-d.x)/d.w*100,y:d.pan.y+(e.clientY-d.y)/d.h*100},zoom))} function end(){drag.current=null} function image(){ const clip=overlayClip(position,zoom,pan.x); return
{if(zoom>1)resetView()}}>
Inspected product original {compare?Measured anomaly heatmap comparison:heatmap&&Measured anomaly localization heatmap}
{compare&&<>
ORIGINALHIGHLIGHTED AREAS{setPosition(Number(e.target.value));onComparisonUsed?.()}}/>}
; } const help=compare?'Drag the divider to compare the photo and the highlighted areas.':movable?'Drag the photo to move around. Double-click to reset.':'Use Compare images to see both side by side.'; return <>
{image()}

{help} Highlighted areas look different from normal examples. Stronger colors do not mean a more serious defect.

setExpanded(false)} onCancel={()=>setExpanded(false)}>

Inspection image ยท {record.id.slice(0,8)}

{expanded&&image()}
; }