import * as React from "react"; import { connect } from "react-redux"; import { State } from "../../store/reducer"; import { stopLoading } from "../../store/actions"; import { Spinner } from "./spinner"; import { ResponsiveImage } from "./responsive-image"; interface ImageProps { loading: boolean; src: string; onLoad: () => void; } class Image extends React.Component { private imgRef = React.createRef(); constructor(props) { super(props); } private checkImgLoad() { if (this.imgRef.current.complete) { this.props.onLoad(); } else { console.log("Loading is not complete"); } } componentDidMount() { this.checkImgLoad(); } componentDidUpdate() { this.checkImgLoad(); } render() { return ( { console.error(event); this.checkImgLoad(); }} src={this.props.src} style={{ visibility: this.props.loading ? "hidden" : "visible" }} alt="Diagram preview" /> {this.props.loading && } ); } } const mapStateToProps = (state: State): Partial => { let isDev = process.env.NODE_ENV === "development"; const host = isDev ? "https://arkit.pro" : ""; return { src: `${host}${state.url}/raw`, loading: !state.loaded }; }; const mapDispatchToProps = (dispatch): Partial => { return { onLoad() { dispatch(stopLoading()); } }; }; export const Preview = connect( mapStateToProps, mapDispatchToProps )(Image);