2019-12-05 22:31:01 +00: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 02:41:41 +00:00
|
|
|
import dis from '../dispatcher/dispatcher';
|
2021-06-29 12:11:58 +00:00
|
|
|
import { pendingVerificationRequestForUser } from '../verification';
|
|
|
|
import { Store } from 'flux/utils';
|
2020-07-30 14:53:54 +00:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { RightPanelPhases, RIGHT_PANEL_PHASES_NO_ARGS } from "./RightPanelStorePhases";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
|
|
|
import { Action } from '../dispatcher/actions';
|
2020-07-30 14:53:54 +00:00
|
|
|
import { SettingLevel } from "../settings/SettingLevel";
|
2019-12-05 22:31:01 +00:00
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
interface RightPanelStoreState {
|
2019-12-05 22:31:01 +00: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 11:04:48 +00:00
|
|
|
showRoomPanel: boolean;
|
|
|
|
showGroupPanel: boolean;
|
2019-12-05 22:31:01 +00:00
|
|
|
|
|
|
|
// The last phase (screen) the right panel was showing
|
2020-07-18 11:04:48 +00:00
|
|
|
lastRoomPhase: RightPanelPhases;
|
|
|
|
lastGroupPhase: RightPanelPhases;
|
2019-12-06 22:04:44 +00:00
|
|
|
|
2020-08-28 09:52:04 +00:00
|
|
|
previousPhase?: RightPanelPhases;
|
|
|
|
|
2019-12-06 22:04:44 +00:00
|
|
|
// Extra information about the last phase
|
2020-07-18 11:04:48 +00: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 22:04:44 +00:00
|
|
|
lastRoomPhaseParams: {},
|
2019-12-05 22:31:01 +00:00
|
|
|
};
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
const GROUP_PHASES = [
|
|
|
|
RightPanelPhases.GroupMemberList,
|
|
|
|
RightPanelPhases.GroupRoomList,
|
|
|
|
RightPanelPhases.GroupRoomInfo,
|
|
|
|
RightPanelPhases.GroupMemberInfo,
|
|
|
|
];
|
2019-12-05 22:31:01 +00:00
|
|
|
|
2020-03-20 00:18:24 +00:00
|
|
|
const MEMBER_INFO_PHASES = [
|
2020-07-18 11:04:48 +00:00
|
|
|
RightPanelPhases.RoomMemberInfo,
|
|
|
|
RightPanelPhases.Room3pidMemberInfo,
|
|
|
|
RightPanelPhases.EncryptionPanel,
|
2020-03-20 00:18:24 +00:00
|
|
|
];
|
|
|
|
|
2019-12-05 22:31:01 +00:00
|
|
|
/**
|
|
|
|
* A class for tracking the state of the right panel between layouts and
|
|
|
|
* sessions.
|
|
|
|
*/
|
2020-07-18 11:08:20 +00:00
|
|
|
export default class RightPanelStore extends Store<ActionPayload> {
|
2020-07-18 11:04:48 +00:00
|
|
|
private static instance: RightPanelStore;
|
|
|
|
private state: RightPanelStoreState;
|
2021-07-09 13:43:07 +00:00
|
|
|
private lastRoomId: string;
|
2019-12-05 22:31:01 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
// Initialise state
|
2020-07-18 11:04:48 +00:00
|
|
|
this.state = INITIAL_STATE;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get isOpenForRoom(): boolean {
|
2020-07-18 11:04:48 +00:00
|
|
|
return this.state.showRoomPanel;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get isOpenForGroup(): boolean {
|
2020-07-18 11:04:48 +00:00
|
|
|
return this.state.showGroupPanel;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
get roomPanelPhase(): RightPanelPhases {
|
|
|
|
return this.state.lastRoomPhase;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
get groupPanelPhase(): RightPanelPhases {
|
|
|
|
return this.state.lastGroupPhase;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 09:52:04 +00:00
|
|
|
get previousPhase(): RightPanelPhases | null {
|
|
|
|
return RIGHT_PANEL_PHASES_NO_ARGS.includes(this.state.previousPhase) ? this.state.previousPhase : null;
|
|
|
|
}
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
get visibleRoomPanelPhase(): RightPanelPhases {
|
2019-12-06 06:28:06 +00:00
|
|
|
return this.isOpenForRoom ? this.roomPanelPhase : null;
|
|
|
|
}
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
get visibleGroupPanelPhase(): RightPanelPhases {
|
2019-12-06 06:28:06 +00:00
|
|
|
return this.isOpenForGroup ? this.groupPanelPhase : null;
|
|
|
|
}
|
|
|
|
|
2019-12-06 22:04:44 +00:00
|
|
|
get roomPanelPhaseParams(): any {
|
2020-07-18 11:04:48 +00:00
|
|
|
return this.state.lastRoomPhaseParams || {};
|
2019-12-06 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
private setState(newState: Partial<RightPanelStoreState>) {
|
|
|
|
this.state = Object.assign(this.state, newState);
|
2019-12-06 06:28:06 +00:00
|
|
|
|
|
|
|
SettingsStore.setValue(
|
|
|
|
"showRightPanelInRoom",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 11:04:48 +00:00
|
|
|
this.state.showRoomPanel,
|
2019-12-06 06:28:06 +00:00
|
|
|
);
|
|
|
|
SettingsStore.setValue(
|
|
|
|
"showRightPanelInGroup",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 11:04:48 +00:00
|
|
|
this.state.showGroupPanel,
|
2019-12-06 06:28:06 +00:00
|
|
|
);
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
if (RIGHT_PANEL_PHASES_NO_ARGS.includes(this.state.lastRoomPhase)) {
|
2019-12-06 06:28:06 +00:00
|
|
|
SettingsStore.setValue(
|
|
|
|
"lastRightPanelPhaseForRoom",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 11:04:48 +00:00
|
|
|
this.state.lastRoomPhase,
|
2019-12-06 06:28:06 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-18 11:04:48 +00:00
|
|
|
if (RIGHT_PANEL_PHASES_NO_ARGS.includes(this.state.lastGroupPhase)) {
|
2019-12-06 06:28:06 +00:00
|
|
|
SettingsStore.setValue(
|
|
|
|
"lastRightPanelPhaseForGroup",
|
|
|
|
null,
|
|
|
|
SettingLevel.DEVICE,
|
2020-07-18 11:04:48 +00:00
|
|
|
this.state.lastGroupPhase,
|
2019-12-06 06:28:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:31:01 +00:00
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
2021-07-19 21:43:11 +00:00
|
|
|
__onDispatch(payload: ActionPayload) { // eslint-disable-line @typescript-eslint/naming-convention
|
2020-03-20 00:18:24 +00:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'view_room':
|
2021-07-09 13:43:07 +00:00
|
|
|
if (payload.room_id === this.lastRoomId) break; // skip this transition, probably a permalink
|
|
|
|
// fallthrough
|
2020-03-20 00:18:24 +00:00
|
|
|
case 'view_group':
|
2021-07-09 13:43:07 +00:00
|
|
|
this.lastRoomId = payload.room_id;
|
2021-06-28 14:48:13 +00:00
|
|
|
|
2020-03-20 00:18:24 +00:00
|
|
|
// Reset to the member list if we're viewing member info
|
2020-07-18 11:04:48 +00:00
|
|
|
if (MEMBER_INFO_PHASES.includes(this.state.lastRoomPhase)) {
|
2021-06-29 12:11:58 +00:00
|
|
|
this.setState({ lastRoomPhase: RightPanelPhases.RoomMemberList, lastRoomPhaseParams: {} });
|
2020-03-20 00:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the same for groups
|
2020-07-18 11:04:48 +00:00
|
|
|
if (this.state.lastGroupPhase === RightPanelPhases.GroupMemberInfo) {
|
2021-06-29 12:11:58 +00:00
|
|
|
this.setState({ lastGroupPhase: RightPanelPhases.GroupMemberList });
|
2020-03-20 00:18:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
case Action.SetRightPanelPhase: {
|
2020-03-26 16:31:31 +00:00
|
|
|
let targetPhase = payload.phase;
|
|
|
|
let refireParams = payload.refireParams;
|
2021-06-24 08:39:45 +00:00
|
|
|
const allowClose = payload.allowClose ?? true;
|
2020-03-26 16:31:31 +00:00
|
|
|
// redirect to EncryptionPanel if there is an ongoing verification request
|
2020-07-18 11:04:48 +00:00
|
|
|
if (targetPhase === RightPanelPhases.RoomMemberInfo && payload.refireParams) {
|
2021-06-29 12:11:58 +00:00
|
|
|
const { member } = payload.refireParams;
|
2020-03-26 16:31:31 +00:00
|
|
|
const pendingRequest = pendingVerificationRequestForUser(member);
|
|
|
|
if (pendingRequest) {
|
2020-07-18 11:04:48 +00:00
|
|
|
targetPhase = RightPanelPhases.EncryptionPanel;
|
2020-03-26 16:31:31 +00:00
|
|
|
refireParams = {
|
|
|
|
verificationRequest: pendingRequest,
|
|
|
|
member,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 11:04:48 +00:00
|
|
|
if (!RightPanelPhases[targetPhase]) {
|
2020-03-20 00:18:24 +00:00
|
|
|
console.warn(`Tried to switch right panel to unknown phase: ${targetPhase}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GROUP_PHASES.includes(targetPhase)) {
|
2020-07-18 11:04:48 +00:00
|
|
|
if (targetPhase === this.state.lastGroupPhase) {
|
|
|
|
this.setState({
|
|
|
|
showGroupPanel: !this.state.showGroupPanel,
|
2020-08-28 09:52:04 +00:00
|
|
|
previousPhase: null,
|
2020-03-20 00:18:24 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-07-18 11:04:48 +00:00
|
|
|
this.setState({
|
2020-03-20 00:18:24 +00:00
|
|
|
lastGroupPhase: targetPhase,
|
|
|
|
showGroupPanel: true,
|
2020-08-28 09:52:04 +00:00
|
|
|
previousPhase: this.state.lastGroupPhase,
|
2020-03-20 00:18:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-24 08:39:45 +00:00
|
|
|
if (targetPhase === this.state.lastRoomPhase && !refireParams && allowClose) {
|
2020-07-18 11:04:48 +00:00
|
|
|
this.setState({
|
|
|
|
showRoomPanel: !this.state.showRoomPanel,
|
2020-08-28 09:52:04 +00:00
|
|
|
previousPhase: null,
|
2020-03-20 00:18:24 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-07-18 11:04:48 +00:00
|
|
|
this.setState({
|
2020-03-20 00:18:24 +00:00
|
|
|
lastRoomPhase: targetPhase,
|
|
|
|
showRoomPanel: true,
|
2020-03-26 16:31:31 +00:00
|
|
|
lastRoomPhaseParams: refireParams || {},
|
2020-08-28 09:52:04 +00:00
|
|
|
previousPhase: this.state.lastRoomPhase,
|
2020-03-20 00:18:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let things like the member info panel actually open to the right member.
|
|
|
|
dis.dispatch({
|
2020-07-18 11:04:48 +00:00
|
|
|
action: Action.AfterRightPanelPhaseChange,
|
2020-03-20 00:18:24 +00:00
|
|
|
phase: targetPhase,
|
2020-03-26 16:31:31 +00:00
|
|
|
...(refireParams || {}),
|
2020-03-20 00:18:24 +00:00
|
|
|
});
|
|
|
|
break;
|
2019-12-10 16:58:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 11:04:48 +00:00
|
|
|
case Action.ToggleRightPanel:
|
2020-03-20 00:18:24 +00:00
|
|
|
if (payload.type === "room") {
|
2020-07-18 11:04:48 +00:00
|
|
|
this.setState({ showRoomPanel: !this.state.showRoomPanel });
|
2020-03-20 00:18:24 +00:00
|
|
|
} else { // group
|
2020-07-18 11:04:48 +00:00
|
|
|
this.setState({ showGroupPanel: !this.state.showGroupPanel });
|
2020-03-20 00:18:24 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getSharedInstance(): RightPanelStore {
|
2020-07-18 11:04:48 +00:00
|
|
|
if (!RightPanelStore.instance) {
|
|
|
|
RightPanelStore.instance = new RightPanelStore();
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
2020-07-18 11:04:48 +00:00
|
|
|
return RightPanelStore.instance;
|
2019-12-05 22:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-28 09:46:29 +00:00
|
|
|
|
|
|
|
window.mxRightPanelStore = RightPanelStore.getSharedInstance();
|