Skip to content

Commit

Permalink
PLT-3638: Fix re-opening of collapsed preview on refresh (mattermost#…
Browse files Browse the repository at this point in the history
…7301)

In this change, user action for toggling the preview is stored in the browser localstorage. Hence If there us a preview visibility value is present
in the localstorage, that will be given the preference compared to the overall preview setting.
  • Loading branch information
pruthvip authored and hmhealey committed Aug 29, 2017
1 parent 257edc9 commit 6a312b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 11 additions & 2 deletions webapp/components/post_view/post_body_additional_content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import YoutubeVideo from 'components/youtube_video';

import Constants from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
import BrowserStore from 'stores/browser_store.jsx';

import React from 'react';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class PostBodyAdditionalContent extends React.PureComponent {
this.handleLinkLoaded = this.handleLinkLoaded.bind(this);

this.state = {
embedVisible: props.previewCollapsed.startsWith('false'),
embedVisible: PostBodyAdditionalContent.isEmbedVisible(props),
link: Utils.extractFirstLink(props.post.message),
linkLoadError: false,
linkLoaded: false
Expand All @@ -62,7 +63,7 @@ export default class PostBodyAdditionalContent extends React.PureComponent {
componentWillReceiveProps(nextProps) {
if (nextProps.previewCollapsed !== this.props.previewCollapsed || nextProps.post.message !== this.props.post.message) {
this.setState({
embedVisible: nextProps.previewCollapsed.startsWith('false'),
embedVisible: PostBodyAdditionalContent.isEmbedVisible(nextProps),
link: Utils.extractFirstLink(nextProps.post.message)
}, () => {
// check the availability of the image link
Expand All @@ -72,6 +73,9 @@ export default class PostBodyAdditionalContent extends React.PureComponent {
}

toggleEmbedVisibility() {
// save the taggle info in the localstorage
BrowserStore.setItem(`isVisible-${this.props.post.id}`, !this.state.embedVisible);

this.setState((prevState) => {
return {embedVisible: !prevState.embedVisible};
});
Expand Down Expand Up @@ -260,4 +264,9 @@ export default class PostBodyAdditionalContent extends React.PureComponent {

return this.props.message;
}

static isEmbedVisible(props) {
// check first in localstorage, if not present, consider previewCollapsed from the parent component
return BrowserStore.getItem(`isVisible-${props.post.id}`, props.previewCollapsed.startsWith('false'));
}
}
7 changes: 4 additions & 3 deletions webapp/stores/browser_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as Utils from 'utils/utils.jsx';
import {Constants, ErrorPageTypes} from 'utils/constants.jsx';

function getPrefix() {
if (global.window.mm_current_user_id) {
return global.window.mm_current_user_id + '_';
if (global.mm_user) {
return global.mm_user.id + '_';
}

console.warn('BrowserStore tried to operate without user present'); //eslint-disable-line no-console
Expand Down Expand Up @@ -50,6 +50,7 @@ class BrowserStoreClass {

getGlobalItem(name, defaultValue = null) {
var result = null;

try {
if (this.isLocalStorageSupported()) {
result = JSON.parse(localStorage.getItem(name));
Expand All @@ -60,7 +61,7 @@ class BrowserStoreClass {
result = null;
}

if (!result) {
if (typeof result === 'undefined' || result === null) {
result = defaultValue;
}

Expand Down

0 comments on commit 6a312b2

Please sign in to comment.