2024-08-21 02:01:29 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
2024-07-30 20:44:19 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import * as jotai from "jotai";
|
|
|
|
import { globalStore } from "./global";
|
|
|
|
|
|
|
|
class ModalsModel {
|
|
|
|
modalsAtom: jotai.PrimitiveAtom<Array<{ displayName: string; props?: any }>>;
|
2024-09-17 08:45:47 +02:00
|
|
|
tosOpen: jotai.PrimitiveAtom<boolean>;
|
2024-07-30 20:44:19 +02:00
|
|
|
|
|
|
|
constructor() {
|
2024-09-17 08:45:47 +02:00
|
|
|
this.tosOpen = jotai.atom(false);
|
2024-07-30 20:44:19 +02:00
|
|
|
this.modalsAtom = jotai.atom([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pushModal = (displayName: string, props?: any) => {
|
|
|
|
const modals = globalStore.get(this.modalsAtom);
|
|
|
|
globalStore.set(this.modalsAtom, [...modals, { displayName, props }]);
|
|
|
|
};
|
|
|
|
|
|
|
|
popModal = (callback?: () => void) => {
|
|
|
|
const modals = globalStore.get(this.modalsAtom);
|
|
|
|
if (modals.length > 0) {
|
|
|
|
const updatedModals = modals.slice(0, -1);
|
|
|
|
globalStore.set(this.modalsAtom, updatedModals);
|
|
|
|
if (callback) callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
hasOpenModals(): boolean {
|
|
|
|
const modals = globalStore.get(this.modalsAtom);
|
|
|
|
return modals.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
isModalOpen(displayName: string): boolean {
|
|
|
|
const modals = globalStore.get(this.modalsAtom);
|
|
|
|
return modals.some((modal) => modal.displayName === displayName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const modalsModel = new ModalsModel();
|
|
|
|
|
|
|
|
export { modalsModel };
|