()
| 5 | import { useEffect, useRef } from "react"; |
| 6 | |
| 7 | export function BoundingBoxOverlay() { |
| 8 | const [response] = useAtom(responseAtom); |
| 9 | const [activeHoverBox, setActiverHoverBox] = useAtom(activerHoverBoxAtom); |
| 10 | const bbWrapperRef = useRef<HTMLDivElement | null>(null); |
| 11 | |
| 12 | const boxes = parseBoundingBoxes(response); |
| 13 | |
| 14 | // remove duplicates |
| 15 | const seen = new Set(); |
| 16 | const uniqueBoxes = boxes.filter((box) => { |
| 17 | const key = box.text + box.numbers.join(","); |
| 18 | if (seen.has(key)) { |
| 19 | return false; |
| 20 | } |
| 21 | seen.add(key); |
| 22 | return true; |
| 23 | }); |
| 24 | |
| 25 | const formatted: { |
| 26 | name: string; |
| 27 | coords: { x: number; y: number; width: number; height: number }; |
| 28 | }[] = uniqueBoxes.map((box) => { |
| 29 | const coords = box.numbers; |
| 30 | return { |
| 31 | name: box.text, |
| 32 | // convert from ["y_min", "x_min", "y_max", "x_max"]; |
| 33 | // convert from 1000x1000 space to percentage |
| 34 | coords: { |
| 35 | x: coords[1] / 1000, |
| 36 | y: coords[0] / 1000, |
| 37 | width: coords[3] / 1000 - coords[1] / 1000, |
| 38 | height: coords[2] / 1000 - coords[0] / 1000, |
| 39 | }, |
| 40 | }; |
| 41 | }); |
| 42 | |
| 43 | useEffect(() => { |
| 44 | // Not the most efficient way to do this but it works |
| 45 | const handleMouseMove = (e: MouseEvent) => { |
| 46 | const x = e.clientX; |
| 47 | const y = e.clientY; |
| 48 | const wrapperPosition = bbWrapperRef.current?.getBoundingClientRect(); |
| 49 | if ( |
| 50 | bbWrapperRef.current && |
| 51 | wrapperPosition && |
| 52 | x > wrapperPosition.left && |
| 53 | x < wrapperPosition.right && |
| 54 | y > wrapperPosition.top && |
| 55 | y < wrapperPosition.bottom |
| 56 | ) { |
| 57 | const rawBoxes = document.querySelectorAll(".bb"); |
| 58 | const positions = [...rawBoxes].map((box: Element) => { |
| 59 | const rect = (box as HTMLElement).getBoundingClientRect(); |
| 60 | return { |
| 61 | left: rect.left, |
| 62 | right: rect.right, |
| 63 | top: rect.top, |
| 64 | bottom: rect.bottom, |
nothing calls this directly
no test coverage detected