Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 5308c91

Browse files
authored
Close any open modals on logout (#12777)
* Close any open modals on logout Split out from #12666 * Add test
1 parent 2fd291c commit 5308c91

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Modal.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import { IDeferred, defer, sleep } from "matrix-js-sdk/src/utils";
2222
import { TypedEventEmitter } from "matrix-js-sdk/src/matrix";
2323
import { Glass } from "@vector-im/compound-web";
2424

25-
import dis from "./dispatcher/dispatcher";
25+
import dis, { defaultDispatcher } from "./dispatcher/dispatcher";
2626
import AsyncWrapper from "./AsyncWrapper";
2727
import { Defaultize } from "./@types/common";
28+
import { ActionPayload } from "./dispatcher/payloads";
2829

2930
const DIALOG_CONTAINER_ID = "mx_Dialog_Container";
3031
const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer";
@@ -114,6 +115,21 @@ export class ModalManager extends TypedEventEmitter<ModalManagerEvent, HandlerMa
114115
return container;
115116
}
116117

118+
public constructor() {
119+
super();
120+
121+
// We never unregister this, but the Modal class is a singleton so there would
122+
// never be an opportunity to do so anyway, except in the entirely theoretical
123+
// scenario of instantiating a non-singleton instance of the Modal class.
124+
defaultDispatcher.register(this.onAction);
125+
}
126+
127+
private onAction = (payload: ActionPayload): void => {
128+
if (payload.action === "logout") {
129+
this.forceCloseAllModals();
130+
}
131+
};
132+
117133
public toggleCurrentDialogVisibility(): void {
118134
const modal = this.getCurrentModal();
119135
if (!modal) return;

test/Modal-test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
import Modal from "../src/Modal";
1818
import QuestionDialog from "../src/components/views/dialogs/QuestionDialog";
19+
import defaultDispatcher from "../src/dispatcher/dispatcher";
1920

2021
describe("Modal", () => {
2122
test("forceCloseAllModals should close all open modals", () => {
@@ -29,4 +30,28 @@ describe("Modal", () => {
2930
Modal.forceCloseAllModals();
3031
expect(Modal.hasDialogs()).toBe(false);
3132
});
33+
34+
test("open modals should be closed on logout", () => {
35+
const modal1OnFinished = jest.fn();
36+
const modal2OnFinished = jest.fn();
37+
38+
Modal.createDialog(QuestionDialog, {
39+
title: "Test dialog 1",
40+
description: "This is a test dialog",
41+
button: "Word",
42+
onFinished: modal1OnFinished,
43+
});
44+
45+
Modal.createDialog(QuestionDialog, {
46+
title: "Test dialog 2",
47+
description: "This is a test dialog",
48+
button: "Word",
49+
onFinished: modal2OnFinished,
50+
});
51+
52+
defaultDispatcher.dispatch({ action: "logout" }, true);
53+
54+
expect(modal1OnFinished).toHaveBeenCalled();
55+
expect(modal2OnFinished).toHaveBeenCalled();
56+
});
3257
});

0 commit comments

Comments
 (0)