Skip to content

Commit

Permalink
MM-11577: Back to previous channel on archive (mattermost#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
jespino authored and hmhealey committed Sep 27, 2018
1 parent c0847f9 commit 616e94a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 26 deletions.
2 changes: 2 additions & 0 deletions actions/global_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export function emitChannelClickEvent(channel) {
});

if (chan.delete_at === 0) {
const penultimate = LocalStorageStore.getPreviousChannelName(userId, teamId);
LocalStorageStore.setPenultimateChannelName(userId, teamId, penultimate);
LocalStorageStore.setPreviousChannelName(userId, teamId, chan.name);
}

Expand Down
5 changes: 4 additions & 1 deletion actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,10 @@ function handleChannelCreatedEvent(msg) {
}

function handleChannelDeletedEvent(msg) {
if (ChannelStore.getCurrentId() === msg.data.channel_id) {
const state = getState();
const config = getConfig(state);
const viewArchivedChannels = config.ExperimentalViewArchivedChannels === 'true';
if (ChannelStore.getCurrentId() === msg.data.channel_id && !viewArchivedChannels) {
const teamUrl = TeamStore.getCurrentTeamRelativeUrl();
browserHistory.push(teamUrl + '/channels/' + Constants.DEFAULT_CHANNEL);
}
Expand Down
3 changes: 2 additions & 1 deletion components/channel_header/channel_header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default class ChannelHeader extends React.Component {
Object.values(RHSStates)
),
lastViewedChannelName: PropTypes.string.isRequired,
penultimateViewedChannelName: PropTypes.string.isRequired,
enableWebrtc: PropTypes.bool.isRequired,
};

Expand Down Expand Up @@ -858,7 +859,7 @@ export default class ChannelHeader extends React.Component {
role='menuitem'
modalId={ModalIdentifiers.DELETE_CHANNEL}
dialogType={DeleteChannelModal}
dialogProps={{channel}}
dialogProps={{channel, penultimateViewedChannelName: this.props.penultimateViewedChannelName}}
>
<FormattedMessage
id='channel_header.delete'
Expand Down
8 changes: 7 additions & 1 deletion components/channel_header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {getLicense, getConfig} from 'mattermost-redux/selectors/entities/general

import {withRouter} from 'react-router-dom';

import {getLastViewedChannelName} from 'selectors/local_storage';
import {getLastViewedChannelName, getPenultimateViewedChannelName} from 'selectors/local_storage';
import {Constants} from 'utils/constants.jsx';

import {
Expand Down Expand Up @@ -50,6 +50,11 @@ function mapStateToProps(state, ownProps) {
lastViewedChannelName = Constants.DEFAULT_CHANNEL;
}

let penultimateViewedChannelName = getPenultimateViewedChannelName(state);
if (!penultimateViewedChannelName) {
penultimateViewedChannelName = Constants.DEFAULT_CHANNEL;
}

return {
channel,
channelMember: getMyChannelMember(state, ownProps.channelId),
Expand All @@ -64,6 +69,7 @@ function mapStateToProps(state, ownProps) {
enableWebrtc: config.EnableWebrtc === 'true',
isReadOnly: isCurrentChannelReadOnly(state),
lastViewedChannelName,
penultimateViewedChannelName,
};
}

Expand Down
26 changes: 5 additions & 21 deletions components/channel_view/channel_view.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,19 @@ import TutorialView from 'components/tutorial';
import {clearMarks, mark, measure, trackEvent} from 'actions/diagnostics_actions.jsx';
import FormattedMarkdownMessage from 'components/formatted_markdown_message';
import {browserHistory} from 'utils/browser_history';
import {Constants} from 'utils/constants.jsx';
import TeamStore from 'stores/team_store.jsx';

export default class ChannelView extends React.PureComponent {
static propTypes = {

/**
* ID of the channel to display
*/
channelId: PropTypes.string.isRequired,

/**
* Set if this channel is deactivated, primarily used for DMs with inactive users
*/
deactivatedChannel: PropTypes.bool.isRequired,

/**
* Object from react-router
*/
match: PropTypes.shape({
url: PropTypes.string.isRequired,
}).isRequired,

/**
* Set to show the tutorial
*/
showTutorial: PropTypes.bool.isRequired,

/**
* Whether the channel is archived
*/
channelIsArchived: PropTypes.bool.isRequired,

viewArchivedChannels: PropTypes.bool.isRequired,
lastViewedChannelName: PropTypes.string.isRequired,
};

Expand Down Expand Up @@ -111,6 +92,9 @@ export default class ChannelView extends React.PureComponent {
if (dur2 !== -1) {
trackEvent('performance', 'team_switch', {duration: Math.round(dur2)});
}
if (this.props.channelIsArchived && !this.props.viewArchivedChannels) {
browserHistory.push(`${TeamStore.getCurrentTeamRelativeUrl()}/channels/${Constants.DEFAULT_CHANNEL}`);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions components/channel_view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function mapStateToProps(state) {
const config = getConfig(state);
const enableTutorial = config.EnableTutorial === 'true';
const tutorialStep = getInt(state, Preferences.TUTORIAL_STEP, getCurrentUserId(state), TutorialSteps.FINISHED);
const viewArchivedChannels = config.ExperimentalViewArchivedChannels === 'true';

let lastViewedChannelName = getLastViewedChannelName(state);
if (!lastViewedChannelName) {
Expand All @@ -43,6 +44,7 @@ function mapStateToProps(state) {
showTutorial: enableTutorial && tutorialStep <= TutorialSteps.INTRO_SCREENS,
channelIsArchived: channel ? channel.delete_at !== 0 : false,
lastViewedChannelName,
viewArchivedChannels,
};
}

Expand Down
6 changes: 5 additions & 1 deletion components/delete_channel_modal/delete_channel_modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class DeleteChannelModal extends React.PureComponent {
currentTeamDetails: PropTypes.object.isRequired,

canViewArchivedChannels: PropTypes.bool,
penultimateViewedChannelName: PropTypes.string.isRequired,

actions: PropTypes.shape({

Expand All @@ -52,7 +53,10 @@ export default class DeleteChannelModal extends React.PureComponent {
if (this.props.channel.id.length !== Constants.CHANNEL_ID_LENGTH) {
return;
}
browserHistory.push('/' + this.props.currentTeamDetails.name + '/channels/' + Constants.DEFAULT_CHANNEL);
if (!this.props.canViewArchivedChannels) {
const {penultimateViewedChannelName} = this.props;
browserHistory.push('/' + this.props.currentTeamDetails.name + '/channels/' + penultimateViewedChannelName);
}
this.props.actions.deleteChannel(this.props.channel.id);
this.onHide();
}
Expand Down
7 changes: 7 additions & 0 deletions selectors/local_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export const getLastViewedChannelName = (state) => {
return localStorageStore.getPreviousChannelName(userId, teamId);
};

export const getPenultimateViewedChannelName = (state) => {
const userId = getCurrentUserId(state);
const teamId = getCurrentTeamId(state);

return localStorageStore.getPenultimateChannelName(userId, teamId);
};

// getLastViewedChannelNameByTeamName combines data from the Redux store and localStorage to return
// the url to the previously selected channel, returning the path to the default channel if none
// exists.
Expand Down
9 changes: 9 additions & 0 deletions stores/local_storage_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Constants} from 'utils/constants.jsx';

const getPreviousTeamIdKey = (userId) => ['user_prev_team', userId].join(':');
const getPreviousChannelNameKey = (userId, teamId) => ['user_team_prev_channel', userId, teamId].join(':');
const getPenultimateChannelNameKey = (userId, teamId) => ['user_team_penultimate_channel', userId, teamId].join(':');

// LocalStorageStore exposes an interface for accessing entries in the localStorage.
//
Expand All @@ -19,6 +20,14 @@ class LocalStorageStoreClass {
localStorage.setItem(getPreviousChannelNameKey(userId, teamId), channelName);
}

getPenultimateChannelName(userId, teamId) {
return localStorage.getItem(getPenultimateChannelNameKey(userId, teamId)) || Constants.DEFAULT_CHANNEL;
}

setPenultimateChannelName(userId, teamId, channelName) {
localStorage.setItem(getPenultimateChannelNameKey(userId, teamId), channelName);
}

getPreviousTeamId(userId) {
return localStorage.getItem(getPreviousTeamIdKey(userId));
}
Expand Down
1 change: 1 addition & 0 deletions tests/components/channel_header.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('components/ChannelHeader', () => {
currentUser: {},
enableWebrtc: false,
lastViewedChannelName: '',
penultimateViewedChannelName: '',
};

const populatedProps = {
Expand Down
3 changes: 2 additions & 1 deletion tests/components/delete_channel_modal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('components/delete_channel_modal', () => {
deleteChannel: emptyFunction,
},
onHide: emptyFunction,
penultimateViewedChannelName: 'my-prev-channel',
};

test('should match snapshot for delete_channel_modal', () => {
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('components/delete_channel_modal', () => {

expect(actions.deleteChannel).toHaveBeenCalledTimes(1);
expect(actions.deleteChannel).toHaveBeenCalledWith(props.channel.id);
expect(browserHistory.push).toHaveBeenCalledWith('/mattermostDev/channels/town-square');
expect(browserHistory.push).toHaveBeenCalledWith('/mattermostDev/channels/my-prev-channel');
expect(wrapper.state('show')).toEqual(false);
});

Expand Down

0 comments on commit 616e94a

Please sign in to comment.