ohif-viewer/platform/ui/src/components/StudyBrowser/StudyBrowser.jsx

190 lines
5.8 KiB
React
Raw Normal View History

import React, { useState } from 'react';
2020-04-21 20:12:46 +02:00
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { ButtonGroup, Button, StudyItem, ThumbnailList } from '@ohif/ui';
2020-04-14 00:35:32 +02:00
const buttonClasses = 'text-white text-base border-none bg-black p-2 min-w-18';
const activeButtonClasses = 'bg-primary-main';
2020-05-15 19:07:21 +02:00
const getInitialActiveTab = tabs => {
2020-04-21 20:12:46 +02:00
return tabs && tabs[0] && tabs[0].name;
};
2020-05-15 19:07:21 +02:00
const getTrackedSeries = displaySets => {
let trackedSeries = 0;
2020-05-15 19:07:21 +02:00
displaySets.forEach(displaySet => {
if (displaySet.isTracked) {
trackedSeries++;
}
});
return trackedSeries;
};
2020-05-18 17:21:57 +02:00
const StudyBrowser = ({
tabs,
activeTabName,
onSetTabActive,
onClickStudy,
onClickThumbnail,
}) => {
const [tabActive, setTabActive] = useState(
activeTabName || getInitialActiveTab(tabs)
);
const [studyActive, setStudyActive] = useState(null);
2020-05-15 14:29:27 +02:00
const [thumbnailActive, setThumbnailActive] = useState(null);
2020-04-21 20:12:46 +02:00
const getTabContent = () => {
2020-05-15 19:07:21 +02:00
const tabData = tabs.find(tab => tab.name === tabActive);
2020-04-21 20:12:46 +02:00
if (!tabData || !tabData.studies || !Array.isArray(tabData.studies)) {
return;
}
return tabData.studies.map(
({
studyInstanceUid,
2020-05-15 14:29:27 +02:00
date,
description,
numInstances,
2020-04-21 20:12:46 +02:00
modalities,
displaySets,
2020-04-21 20:12:46 +02:00
}) => {
const isActive = studyActive === studyInstanceUid;
return (
<React.Fragment key={studyInstanceUid}>
<StudyItem
2020-05-15 14:29:27 +02:00
date={date}
description={description}
numInstances={numInstances}
2020-04-21 20:12:46 +02:00
modalities={modalities}
trackedSeries={getTrackedSeries(displaySets)}
2020-04-21 20:12:46 +02:00
isActive={isActive}
onClick={() => {
setStudyActive(isActive ? null : studyInstanceUid);
2020-05-15 10:05:40 +02:00
if (onClickStudy) {
onClickStudy(studyInstanceUid);
}
2020-04-21 20:12:46 +02:00
}}
/>
2020-05-15 10:05:40 +02:00
{isActive && displaySets && (
<ThumbnailList
2020-05-18 17:21:57 +02:00
dragData={{ type: 'displayset' }}
2020-05-15 10:05:40 +02:00
thumbnails={displaySets}
thumbnailActive={thumbnailActive}
2020-05-15 19:07:21 +02:00
onThumbnailClick={thumbnailId => {
setThumbnailActive(
thumbnailId === thumbnailActive ? null : thumbnailId
);
2020-05-15 10:05:40 +02:00
2020-05-15 19:07:21 +02:00
if (onClickThumbnail) {
// TODO: what is thumbnailId? Should pass display set instead
onClickThumbnail(thumbnailId);
2020-05-15 10:05:40 +02:00
}
2020-05-15 19:07:21 +02:00
}}
2020-05-15 10:05:40 +02:00
/>
2020-04-21 20:12:46 +02:00
)}
</React.Fragment>
);
}
);
};
return (
<React.Fragment>
<div className="flex flex-row items-center justify-center border-b w-100 h-16 border-secondary-light p-4 bg-primary-dark">
<ButtonGroup
variant="outlined"
color="inherit"
className="border border-secondary-light rounded-md"
>
2020-05-15 19:07:21 +02:00
{tabs.map(tab => {
2020-04-21 20:12:46 +02:00
const { name, label } = tab;
const isActive = tabActive === name;
return (
<Button
2020-04-21 20:12:46 +02:00
key={name}
className={classnames(
buttonClasses,
isActive && activeButtonClasses
)}
2020-04-14 00:35:32 +02:00
size="initial"
onClick={() => {
2020-04-21 20:12:46 +02:00
setTabActive(name);
setStudyActive(null);
if (onSetTabActive) {
2020-05-18 17:21:57 +02:00
onSetTabActive(name);
}
}}
>
2020-04-21 20:12:46 +02:00
{label}
</Button>
);
})}
</ButtonGroup>
</div>
<div className="flex flex-col flex-1 overflow-auto invisible-scrollbar">
2020-04-21 20:12:46 +02:00
{getTabContent()}
</div>
</React.Fragment>
);
};
2020-04-21 20:12:46 +02:00
StudyBrowser.propTypes = {
2020-05-15 10:05:40 +02:00
onClickStudy: PropTypes.func,
onClickThumbnail: PropTypes.func,
2020-04-21 20:12:46 +02:00
tabs: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
studies: PropTypes.arrayOf(
PropTypes.shape({
studyInstanceUid: PropTypes.string.isRequired,
date: PropTypes.string,
numInstances: PropTypes.number,
2020-05-15 14:29:27 +02:00
modalities: PropTypes.string,
2020-04-21 20:12:46 +02:00
description: PropTypes.string,
2020-05-18 17:21:57 +02:00
// These apply to each thumbnail; we should set Thumbnails
// As children and kill the <ThumbnailList> component to
// make it easier to set this prop.
// Do much less nesting so we have a bit more control.
/**
* Data the thumbnail should expose to a receiving drop target. Use a matching
* `dragData.type` to identify which targets can receive this draggable item.
* If this is not set, drag-n-drop will be disabled for this thumbnail.
*
* Ref: https://react-dnd.github.io/react-dnd/docs/api/use-drag#specification-object-members
*/
dragData: PropTypes.shape({
/** Must match the "type" a dropTarget expects */
type: PropTypes.string.isRequired,
}),
2020-04-21 20:12:46 +02:00
displaySets: PropTypes.arrayOf(
PropTypes.shape({
2020-05-15 19:07:21 +02:00
displaySetInstanceUID: PropTypes.string.isRequired,
2020-04-21 20:12:46 +02:00
imageSrc: PropTypes.string,
imageAltText: PropTypes.string,
seriesDate: PropTypes.string,
seriesNumber: PropTypes.number,
numInstances: PropTypes.number,
description: PropTypes.string,
componentType: PropTypes.oneOf([
'thumbnail',
'thumbnailTracked',
'thumbnailNoImage',
]).isRequired,
isTracked: PropTypes.bool,
viewportIdentificator: PropTypes.string,
})
),
})
).isRequired,
})
),
};
export default StudyBrowser;