diff --git a/app/components/post_body/post_body.js b/app/components/post_body/post_body.js
index a288ebb0b..006f797d3 100644
--- a/app/components/post_body/post_body.js
+++ b/app/components/post_body/post_body.js
@@ -21,7 +21,6 @@ import PostBodyAdditionalContent from 'app/components/post_body_additional_conte
import {emptyFunction} from 'app/utils/general';
import {getMarkdownTextStyles, getMarkdownBlockStyles} from 'app/utils/markdown';
-import {extractFirstLink} from 'app/utils/url';
import {makeStyleSheetFromTheme} from 'app/utils/theme';
import Reactions from 'app/components/reactions';
@@ -67,20 +66,6 @@ class PostBody extends PureComponent {
toggleSelected: emptyFunction
};
- constructor(props) {
- super(props);
-
- this.state = {
- link: extractFirstLink(props.message)
- };
- }
-
- componentWillReceiveProps(nextProps) {
- if (nextProps.message !== this.props.message) {
- this.setState({link: extractFirstLink(nextProps.message)});
- }
- }
-
handleHideUnderlay = () => {
this.props.toggleSelected(false);
};
@@ -241,17 +226,14 @@ class PostBody extends PureComponent {
>
{messageComponent}
- {Boolean(this.state.link) &&
- }
{this.renderFileAttachments()}
@@ -266,17 +248,14 @@ class PostBody extends PureComponent {
cancelText={formatMessage({id: 'channel_modal.cancel', defaultMessage: 'Cancel'})}
>
{messageComponent}
- {Boolean(this.state.link) &&
- }
{this.renderFileAttachments()}
{hasReactions && }
diff --git a/app/components/post_body_additional_content/index.js b/app/components/post_body_additional_content/index.js
index 95590ae6d..4229087a3 100644
--- a/app/components/post_body_additional_content/index.js
+++ b/app/components/post_body_additional_content/index.js
@@ -14,21 +14,42 @@ import {getBool} from 'mattermost-redux/selectors/entities/preferences';
import {ViewTypes} from 'app/constants';
import {getDimensions} from 'app/selectors/device';
import {getTheme} from 'app/selectors/preferences';
+import {extractFirstLink} from 'app/utils/url';
import PostBodyAdditionalContent from './post_body_additional_content';
-function mapStateToProps(state, ownProps) {
- const config = getConfig(state);
- const previewsEnabled = getBool(state, Preferences.CATEGORY_ADVANCED_SETTINGS, `${ViewTypes.FEATURE_TOGGLE_PREFIX}${ViewTypes.EMBED_PREVIEW}`);
+function makeGetFirstLink() {
+ let link;
+ let lastMessage;
- return {
- ...ownProps,
- ...getDimensions(state),
- config,
- openGraphData: getOpenGraphMetadataForUrl(state, ownProps.link),
- showLinkPreviews: previewsEnabled && config.EnableLinkPreviews === 'true',
- theme: getTheme(state)
+ return (message) => {
+ if (message !== lastMessage) {
+ link = extractFirstLink(message);
+ lastMessage = message;
+ }
+
+ return link;
};
}
-export default connect(mapStateToProps)(PostBodyAdditionalContent);
+function makeMapStateToProps() {
+ const getFirstLink = makeGetFirstLink();
+
+ return function mapStateToProps(state, ownProps) {
+ const config = getConfig(state);
+ const previewsEnabled = getBool(state, Preferences.CATEGORY_ADVANCED_SETTINGS, `${ViewTypes.FEATURE_TOGGLE_PREFIX}${ViewTypes.EMBED_PREVIEW}`);
+ const link = getFirstLink(ownProps.message);
+
+ return {
+ ...ownProps,
+ ...getDimensions(state),
+ config,
+ link,
+ openGraphData: getOpenGraphMetadataForUrl(state, link),
+ showLinkPreviews: previewsEnabled && config.EnableLinkPreviews === 'true',
+ theme: getTheme(state)
+ };
+ };
+}
+
+export default connect(makeMapStateToProps)(PostBodyAdditionalContent);
diff --git a/app/components/post_body_additional_content/post_body_additional_content.js b/app/components/post_body_additional_content/post_body_additional_content.js
index 6612aebcd..b92e1c0cf 100644
--- a/app/components/post_body_additional_content/post_body_additional_content.js
+++ b/app/components/post_body_additional_content/post_body_additional_content.js
@@ -120,23 +120,25 @@ export default class PostBodyAdditionalContent extends PureComponent {
const {link} = this.props;
const {linkLoaded} = this.state;
- let imageUrl;
- if (isImageLink(link)) {
- imageUrl = link;
- } else if (isYoutubeLink(link)) {
- const videoId = youTubeVideoId(link);
- imageUrl = `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`;
- }
+ if (link) {
+ let imageUrl;
+ if (isImageLink(link)) {
+ imageUrl = link;
+ } else if (isYoutubeLink(link)) {
+ const videoId = youTubeVideoId(link);
+ imageUrl = `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`;
+ }
- if (imageUrl && !linkLoaded) {
- Image.getSize(imageUrl, (width, height) => {
- if (!this.mounted) {
- return;
- }
+ if (imageUrl && !linkLoaded) {
+ Image.getSize(imageUrl, (width, height) => {
+ if (!this.mounted) {
+ return;
+ }
- const dimensions = this.calculateDimensions(width, height);
- this.setState({...dimensions, linkLoaded: true});
- }, () => null);
+ const dimensions = this.calculateDimensions(width, height);
+ this.setState({...dimensions, linkLoaded: true});
+ }, () => null);
+ }
}
};
@@ -244,14 +246,20 @@ export default class PostBodyAdditionalContent extends PureComponent {
render() {
const {link, openGraphData} = this.props;
const {linkLoaded, linkLoadError} = this.state;
- const isYouTube = isYoutubeLink(link);
- const isImage = isImageLink(link);
- const isOpenGraph = Boolean(openGraphData && openGraphData.description);
+ let isYouTube = false;
+ let isImage = false;
+ let isOpenGraph = false;
- if (((isImage && !isOpenGraph) || isYouTube) && !linkLoadError) {
- const embed = this.generateToggleableEmbed(isImage, isYouTube);
- if (embed && (linkLoaded || isYouTube)) {
- return embed;
+ if (link) {
+ isYouTube = isYoutubeLink(link);
+ isImage = isImageLink(link);
+ isOpenGraph = Boolean(openGraphData && openGraphData.description);
+
+ if (((isImage && !isOpenGraph) || isYouTube) && !linkLoadError) {
+ const embed = this.generateToggleableEmbed(isImage, isYouTube);
+ if (embed && (linkLoaded || isYouTube)) {
+ return embed;
+ }
}
}