/* global cornerstone */ import './ImageThumbnail.styl'; import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import ViewportErrorIndicator from '../../viewer/ViewportErrorIndicator'; import ViewportLoadingIndicator from '../../viewer/ViewportLoadingIndicator'; // TODO: How should we have this component depend on Cornerstone? // - Passed in as a prop? // - Set as external dependency? // - Pass in the entire load and render function as a prop? //import cornerstone from 'cornerstone-core'; export default class ImageThumbnail extends PureComponent { static propTypes = { imageSrc: PropTypes.string, imageId: PropTypes.string, error: PropTypes.bool.isRequired, width: PropTypes.number.isRequired, height: PropTypes.number.isRequired, stackPercentComplete: PropTypes.number.isRequired, }; static defaultProps = { error: false, stackPercentComplete: 0, width: 217, height: 123, }; constructor(props) { super(props); this.canvas = React.createRef(); this.state = { loading: this.shouldRenderToCanvas(), }; } shouldRenderToCanvas() { return this.props.imageId && !this.props.imageSrc; } componentDidMount() { if (this.shouldRenderToCanvas()) { cornerstone .loadAndCacheImage(this.props.imageId) .then(image => { cornerstone.renderToCanvas(this.canvas.current, image); this.setState({ loading: false, }); }) .catch(error => { this.setState({ loading: false, error: true, }); throw new Error(error); }); } } render() { let loadingOrError; if (this.props.error) { loadingOrError = ; } else if (this.state.loading) { loadingOrError = ; } const showStackLoadingProgressBar = this.props.stackPercentComplete !== undefined; return (
{this.shouldRenderToCanvas() ? ( ) : ( {''} )}
{loadingOrError} {showStackLoadingProgressBar && (
)} {this.state.loading && (
)}
); } }