Files
ThreadNet-Web/src/stores/ModalWidgetStore.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-09-25 14:08:27 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-09-25 14:08:27 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2020-09-25 14:08:27 +01:00
*/
2025-02-05 13:25:06 +00:00
import { type IModalWidgetOpenRequestData, type IModalWidgetReturnData, type Widget } from "matrix-widget-api";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
2020-09-25 14:08:27 +01:00
import { AsyncStoreWithClient } from "./AsyncStoreWithClient";
import defaultDispatcher from "../dispatcher/dispatcher";
2025-02-05 13:25:06 +00:00
import { type ActionPayload } from "../dispatcher/payloads";
import Modal, { type IHandle, type IModal } from "../Modal";
2020-09-25 14:08:27 +01:00
import ModalWidgetDialog from "../components/views/dialogs/ModalWidgetDialog";
2021-06-29 13:11:58 +01:00
import { WidgetMessagingStore } from "./widgets/WidgetMessagingStore";
2021-10-15 16:30:53 +02:00
2020-09-25 14:08:27 +01:00
interface IState {
modal?: IModal<any>;
openedFromId?: string;
}
export class ModalWidgetStore extends AsyncStoreWithClient<IState> {
private static readonly internalInstance = (() => {
const instance = new ModalWidgetStore();
2024-10-10 15:08:43 +01:00
instance.start();
return instance;
})();
private modalInstance: IHandle<typeof ModalWidgetDialog> | null = null;
private openSourceWidgetId: string | null = null;
private openSourceWidgetRoomId: string | null = null;
2020-09-25 14:08:27 +01:00
private constructor() {
super(defaultDispatcher, {});
}
public static get instance(): ModalWidgetStore {
return ModalWidgetStore.internalInstance;
}
protected async onAction(payload: ActionPayload): Promise<any> {
// nothing
}
public canOpenModalWidget = (): boolean => {
2020-09-25 14:08:27 +01:00
return !this.modalInstance;
};
2021-02-02 14:22:28 +00:00
public openModalWidget = (
requestData: IModalWidgetOpenRequestData,
sourceWidget: Widget,
widgetRoomId?: string,
): void => {
2020-09-25 14:08:27 +01:00
if (this.modalInstance) return;
2020-10-19 20:39:43 +01:00
this.openSourceWidgetId = sourceWidget.id;
this.openSourceWidgetRoomId = widgetRoomId ?? null;
2022-06-14 17:51:51 +01:00
this.modalInstance = Modal.createDialog(
ModalWidgetDialog,
{
2021-06-29 13:11:58 +01:00
widgetDefinition: { ...requestData },
2021-02-02 14:22:28 +00:00
widgetRoomId,
2020-10-19 20:39:43 +01:00
sourceWidgetId: sourceWidget.id,
onFinished: (success, data) => {
this.closeModalWidget(sourceWidget, widgetRoomId, success && data ? data : { "m.exited": true });
2020-09-25 14:08:27 +01:00
this.openSourceWidgetId = null;
this.openSourceWidgetRoomId = null;
2020-09-25 14:08:27 +01:00
this.modalInstance = null;
2022-12-12 12:24:14 +01:00
},
2020-09-25 14:08:27 +01:00
},
undefined,
/* priority = */ false,
/* static = */ true,
);
2020-09-25 14:08:27 +01:00
};
public closeModalWidget = (
sourceWidget: Widget,
widgetRoomId: string | undefined,
data: IModalWidgetReturnData,
): void => {
2020-09-25 14:08:27 +01:00
if (!this.modalInstance) return;
if (this.openSourceWidgetId === sourceWidget.id && this.openSourceWidgetRoomId === widgetRoomId) {
2020-09-25 14:08:27 +01:00
this.openSourceWidgetId = null;
this.openSourceWidgetRoomId = null;
2020-09-25 14:08:27 +01:00
this.modalInstance.close();
this.modalInstance = null;
const sourceMessaging = WidgetMessagingStore.instance.getMessaging(sourceWidget, widgetRoomId);
2020-09-25 14:08:27 +01:00
if (!sourceMessaging) {
2021-10-15 16:30:53 +02:00
logger.error("No source widget messaging for modal widget");
2020-09-25 14:08:27 +01:00
return;
}
2020-10-19 20:39:43 +01:00
sourceMessaging.notifyModalWidgetClose(data);
2020-09-25 14:08:27 +01:00
}
};
}
window.mxModalWidgetStore = ModalWidgetStore.instance;