2019-12-05 23:31:01 +01:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-14 04:41:41 +02:00
|
|
|
import dis from '../dispatcher/dispatcher';
|
2020-03-26 17:31:31 +01:00
|
|
|
import {pendingVerificationRequestForUser} from '../verification';
|
2019-12-05 23:31:01 +01:00
|
|
|
import {Store} from 'flux/utils';
|
2020-07-30 16:53:54 +02:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2020-07-18 13:04:48 +02:00
|
|
|
import {RightPanelPhases, RIGHT_PANEL_PHASES_NO_ARGS} from "./RightPanelStorePhases";
|
2020-07-18 13:08:20 +02:00
|
|
|
import {ActionPayload} from "../dispatcher/payloads";
|
|
|
|
import {Action} from '../dispatcher/actions';
|
2020-07-30 16:53:54 +02:00
|
|
|
import { SettingLevel } from "../settings/SettingLevel";
|
2019-12-05 23:31:01 +01:00
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
interface RightPanelStoreState {
|
2019-12-05 23:31:01 +01:00
|
|
|
// Whether or not to show the right panel at all. We split out rooms and groups
|
|
|
|
// because they're different flows for the user to follow.
|
2020-07-18 13:04:48 +02:00
|
|
|
showRoomPanel: boolean;
|
|
|
|
showGroupPanel: boolean;
|
2019-12-05 23:31:01 +01:00
|
|
|
|
|
|
|
// The last phase (screen) the right panel was showing
|
2020-07-18 13:04:48 +02:00
|
|
|
lastRoomPhase: RightPanelPhases;
|
|
|
|
lastGroupPhase: RightPanelPhases;
|
2019-12-06 23:04:44 +01:00
|
|
|
|
2020-08-28 11:52:04 +02:00
|
|
|
previousPhase?: RightPanelPhases;
|
|
|
|
|
2019-12-06 23:04:44 +01:00
|
|
|
// Extra information about the last phase
|
2020-07-18 13:04:48 +02:00
|
|
|
lastRoomPhaseParams: {[key: string]: any};
|
|
|
|
}
|
|
|
|
|
|
|
|
const INITIAL_STATE: RightPanelStoreState = {
|
|
|
|
showRoomPanel: SettingsStore.getValue("showRightPanelInRoom"),
|
|
|
|
showGroupPanel: SettingsStore.getValue("showRightPanelInGroup"),
|
|
|
|
lastRoomPhase: SettingsStore.getValue("lastRightPanelPhaseForRoom"),
|
|
|
|
lastGroupPhase: SettingsStore.getValue("lastRightPanelPhaseForGroup"),
|
2019-12-06 23:04:44 +01:00
|
|
|
lastRoomPhaseParams: {},
|
2019-12-05 23:31:01 +01:00
|
|
|
};
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
const GROUP_PHASES = [
|
|
|
|
RightPanelPhases.GroupMemberList,
|
|
|
|
RightPanelPhases.GroupRoomList,
|
|
|
|
RightPanelPhases.GroupRoomInfo,
|
|
|
|
RightPanelPhases.GroupMemberInfo,
|
|
|
|
];
|
2019-12-05 23:31:01 +01:00
|
|
|
|
2020-03-20 01:18:24 +01:00
|
|
|
const MEMBER_INFO_PHASES = [
|
2020-07-18 13:04:48 +02:00
|
|
|
RightPanelPhases.RoomMemberInfo,
|
|
|
|
RightPanelPhases.Room3pidMemberInfo,
|
|
|
|
RightPanelPhases.EncryptionPanel,
|
2020-03-20 01:18:24 +01:00
|
|
|
];
|
|
|
|
|
2019-12-05 23:31:01 +01:00
|
|
|
/**
|
|
|
|
* A class for tracking the state of the right panel between layouts and
|
|
|
|
* sessions.
|
|
|
|
*/
|
2020-07-18 13:08:20 +02:00
|
|
|
export default class RightPanelStore extends Store<ActionPayload> {
|
2020-07-18 13:04:48 +02:00
|
|
|
private static instance: RightPanelStore;
|
|
|
|
private state: RightPanelStoreState;
|
2019-12-05 23:31:01 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
// Initialise state
|
2020-07-18 13:04:48 +02:00
|
|
|
this.state = INITIAL_STATE;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get isOpenForRoom(): boolean {
|
2020-07-18 13:04:48 +02:00
|
|
|
return this.state.showRoomPanel;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get isOpenForGroup(): boolean {
|
2020-07-18 13:04:48 +02:00
|
|
|
return this.state.showGroupPanel;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
get roomPanelPhase(): RightPanelPhases {
|
|
|
|
return this.state.lastRoomPhase;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
get groupPanelPhase(): RightPanelPhases {
|
|
|
|
return this.state.lastGroupPhase;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:52:04 +02:00
|
|
|
get previousPhase(): RightPanelPhases | null {
|
|
|
|
return RIGHT_PANEL_PHASES_NO_ARGS.includes(this.state.previousPhase) ? this.state.previousPhase : null;
|
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
get visibleRoomPanelPhase(): RightPanelPhases {
|
2019-12-06 07:28:06 +01:00
|
|
|
return this.isOpenForRoom ? this.roomPanelPhase : null;
|
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
get visibleGroupPanelPhase(): RightPanelPhases {
|
2019-12-06 07:28:06 +01:00
|
|
|
return this.isOpenForGroup ? this.groupPanelPhase : null;
|
|
|
|
}
|
|
|
|
|
2019-12-06 23:04:44 +01:00
|
|
|
get roomPanelPhaseParams(): any {
|
2020-07-18 13:04:48 +02:00
|
|
|
return this.state.lastRoomPhaseParams || {};
|
2019-12-06 23:04:44 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
private setState(newState: Partial<RightPanelStoreState>) {
|
|
|
|
this.state = Object.assign(this.state, newState);
|
2019-12-06 07:28:06 +01:00
|
|
|
|
|
|
|
SettingsStore.setValue(
|
|
|
|
"showRightPanelInRoom",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 13:04:48 +02:00
|
|
|
this.state.showRoomPanel,
|
2019-12-06 07:28:06 +01:00
|
|
|
);
|
|
|
|
SettingsStore.setValue(
|
|
|
|
"showRightPanelInGroup",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 13:04:48 +02:00
|
|
|
this.state.showGroupPanel,
|
2019-12-06 07:28:06 +01:00
|
|
|
);
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
if (RIGHT_PANEL_PHASES_NO_ARGS.includes(this.state.lastRoomPhase)) {
|
2019-12-06 07:28:06 +01:00
|
|
|
SettingsStore.setValue(
|
|
|
|
"lastRightPanelPhaseForRoom",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 13:04:48 +02:00
|
|
|
this.state.lastRoomPhase,
|
2019-12-06 07:28:06 +01:00
|
|
|
);
|
|
|
|
}
|
2020-07-18 13:04:48 +02:00
|
|
|
if (RIGHT_PANEL_PHASES_NO_ARGS.includes(this.state.lastGroupPhase)) {
|
2019-12-06 07:28:06 +01:00
|
|
|
SettingsStore.setValue(
|
|
|
|
"lastRightPanelPhaseForGroup",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 13:04:48 +02:00
|
|
|
this.state.lastGroupPhase,
|
2019-12-06 07:28:06 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-05 23:31:01 +01:00
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
__onDispatch(payload: ActionPayload) {
|
2020-03-20 01:18:24 +01:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'view_room':
|
|
|
|
case 'view_group':
|
|
|
|
// Reset to the member list if we're viewing member info
|
2020-07-18 13:04:48 +02:00
|
|
|
if (MEMBER_INFO_PHASES.includes(this.state.lastRoomPhase)) {
|
|
|
|
this.setState({lastRoomPhase: RightPanelPhases.RoomMemberList, lastRoomPhaseParams: {}});
|
2020-03-20 01:18:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the same for groups
|
2020-07-18 13:04:48 +02:00
|
|
|
if (this.state.lastGroupPhase === RightPanelPhases.GroupMemberInfo) {
|
|
|
|
this.setState({lastGroupPhase: RightPanelPhases.GroupMemberList});
|
2020-03-20 01:18:24 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
case Action.SetRightPanelPhase: {
|
2020-03-26 17:31:31 +01:00
|
|
|
let targetPhase = payload.phase;
|
|
|
|
let refireParams = payload.refireParams;
|
|
|
|
// redirect to EncryptionPanel if there is an ongoing verification request
|
2020-07-18 13:04:48 +02:00
|
|
|
if (targetPhase === RightPanelPhases.RoomMemberInfo && payload.refireParams) {
|
2020-03-26 17:31:31 +01:00
|
|
|
const {member} = payload.refireParams;
|
|
|
|
const pendingRequest = pendingVerificationRequestForUser(member);
|
|
|
|
if (pendingRequest) {
|
2020-07-18 13:04:48 +02:00
|
|
|
targetPhase = RightPanelPhases.EncryptionPanel;
|
2020-03-26 17:31:31 +01:00
|
|
|
refireParams = {
|
|
|
|
verificationRequest: pendingRequest,
|
|
|
|
member,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 13:04:48 +02:00
|
|
|
if (!RightPanelPhases[targetPhase]) {
|
2020-03-20 01:18:24 +01:00
|
|
|
console.warn(`Tried to switch right panel to unknown phase: ${targetPhase}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GROUP_PHASES.includes(targetPhase)) {
|
2020-07-18 13:04:48 +02:00
|
|
|
if (targetPhase === this.state.lastGroupPhase) {
|
|
|
|
this.setState({
|
|
|
|
showGroupPanel: !this.state.showGroupPanel,
|
2020-08-28 11:52:04 +02:00
|
|
|
previousPhase: null,
|
2020-03-20 01:18:24 +01:00
|
|
|
});
|
|
|
|
} else {
|
2020-07-18 13:04:48 +02:00
|
|
|
this.setState({
|
2020-03-20 01:18:24 +01:00
|
|
|
lastGroupPhase: targetPhase,
|
|
|
|
showGroupPanel: true,
|
2020-08-28 11:52:04 +02:00
|
|
|
previousPhase: this.state.lastGroupPhase,
|
2020-03-20 01:18:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-18 13:04:48 +02:00
|
|
|
if (targetPhase === this.state.lastRoomPhase && !refireParams) {
|
|
|
|
this.setState({
|
|
|
|
showRoomPanel: !this.state.showRoomPanel,
|
2020-08-28 11:52:04 +02:00
|
|
|
previousPhase: null,
|
2020-03-20 01:18:24 +01:00
|
|
|
});
|
|
|
|
} else {
|
2020-07-18 13:04:48 +02:00
|
|
|
this.setState({
|
2020-03-20 01:18:24 +01:00
|
|
|
lastRoomPhase: targetPhase,
|
|
|
|
showRoomPanel: true,
|
2020-03-26 17:31:31 +01:00
|
|
|
lastRoomPhaseParams: refireParams || {},
|
2020-08-28 11:52:04 +02:00
|
|
|
previousPhase: this.state.lastRoomPhase,
|
2020-03-20 01:18:24 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let things like the member info panel actually open to the right member.
|
|
|
|
dis.dispatch({
|
2020-07-18 13:04:48 +02:00
|
|
|
action: Action.AfterRightPanelPhaseChange,
|
2020-03-20 01:18:24 +01:00
|
|
|
phase: targetPhase,
|
2020-03-26 17:31:31 +01:00
|
|
|
...(refireParams || {}),
|
2020-03-20 01:18:24 +01:00
|
|
|
});
|
|
|
|
break;
|
2019-12-10 17:58:44 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 13:04:48 +02:00
|
|
|
case Action.ToggleRightPanel:
|
2020-03-20 01:18:24 +01:00
|
|
|
if (payload.type === "room") {
|
2020-07-18 13:04:48 +02:00
|
|
|
this.setState({ showRoomPanel: !this.state.showRoomPanel });
|
2020-03-20 01:18:24 +01:00
|
|
|
} else { // group
|
2020-07-18 13:04:48 +02:00
|
|
|
this.setState({ showGroupPanel: !this.state.showGroupPanel });
|
2020-03-20 01:18:24 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getSharedInstance(): RightPanelStore {
|
2020-07-18 13:04:48 +02:00
|
|
|
if (!RightPanelStore.instance) {
|
|
|
|
RightPanelStore.instance = new RightPanelStore();
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
2020-07-18 13:04:48 +02:00
|
|
|
return RightPanelStore.instance;
|
2019-12-05 23:31:01 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-28 11:46:29 +02:00
|
|
|
|
|
|
|
window.mxRightPanelStore = RightPanelStore.getSharedInstance();
|