import React from 'react'; import type { BoundingBoxProps } from '@/types'; import { useBoundingBox } from '@/hooks/useBoundingBox'; import { segmentationLogger } from '@/utils/logger'; /** * Modern React Bounding Box component using hooks */ export const BoundingBox: React.FC = ({ image, boxes = [], options = {}, pixelSegmentation, segmentationJsonUrl, segmentationMasks, segmentationColors, segmentationTransparency, separateSegmentation = false, selectedIndex, className, onSelected, drawBox: customDrawBox, drawLabel: customDrawLabel, ...rest }) => { // Configure segmentation options const segmentationConfig = { segmentationData: pixelSegmentation, segmentationMasks, colors: segmentationColors, transparency: segmentationTransparency, separateCanvas: separateSegmentation, }; // Main hook orchestration const { mainCanvasRef, segmentationCanvasRef, selectedIndex: currentSelectedIndex, hoveredIndex, isLoading, error, selectBox, handleMouseMove, handleMouseOut, handleCanvasClick, } = useBoundingBox({ image, boxes, options, segmentation: segmentationConfig, onSelection: onSelected, }); // Sync external selectedIndex with internal state React.useEffect(() => { if (selectedIndex !== undefined && selectedIndex !== currentSelectedIndex) { selectBox(selectedIndex); } }, [selectedIndex, currentSelectedIndex, selectBox]); // Handle loading segmentation from URL React.useEffect(() => { if (segmentationJsonUrl) { fetch(segmentationJsonUrl) .then(response => response.json()) .then(data => { // Handle deepdetect format if (data.body?.predictions?.[0]?.vals) { // This would need to update the segmentation data // Implementation depends on how we want to handle this segmentationLogger.log( 'Loaded segmentation data:', data.body.predictions[0].vals ); } }) .catch(err => { segmentationLogger.error('Failed to load segmentation:', err); }); } }, [segmentationJsonUrl]); if (error) { return (
Error loading image: {error}
); } return (
{/* Main canvas for image and bounding boxes */} {/* Separate segmentation canvas (if enabled) */} {separateSegmentation && ( )} {/* Loading indicator */} {isLoading && (
Loading...
)}
); }; // Default export for backward compatibility export default BoundingBox;