* Remove mattermost-redux * Move mm-redux files into app/redux * Add @redux path to tsconfig.json * Fix imports * Install missing dependencies * Fix tsc errors * Fix i18n_utils test * Fix more imports * Remove redux websocket * Fix tests * Rename @redux * Apply changes from mattermost-redux PR 1103 * Remove mattermost-redux mention in template * Add missing imports * Rename app/redux/ to app/mm-redux/ * Remove test file * Fix fetching Sidebar GM profiles Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
87 lines
2.9 KiB
JavaScript
87 lines
2.9 KiB
JavaScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {connect} from 'react-redux';
|
|
import {bindActionCreators} from 'redux';
|
|
|
|
import {getRedirectLocation} from '@mm-redux/actions/general';
|
|
import {Preferences} from '@mm-redux/constants';
|
|
import {getConfig} from '@mm-redux/selectors/entities/general';
|
|
import {getOpenGraphMetadataForUrl, getExpandedLink} from '@mm-redux/selectors/entities/posts';
|
|
import {getBool, getTheme} from '@mm-redux/selectors/entities/preferences';
|
|
|
|
import {ViewTypes} from 'app/constants';
|
|
import {getDimensions} from 'app/selectors/device';
|
|
import {extractFirstLink} from 'app/utils/url';
|
|
|
|
import PostBodyAdditionalContent from './post_body_additional_content';
|
|
|
|
function makeGetFirstLink() {
|
|
let link;
|
|
let lastMessage;
|
|
|
|
return (message) => {
|
|
if (message !== lastMessage) {
|
|
link = extractFirstLink(message);
|
|
lastMessage = message;
|
|
}
|
|
|
|
return link;
|
|
};
|
|
}
|
|
|
|
function getOpenGraphData(metadata, url) {
|
|
if (!metadata || !metadata.embeds) {
|
|
return null;
|
|
}
|
|
|
|
return metadata.embeds.find((embed) => {
|
|
return embed.type === 'opengraph' && embed.url === url ? embed.data : null;
|
|
});
|
|
}
|
|
|
|
function makeMapStateToProps() {
|
|
const getFirstLink = makeGetFirstLink();
|
|
|
|
return function mapStateToProps(state, ownProps) {
|
|
const config = getConfig(state);
|
|
const link = getFirstLink(ownProps.message);
|
|
let expandedLink;
|
|
if (link) {
|
|
expandedLink = getExpandedLink(state, link);
|
|
}
|
|
|
|
// Link previews used to be an advanced settings until server version 4.4 when it was changed to be a display setting.
|
|
// We are checking both here until we bump the server requirement for the mobile apps.
|
|
const previewsEnabled = (getBool(state, Preferences.CATEGORY_ADVANCED_SETTINGS, `${ViewTypes.FEATURE_TOGGLE_PREFIX}${ViewTypes.EMBED_PREVIEW}`) ||
|
|
getBool(state, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.LINK_PREVIEW_DISPLAY, true));
|
|
|
|
const removeLinkPreview = ownProps.postProps.remove_link_preview === 'true';
|
|
|
|
let openGraphData = getOpenGraphMetadataForUrl(state, ownProps.postId, link);
|
|
if (!openGraphData) {
|
|
const data = getOpenGraphData(ownProps.metadata, link);
|
|
openGraphData = data?.data;
|
|
}
|
|
|
|
return {
|
|
...getDimensions(state),
|
|
googleDeveloperKey: config.GoogleDeveloperKey,
|
|
link,
|
|
expandedLink,
|
|
openGraphData,
|
|
showLinkPreviews: previewsEnabled && config.EnableLinkPreviews === 'true' && !removeLinkPreview,
|
|
theme: getTheme(state),
|
|
};
|
|
};
|
|
}
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
return {
|
|
actions: bindActionCreators({
|
|
getRedirectLocation,
|
|
}, dispatch),
|
|
};
|
|
}
|
|
|
|
export default connect(makeMapStateToProps, mapDispatchToProps)(PostBodyAdditionalContent);
|