2021-05-28 20:32:48 +02:00
|
|
|
import React, { useEffect, useState, useRef } from 'react';
|
2021-07-02 22:54:26 +02:00
|
|
|
import { useParams, useLocation } from 'react-router';
|
2022-04-06 19:28:42 +02:00
|
|
|
|
2020-05-18 05:40:09 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2020-05-22 02:34:53 +02:00
|
|
|
// TODO: DicomMetadataStore should be injected?
|
2021-05-28 20:32:48 +02:00
|
|
|
import { DicomMetadataStore } from '@ohif/core';
|
2020-06-03 19:12:13 +02:00
|
|
|
import { DragAndDropProvider, ImageViewerProvider } from '@ohif/ui';
|
2020-06-29 18:58:36 +02:00
|
|
|
import { useQuery } from '@hooks';
|
2020-05-18 05:27:31 +02:00
|
|
|
import ViewportGrid from '@components/ViewportGrid';
|
2020-05-07 20:02:51 +02:00
|
|
|
import Compose from './Compose';
|
|
|
|
|
|
2022-09-07 17:59:11 +02:00
|
|
|
/**
|
|
|
|
|
* Initialize the route.
|
|
|
|
|
*
|
|
|
|
|
* @param props.servicesManager to read services from
|
|
|
|
|
* @param props.studyInstanceUIDs for a list of studies to read
|
|
|
|
|
* @param props.dataSource to read the data from
|
|
|
|
|
* @returns array of subscriptions to cancel
|
|
|
|
|
*/
|
2022-09-21 05:40:26 +02:00
|
|
|
function defaultRouteInit(
|
|
|
|
|
{ servicesManager, studyInstanceUIDs, dataSource },
|
|
|
|
|
hangingProtocol
|
|
|
|
|
) {
|
2021-05-28 20:32:48 +02:00
|
|
|
const {
|
|
|
|
|
DisplaySetService,
|
|
|
|
|
HangingProtocolService,
|
|
|
|
|
} = servicesManager.services;
|
|
|
|
|
|
|
|
|
|
const unsubscriptions = [];
|
|
|
|
|
const {
|
|
|
|
|
unsubscribe: instanceAddedUnsubscribe,
|
|
|
|
|
} = DicomMetadataStore.subscribe(
|
|
|
|
|
DicomMetadataStore.EVENTS.INSTANCES_ADDED,
|
2022-09-07 17:59:11 +02:00
|
|
|
function({ StudyInstanceUID, SeriesInstanceUID, madeInClient = false }) {
|
2021-05-28 20:32:48 +02:00
|
|
|
const seriesMetadata = DicomMetadataStore.getSeries(
|
|
|
|
|
StudyInstanceUID,
|
|
|
|
|
SeriesInstanceUID
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
DisplaySetService.makeDisplaySets(seriesMetadata.instances, madeInClient);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
unsubscriptions.push(instanceAddedUnsubscribe);
|
|
|
|
|
|
2022-09-07 17:59:11 +02:00
|
|
|
const allRetrieves = studyInstanceUIDs.map(StudyInstanceUID =>
|
|
|
|
|
dataSource.retrieve.series.metadata({ StudyInstanceUID })
|
2021-05-28 20:32:48 +02:00
|
|
|
);
|
|
|
|
|
|
2022-09-07 17:59:11 +02:00
|
|
|
// The hanging protocol matching service is fairly expensive to run multiple
|
|
|
|
|
// times, and doesn't allow partial matches to be made (it will simply fail
|
|
|
|
|
// to display anything if a required match fails), so we wait here until all metadata
|
|
|
|
|
// is retrieved (which will synchronously trigger the display set creation)
|
|
|
|
|
// until we run the hanging protocol matching service.
|
|
|
|
|
|
|
|
|
|
Promise.allSettled(allRetrieves).then(() => {
|
|
|
|
|
const displaySets = DisplaySetService.getActiveDisplaySets();
|
|
|
|
|
|
|
|
|
|
if (!displaySets || !displaySets.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const studyMap = {};
|
|
|
|
|
|
|
|
|
|
// Prior studies don't quite work properly yet, but the studies list
|
|
|
|
|
// is at least being generated and passed in.
|
|
|
|
|
const studies = displaySets.reduce((prev, curr) => {
|
|
|
|
|
const { StudyInstanceUID } = curr;
|
|
|
|
|
if (!studyMap[StudyInstanceUID]) {
|
|
|
|
|
const study = DicomMetadataStore.getStudy(StudyInstanceUID);
|
|
|
|
|
studyMap[StudyInstanceUID] = study;
|
|
|
|
|
prev.push(study);
|
|
|
|
|
}
|
|
|
|
|
return prev;
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// The assumption is that the display set at position 0 is the first
|
|
|
|
|
// study being displayed, and is thus the "active" study.
|
|
|
|
|
const activeStudy = studies[0];
|
|
|
|
|
|
2022-09-21 05:40:26 +02:00
|
|
|
// run the hanging protocol matching on the displaySets with the predefined
|
|
|
|
|
// hanging protocol in the mode configuration
|
|
|
|
|
HangingProtocolService.run(
|
|
|
|
|
{ studies, activeStudy, displaySets },
|
|
|
|
|
hangingProtocol
|
|
|
|
|
);
|
2021-06-15 16:19:45 +02:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 20:32:48 +02:00
|
|
|
return unsubscriptions;
|
|
|
|
|
}
|
2020-07-24 12:51:57 +02:00
|
|
|
|
2020-05-16 11:05:50 +02:00
|
|
|
export default function ModeRoute({
|
|
|
|
|
mode,
|
|
|
|
|
dataSourceName,
|
|
|
|
|
extensionManager,
|
2020-05-18 17:21:57 +02:00
|
|
|
servicesManager,
|
2022-07-27 18:39:04 +02:00
|
|
|
commandsManager,
|
2020-07-03 19:18:59 +02:00
|
|
|
hotkeysManager,
|
2020-05-16 11:05:50 +02:00
|
|
|
}) {
|
2020-05-20 06:49:38 +02:00
|
|
|
// Parse route params/querystring
|
2021-07-02 22:54:26 +02:00
|
|
|
const location = useLocation();
|
2020-05-20 06:49:38 +02:00
|
|
|
const query = useQuery();
|
2021-05-28 20:32:48 +02:00
|
|
|
const params = useParams();
|
2020-05-16 11:05:50 +02:00
|
|
|
|
2021-05-28 20:32:48 +02:00
|
|
|
const [studyInstanceUIDs, setStudyInstanceUIDs] = useState();
|
|
|
|
|
|
|
|
|
|
const [refresh, setRefresh] = useState(false);
|
|
|
|
|
const layoutTemplateData = useRef(false);
|
|
|
|
|
const locationRef = useRef(null);
|
|
|
|
|
const isMounted = useRef(false);
|
|
|
|
|
|
|
|
|
|
if (location !== locationRef.current) {
|
|
|
|
|
layoutTemplateData.current = null;
|
|
|
|
|
locationRef.current = location;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 14:32:54 +02:00
|
|
|
const {
|
|
|
|
|
DisplaySetService,
|
|
|
|
|
HangingProtocolService: hangingProtocolService,
|
|
|
|
|
} = servicesManager.services;
|
2021-06-23 20:41:27 +02:00
|
|
|
|
2022-09-21 05:40:26 +02:00
|
|
|
const { extensions, sopClassHandlers, hotkeys, hangingProtocol } = mode;
|
2020-06-05 17:36:11 +02:00
|
|
|
|
|
|
|
|
if (dataSourceName === undefined) {
|
|
|
|
|
dataSourceName = extensionManager.defaultDataSourceName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extensionManager.setActiveDataSource(dataSourceName);
|
|
|
|
|
|
2020-07-03 19:18:59 +02:00
|
|
|
const dataSources = extensionManager.getActiveDataSource();
|
2020-06-05 17:36:11 +02:00
|
|
|
|
2020-07-03 19:18:59 +02:00
|
|
|
// Only handling one instance of the datasource type (E.g. one DICOMWeb server)
|
2020-05-16 11:05:50 +02:00
|
|
|
const dataSource = dataSources[0];
|
2020-07-03 19:18:59 +02:00
|
|
|
// Only handling one route per mode for now
|
2020-05-20 06:49:38 +02:00
|
|
|
const route = mode.routes[0];
|
2020-05-16 11:05:50 +02:00
|
|
|
|
2020-05-09 20:23:43 +02:00
|
|
|
// For each extension, look up their context modules
|
2020-05-11 18:56:46 +02:00
|
|
|
// TODO: move to extension manager.
|
2020-05-09 20:23:43 +02:00
|
|
|
let contextModules = [];
|
2022-04-06 19:28:42 +02:00
|
|
|
|
|
|
|
|
Object.keys(extensions).forEach(extensionId => {
|
2020-05-09 20:23:43 +02:00
|
|
|
const allRegisteredModuleIds = Object.keys(extensionManager.modulesMap);
|
|
|
|
|
const moduleIds = allRegisteredModuleIds.filter(id =>
|
|
|
|
|
id.includes(`${extensionId}.contextModule.`)
|
|
|
|
|
);
|
2020-05-07 16:55:59 +02:00
|
|
|
|
2020-05-09 20:23:43 +02:00
|
|
|
if (!moduleIds || !moduleIds.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const modules = moduleIds.map(extensionManager.getModuleEntry);
|
|
|
|
|
contextModules = contextModules.concat(modules);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const contextModuleProviders = contextModules.map(a => a.provider);
|
|
|
|
|
const CombinedContextProvider = ({ children }) =>
|
|
|
|
|
Compose({ components: contextModuleProviders, children });
|
2020-05-09 10:57:52 +02:00
|
|
|
|
2020-05-15 19:07:21 +02:00
|
|
|
function ViewportGridWithDataSource(props) {
|
|
|
|
|
return ViewportGrid({ ...props, dataSource });
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 20:32:48 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
// Preventing state update for unmounted component
|
|
|
|
|
isMounted.current = true;
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted.current = false;
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Todo: this should not be here, data source should not care about params
|
|
|
|
|
const initializeDataSource = async (params, query) => {
|
2021-06-15 16:19:45 +02:00
|
|
|
const studyInstanceUIDs = await dataSource.initialize({
|
2021-05-28 20:32:48 +02:00
|
|
|
params,
|
|
|
|
|
query,
|
|
|
|
|
});
|
|
|
|
|
setStudyInstanceUIDs(studyInstanceUIDs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
initializeDataSource(params, query);
|
|
|
|
|
return () => {
|
|
|
|
|
layoutTemplateData.current = null;
|
|
|
|
|
};
|
|
|
|
|
}, [location]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const retrieveLayoutData = async () => {
|
|
|
|
|
const layoutData = await route.layoutTemplate({
|
|
|
|
|
location,
|
|
|
|
|
servicesManager,
|
|
|
|
|
studyInstanceUIDs,
|
|
|
|
|
});
|
|
|
|
|
if (isMounted.current) {
|
|
|
|
|
layoutTemplateData.current = layoutData;
|
|
|
|
|
setRefresh(!refresh);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
if (studyInstanceUIDs?.length && studyInstanceUIDs[0] !== undefined) {
|
|
|
|
|
retrieveLayoutData();
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
layoutTemplateData.current = null;
|
|
|
|
|
};
|
|
|
|
|
}, [studyInstanceUIDs]);
|
|
|
|
|
|
2020-06-29 18:58:36 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!hotkeys) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hotkeysManager.setDefaultHotKeys(hotkeys);
|
2021-08-19 14:05:33 +02:00
|
|
|
|
|
|
|
|
const userPreferredHotkeys = JSON.parse(
|
|
|
|
|
localStorage.getItem('hotkey-definitions')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (userPreferredHotkeys?.length) {
|
|
|
|
|
hotkeysManager.setHotkeys(userPreferredHotkeys);
|
|
|
|
|
} else {
|
|
|
|
|
hotkeysManager.setHotkeys(hotkeys);
|
|
|
|
|
}
|
2020-06-29 18:58:36 +02:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
hotkeysManager.destroy();
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-05-18 17:21:57 +02:00
|
|
|
useEffect(() => {
|
2021-05-28 20:32:48 +02:00
|
|
|
if (!layoutTemplateData.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-24 12:51:57 +02:00
|
|
|
// TODO: For some reason this is running before the Providers
|
2022-07-27 18:39:04 +02:00
|
|
|
// are calling setServiceImplementation
|
|
|
|
|
// TODO -> iterate through services.
|
2020-07-24 12:51:57 +02:00
|
|
|
|
2020-07-03 19:18:59 +02:00
|
|
|
// Extension
|
2021-05-28 20:32:48 +02:00
|
|
|
|
|
|
|
|
// Add SOPClassHandlers to a new SOPClassManager.
|
|
|
|
|
DisplaySetService.init(extensionManager, sopClassHandlers);
|
|
|
|
|
|
2022-07-27 18:39:04 +02:00
|
|
|
extensionManager.onModeEnter({
|
|
|
|
|
servicesManager,
|
|
|
|
|
extensionManager,
|
|
|
|
|
commandsManager,
|
|
|
|
|
});
|
2022-10-05 14:32:54 +02:00
|
|
|
// Sets the active hanging protocols - if hangingProtocol is undefined,
|
|
|
|
|
// resets to default. Done before the onModeEnter to allow the onModeEnter
|
|
|
|
|
// to perform custom hanging protocol actions
|
|
|
|
|
hangingProtocolService.setActiveProtocols(hangingProtocol);
|
2022-07-27 18:39:04 +02:00
|
|
|
mode?.onModeEnter({ servicesManager, extensionManager, commandsManager });
|
2021-05-28 20:32:48 +02:00
|
|
|
|
2022-10-05 14:32:54 +02:00
|
|
|
|
2021-05-28 20:32:48 +02:00
|
|
|
const setupRouteInit = async () => {
|
|
|
|
|
if (route.init) {
|
2022-09-21 05:40:26 +02:00
|
|
|
return await route.init(
|
|
|
|
|
{
|
|
|
|
|
servicesManager,
|
|
|
|
|
extensionManager,
|
|
|
|
|
hotkeysManager,
|
|
|
|
|
studyInstanceUIDs,
|
|
|
|
|
dataSource,
|
|
|
|
|
},
|
|
|
|
|
hangingProtocol
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return defaultRouteInit(
|
|
|
|
|
{
|
2021-05-28 20:32:48 +02:00
|
|
|
servicesManager,
|
|
|
|
|
studyInstanceUIDs,
|
|
|
|
|
dataSource,
|
2022-09-21 05:40:26 +02:00
|
|
|
},
|
|
|
|
|
hangingProtocol
|
|
|
|
|
);
|
2021-05-28 20:32:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let unsubscriptions;
|
|
|
|
|
setupRouteInit().then(unsubs => {
|
|
|
|
|
unsubscriptions = unsubs;
|
|
|
|
|
});
|
2020-08-04 11:11:23 +02:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
extensionManager.onModeExit();
|
2020-10-06 03:42:30 +02:00
|
|
|
mode?.onModeExit({ servicesManager, extensionManager });
|
2021-05-28 20:32:48 +02:00
|
|
|
unsubscriptions.forEach(unsub => {
|
|
|
|
|
unsub();
|
|
|
|
|
});
|
2020-08-04 11:11:23 +02:00
|
|
|
};
|
2020-05-21 03:08:49 +02:00
|
|
|
}, [
|
|
|
|
|
mode,
|
|
|
|
|
dataSourceName,
|
|
|
|
|
location,
|
|
|
|
|
route,
|
|
|
|
|
servicesManager,
|
|
|
|
|
extensionManager,
|
2020-07-03 19:18:59 +02:00
|
|
|
hotkeysManager,
|
2021-05-28 20:32:48 +02:00
|
|
|
studyInstanceUIDs,
|
|
|
|
|
refresh,
|
2020-05-21 03:08:49 +02:00
|
|
|
]);
|
2020-05-18 17:21:57 +02:00
|
|
|
|
2021-05-28 20:32:48 +02:00
|
|
|
const renderLayoutData = props => {
|
|
|
|
|
const layoutTemplateModuleEntry = extensionManager.getModuleEntry(
|
|
|
|
|
layoutTemplateData.current.id
|
2020-05-18 17:21:57 +02:00
|
|
|
);
|
2021-05-28 20:32:48 +02:00
|
|
|
const LayoutComponent = layoutTemplateModuleEntry.component;
|
2020-05-22 02:34:53 +02:00
|
|
|
|
2021-05-28 20:32:48 +02:00
|
|
|
return <LayoutComponent {...props} />;
|
|
|
|
|
};
|
2020-07-24 12:51:57 +02:00
|
|
|
|
2020-05-09 20:23:43 +02:00
|
|
|
return (
|
2020-05-21 03:07:25 +02:00
|
|
|
<ImageViewerProvider
|
2021-05-28 20:32:48 +02:00
|
|
|
// initialState={{ StudyInstanceUIDs: StudyInstanceUIDs }}
|
|
|
|
|
StudyInstanceUIDs={studyInstanceUIDs}
|
2022-02-08 14:17:37 +01:00
|
|
|
// reducer={reducer}
|
2020-05-21 03:07:25 +02:00
|
|
|
>
|
2020-05-24 05:57:14 +02:00
|
|
|
<CombinedContextProvider>
|
|
|
|
|
<DragAndDropProvider>
|
2021-05-28 20:32:48 +02:00
|
|
|
{layoutTemplateData.current &&
|
|
|
|
|
studyInstanceUIDs?.length &&
|
|
|
|
|
studyInstanceUIDs[0] !== undefined &&
|
|
|
|
|
renderLayoutData({
|
|
|
|
|
...layoutTemplateData.current.props,
|
|
|
|
|
ViewportGridComp: ViewportGridWithDataSource,
|
|
|
|
|
})}
|
2020-05-24 05:57:14 +02:00
|
|
|
</DragAndDropProvider>
|
|
|
|
|
</CombinedContextProvider>
|
2020-05-21 03:07:25 +02:00
|
|
|
</ImageViewerProvider>
|
2020-05-09 20:23:43 +02:00
|
|
|
);
|
2020-05-07 16:55:59 +02:00
|
|
|
}
|
2020-05-18 05:40:09 +02:00
|
|
|
|
|
|
|
|
ModeRoute.propTypes = {
|
|
|
|
|
mode: PropTypes.object.isRequired,
|
|
|
|
|
dataSourceName: PropTypes.string,
|
|
|
|
|
extensionManager: PropTypes.object,
|
2020-05-21 03:07:25 +02:00
|
|
|
servicesManager: PropTypes.object,
|
2020-08-19 21:02:59 +02:00
|
|
|
hotkeysManager: PropTypes.object,
|
2020-05-18 05:40:09 +02:00
|
|
|
};
|