diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index 85f0535851..340c4fa2ca 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -59,6 +59,7 @@ import SessionRestoreErrorDialog from "./components/views/dialogs/SessionRestore import StorageEvictedDialog from "./components/views/dialogs/StorageEvictedDialog"; import { setSentryUser } from "./sentry"; import SdkConfig from "./SdkConfig"; +import { DialogOpener } from "./utils/DialogOpener"; const HOMESERVER_URL_KEY = "mx_hs_url"; const ID_SERVER_URL_KEY = "mx_is_url"; @@ -790,6 +791,7 @@ async function startMatrixClient(startSyncing = true): Promise { TypingStore.sharedInstance().reset(); ToastStore.sharedInstance().reset(); + DialogOpener.instance.prepare(); Notifier.start(); UserActivity.sharedInstance().start(); DMRoomMap.makeShared().start(); diff --git a/src/stores/RoomViewStore.tsx b/src/stores/RoomViewStore.tsx index a346d90e26..d802845eee 100644 --- a/src/stores/RoomViewStore.tsx +++ b/src/stores/RoomViewStore.tsx @@ -44,7 +44,6 @@ import { JoinRoomPayload } from "../dispatcher/payloads/JoinRoomPayload"; import { JoinRoomReadyPayload } from "../dispatcher/payloads/JoinRoomReadyPayload"; import { JoinRoomErrorPayload } from "../dispatcher/payloads/JoinRoomErrorPayload"; import { ViewRoomErrorPayload } from "../dispatcher/payloads/ViewRoomErrorPayload"; -import RoomSettingsDialog from "../components/views/dialogs/RoomSettingsDialog"; import ErrorDialog from "../components/views/dialogs/ErrorDialog"; import { ActiveRoomChangedPayload } from "../dispatcher/payloads/ActiveRoomChangedPayload"; @@ -269,13 +268,6 @@ export class RoomViewStore extends Store { } } break; - case 'open_room_settings': { - Modal.createTrackedDialog('Room settings', '', RoomSettingsDialog, { - roomId: payload.room_id || this.state.roomId, - initialTabId: payload.initial_tab_id, - }, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true); - break; - } } } diff --git a/src/utils/DialogOpener.ts b/src/utils/DialogOpener.ts new file mode 100644 index 0000000000..3908a5af1e --- /dev/null +++ b/src/utils/DialogOpener.ts @@ -0,0 +1,54 @@ +/* +Copyright 2022 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. +*/ + +import defaultDispatcher from "../dispatcher/dispatcher"; +import { ActionPayload } from "../dispatcher/payloads"; +import Modal from "../Modal"; +import RoomSettingsDialog from "../components/views/dialogs/RoomSettingsDialog"; +import { RoomViewStore } from "../stores/RoomViewStore"; + +/** + * Auxiliary class to listen for dialog opening over the dispatcher and + * open the required dialogs. Not all dialogs run through here, but the + * ones which cause import cycles are good candidates. + */ +export class DialogOpener { + public static readonly instance = new DialogOpener(); + + private isRegistered = false; + + private constructor() { + } + + // We could do this in the constructor, but then we wouldn't have + // a function to call from Lifecycle to capture the class. + public prepare() { + if (this.isRegistered) return; + defaultDispatcher.register(this.onDispatch); + this.isRegistered = true; + } + + private onDispatch = (payload: ActionPayload) => { + switch (payload.action) { + case 'open_room_settings': + Modal.createTrackedDialog('Room settings', '', RoomSettingsDialog, { + roomId: payload.room_id || RoomViewStore.instance.getRoomId(), + initialTabId: payload.initial_tab_id, + }, /*className=*/null, /*isPriority=*/false, /*isStatic=*/true); + break; + } + }; +}