Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OHIF-2526: Display warning messages if fails to parse sr report #2543

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions platform/core/src/DICOMSR/parseDicomStructuredReport.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import dcmjs from 'dcmjs';
import classes from '../classes';

import findInstanceMetadataBySopInstanceUID from './utils/findInstanceMetadataBySopInstanceUid';

const { LogManager } = classes;

/**
* Function to parse the part10 array buffer that comes from a DICOM Structured report into measurementData
* measurementData format is a viewer specific format to be stored into the redux and consumed by other components
Expand All @@ -20,9 +23,21 @@ const parseDicomStructuredReport = (part10SRArrayBuffer, displaySets) => {
);

const { MeasurementReport } = dcmjs.adapters.Cornerstone;
const storedMeasurementByToolType = MeasurementReport.generateToolState(
dataset
);

let storedMeasurementByToolType;
try {
storedMeasurementByToolType = MeasurementReport.generateToolState(dataset);
} catch (error) {
const seriesDescription = dataset.SeriesDescription || '';
LogManager.publish(LogManager.EVENTS.OnLog, {
title: `Failed to parse ${seriesDescription} measurement report`,
type: 'warning',
message: error.message || '',
notify: true,
});
return;
}

const measurementData = {};
let measurementNumber = 0;

Expand Down
18 changes: 18 additions & 0 deletions platform/core/src/classes/LogManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import PubSub from './PubSub';

/** Log Events */
export const LogEvents = Object.freeze({
OnLog: 'onLog',
});

/**
* Log manager that implements pub/sub.
* This manager can be used to send logs across different packages
* using previously registered events.
*/
class LogManager extends PubSub {
EVENTS = LogEvents;
}

/** Singleton */
export default new LogManager();
75 changes: 75 additions & 0 deletions platform/core/src/classes/PubSub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const _subscriptions = Symbol('subscriptions');
const _lastSubscriptionId = Symbol('lastSubscriptionId');

/**
* Class to implement publish/subscribe pattern
*
* @class
* @classdesc Pub/sub mechanism
*/
export default class PubSub {
constructor() {
this[_subscriptions] = {};
this[_lastSubscriptionId] = 0;
}

/**
* Subscribe to event
*
* @param {string} eventName Event name
* @param {Function} callback Callback function
* @returns {void}
*/
subscribe(eventName, callback) {
if (eventName === undefined) {
throw new Error('Event name is required');
}

if (typeof callback !== 'function') {
throw new Error('Callback must be a function');
}

if (!this[_subscriptions].hasOwnProperty(eventName)) {
this[_subscriptions][eventName] = {};
}

const subscriptionId = `sub${this[_lastSubscriptionId]++}`;
this[_subscriptions][eventName][subscriptionId] = callback;
}

/**
* Removes a subscription
*
* @param {string} eventName Event name
* @param {Function} [callback] Callback function
* @returns {void}
*/
unsubscribe(eventName, callback) {
const callbacks = this[_subscriptions][eventName] || {};
for (let subscriptionId in callbacks) {
if (!callback) {
delete callbacks[subscriptionId];
} else if (callbacks[subscriptionId] === callback) {
delete callbacks[subscriptionId];
}
}
}

/**
* Publish event to all subscriptions
*
* @param {String} eventName Event name
* @param {any} [payload] Data that will be published
* @returns {void}
*/
publish(eventName, ...payload) {
if (eventName === undefined) {
throw new Error('Event name is required');
}

const callbacks = this[_subscriptions][eventName] || {};
for (let subscriptionId in callbacks) {
callbacks[subscriptionId](...payload);
}
}
}
4 changes: 4 additions & 0 deletions platform/core/src/classes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import CommandsManager from './CommandsManager.js';
import { DICOMFileLoadingListener } from './StudyLoadingListener';
import HotkeysManager from './HotkeysManager.js';
import ImageSet from './ImageSet';
import LogManager from './LogManager';
import PubSub from './PubSub';
import MetadataProvider from './MetadataProvider';
import OHIFError from './OHIFError.js';
import { OHIFStudyMetadataSource } from './OHIFStudyMetadataSource';
Expand Down Expand Up @@ -36,7 +38,9 @@ const classes = {
MetadataProvider,
CommandsManager,
HotkeysManager,
LogManager,
ImageSet,
PubSub,
StudyPrefetcher,
StudyLoadingListener,
StackLoadingListener,
Expand Down
17 changes: 17 additions & 0 deletions platform/ui/src/contextProviders/SnackbarProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import React, {
useEffect,
} from 'react';
import PropTypes from 'prop-types';
import { classes } from '@ohif/core';

import SnackbarContainer from '../components/snackbar/SnackbarContainer';
import SnackbarTypes from '../components/snackbar/SnackbarTypes';

const { LogManager } = classes;

const SnackbarContext = createContext(null);

export const useSnackbarContext = () => useContext(SnackbarContext);
Expand All @@ -28,6 +31,20 @@ const SnackbarProvider = ({ children, service }) => {
const [count, setCount] = useState(1);
const [snackbarItems, setSnackbarItems] = useState([]);

useEffect(() => {
const onLogHandler = ({ type, notify, title, message }) => {
if (notify) {
show({ type, title, message });
}
};

LogManager.subscribe(LogManager.EVENTS.OnLog, onLogHandler);

return () => {
LogManager.subscribe(LogManager.EVENTS.OnLog, onLogHandler);
};
}, [show]);

const show = useCallback(
options => {
if (!options || (!options.title && !options.message)) {
Expand Down