From 9b50fb855350ea1e747ee946ab3f955430abeb75 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 23 Feb 2016 16:28:16 -0500 Subject: Refactored embedded image/video code and prevented them from being displayed on deleted posts --- web/react/components/post_image.jsx | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 web/react/components/post_image.jsx (limited to 'web/react/components/post_image.jsx') diff --git a/web/react/components/post_image.jsx b/web/react/components/post_image.jsx new file mode 100644 index 000000000..b35f6d1de --- /dev/null +++ b/web/react/components/post_image.jsx @@ -0,0 +1,62 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +export default class PostImageEmbed extends React.Component { + constructor(props) { + super(props); + + this.state = { + loaded: false + }; + } + + componentWillMount() { + this.loadImg(this.props.link); + } + + componentWillReceiveProps(nextProps) { + if (nextProps.link !== this.props.link) { + this.setState({ + loaded: false + }); + } + } + + componentDidUpdate(prevProps) { + if (!this.state.loaded && prevProps.link !== this.props.link) { + this.loadImg(this.props.link); + } + } + + loadImg(src) { + const img = new Image(); + img.onload = () => { + this.setState({ + loaded: true + }); + }; + img.src = src; + } + + render() { + if (!this.state.loaded) { + return ( + + ); + } + + return ( + + ); + } +} + +PostImageEmbed.propTypes = { + link: React.PropTypes.string.isRequired +}; -- cgit v1.2.3-1-g7c22 From b2e333e83cf3a9693744fa2baab937bb3ac8be28 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 23 Feb 2016 16:35:15 -0500 Subject: Improved handling when an embedded image cannot be loaded --- web/react/components/post_image.jsx | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'web/react/components/post_image.jsx') diff --git a/web/react/components/post_image.jsx b/web/react/components/post_image.jsx index b35f6d1de..da4a25794 100644 --- a/web/react/components/post_image.jsx +++ b/web/react/components/post_image.jsx @@ -5,8 +5,12 @@ export default class PostImageEmbed extends React.Component { constructor(props) { super(props); + this.handleLoadComplete = this.handleLoadComplete.bind(this); + this.handleLoadError = this.handleLoadError.bind(this); + this.state = { - loaded: false + loaded: false, + errored: false }; } @@ -17,7 +21,8 @@ export default class PostImageEmbed extends React.Component { componentWillReceiveProps(nextProps) { if (nextProps.link !== this.props.link) { this.setState({ - loaded: false + loaded: false, + errored: false }); } } @@ -30,15 +35,29 @@ export default class PostImageEmbed extends React.Component { loadImg(src) { const img = new Image(); - img.onload = () => { - this.setState({ - loaded: true - }); - }; + img.onload = this.handleLoadComplete; + img.onerror = this.handleLoadError; img.src = src; } + handleLoadComplete() { + this.setState({ + loaded: true + }); + } + + handleLoadError() { + this.setState({ + errored: true, + loaded: true + }); + } + render() { + if (this.state.errored) { + return null; + } + if (!this.state.loaded) { return (