import React, { useEffect, useState } from 'react'; import PropTypes from 'prop-types'; import { PanelSection } from '../../components'; import SegmentationConfig from './SegmentationConfig'; import SegmentationDropDownRow from './SegmentationDropDownRow'; import NoSegmentationRow from './NoSegmentationRow'; import AddSegmentRow from './AddSegmentRow'; import SegmentationGroupSegment from './SegmentationGroupSegment'; const SegmentationGroupTable = ({ segmentations, // segmentation initial config segmentationConfig, // UI show/hide disableEditing, showAddSegmentation, showAddSegment, showDeleteSegment, // segmentation/segment handlers onSegmentationAdd, onSegmentationEdit, onSegmentationClick, onSegmentationDelete, onSegmentationDownload, onSegmentationDownloadRTSS, storeSegmentation, // segment handlers onSegmentClick, onSegmentAdd, onSegmentDelete, onSegmentEdit, onToggleSegmentationVisibility, onToggleSegmentVisibility, onToggleSegmentLock, onSegmentColorClick, // segmentation config handlers setFillAlpha, setFillAlphaInactive, setOutlineWidthActive, setOutlineOpacityActive, setRenderFill, setRenderInactiveSegmentations, setRenderOutline, }) => { const [isConfigOpen, setIsConfigOpen] = useState(false); const [activeSegmentationId, setActiveSegmentationId] = useState(null); const onActiveSegmentationChange = segmentationId => { onSegmentationClick(segmentationId); setActiveSegmentationId(segmentationId); }; useEffect(() => { // find the first active segmentation to set let activeSegmentationIdToSet = segmentations?.find(segmentation => segmentation.isActive)?.id; // If there is no active segmentation, set the first one to be active if (!activeSegmentationIdToSet && segmentations?.length > 0) { activeSegmentationIdToSet = segmentations[0].id; } // If there is no segmentation, set the active segmentation to null if (segmentations?.length === 0) { activeSegmentationIdToSet = null; } setActiveSegmentationId(activeSegmentationIdToSet); }, [segmentations]); const activeSegmentation = segmentations?.find( segmentation => segmentation.id === activeSegmentationId ); return (
setIsConfigOpen(isOpen => !isOpen), }, ] } > {isConfigOpen && ( )}
{segmentations?.length === 0 ? (
{showAddSegmentation && !disableEditing && ( )}
) : (
{!disableEditing && showAddSegment && ( onSegmentAdd(activeSegmentationId)} /> )}
)}
{activeSegmentation && (
{activeSegmentation?.segments?.map(segment => { if (!segment) { return null; } const { segmentIndex, color, label, isVisible, isLocked } = segment; return (
); })}
)}
); }; SegmentationGroupTable.propTypes = { segmentations: PropTypes.arrayOf( PropTypes.shape({ id: PropTypes.string.isRequired, isActive: PropTypes.bool.isRequired, segments: PropTypes.arrayOf( PropTypes.shape({ segmentIndex: PropTypes.number.isRequired, color: PropTypes.array.isRequired, label: PropTypes.string.isRequired, isVisible: PropTypes.bool.isRequired, isLocked: PropTypes.bool.isRequired, }) ), }) ), segmentationConfig: PropTypes.object.isRequired, disableEditing: PropTypes.bool, showAddSegmentation: PropTypes.bool, showAddSegment: PropTypes.bool, showDeleteSegment: PropTypes.bool, onSegmentationAdd: PropTypes.func.isRequired, onSegmentationEdit: PropTypes.func.isRequired, onSegmentationClick: PropTypes.func.isRequired, onSegmentationDelete: PropTypes.func.isRequired, onSegmentationDownload: PropTypes.func.isRequired, onSegmentationDownloadRTSS: PropTypes.func.isRequired, storeSegmentation: PropTypes.func.isRequired, onSegmentClick: PropTypes.func.isRequired, onSegmentAdd: PropTypes.func.isRequired, onSegmentDelete: PropTypes.func.isRequired, onSegmentEdit: PropTypes.func.isRequired, onToggleSegmentationVisibility: PropTypes.func.isRequired, onToggleSegmentVisibility: PropTypes.func.isRequired, onToggleSegmentLock: PropTypes.func.isRequired, onSegmentColorClick: PropTypes.func.isRequired, setFillAlpha: PropTypes.func.isRequired, setFillAlphaInactive: PropTypes.func.isRequired, setOutlineWidthActive: PropTypes.func.isRequired, setOutlineOpacityActive: PropTypes.func.isRequired, setRenderFill: PropTypes.func.isRequired, setRenderInactiveSegmentations: PropTypes.func.isRequired, setRenderOutline: PropTypes.func.isRequired, }; SegmentationGroupTable.defaultProps = { segmentations: [], disableEditing: false, showAddSegmentation: true, showAddSegment: true, showDeleteSegment: true, onSegmentationAdd: () => {}, onSegmentationEdit: () => {}, onSegmentationClick: () => {}, onSegmentationDelete: () => {}, onSegmentationDownload: () => {}, onSemgnetationDownloadRTSS: () => {}, storeSegmentation: () => {}, onSegmentClick: () => {}, onSegmentAdd: () => {}, onSegmentDelete: () => {}, onSegmentEdit: () => {}, onToggleSegmentationVisibility: () => {}, onToggleSegmentVisibility: () => {}, onToggleSegmentLock: () => {}, onSegmentColorClick: () => {}, setFillAlpha: () => {}, setFillAlphaInactive: () => {}, setOutlineWidthActive: () => {}, setOutlineOpacityActive: () => {}, setRenderFill: () => {}, setRenderInactiveSegmentations: () => {}, setRenderOutline: () => {}, }; export default SegmentationGroupTable;