2024-07-30 20:44:19 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-08-30 01:06:15 +02:00
|
|
|
import { atoms, globalStore } from "@/store/global";
|
2024-07-30 20:44:19 +02:00
|
|
|
import { modalsModel } from "@/store/modalmodel";
|
|
|
|
import * as jotai from "jotai";
|
2024-08-30 01:06:15 +02:00
|
|
|
import { useEffect } from "react";
|
2024-07-30 20:44:19 +02:00
|
|
|
import { getModalComponent } from "./modalregistry";
|
|
|
|
import { TosModal } from "./tos";
|
|
|
|
|
|
|
|
const ModalsRenderer = () => {
|
|
|
|
const clientData = jotai.useAtomValue(atoms.client);
|
2024-09-17 08:45:47 +02:00
|
|
|
const [tosOpen, setTosOpen] = jotai.useAtom(modalsModel.tosOpen);
|
2024-07-30 20:44:19 +02:00
|
|
|
const [modals] = jotai.useAtom(modalsModel.modalsAtom);
|
|
|
|
const rtn: JSX.Element[] = [];
|
|
|
|
for (const modal of modals) {
|
|
|
|
const ModalComponent = getModalComponent(modal.displayName);
|
|
|
|
if (ModalComponent) {
|
|
|
|
rtn.push(<ModalComponent key={modal.displayName} {...modal.props} />);
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 08:45:47 +02:00
|
|
|
if (tosOpen) {
|
2024-07-30 20:44:19 +02:00
|
|
|
rtn.push(<TosModal key={TosModal.displayName} />);
|
|
|
|
}
|
2024-09-17 08:45:47 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!clientData.tosagreed) {
|
|
|
|
setTosOpen(true);
|
|
|
|
}
|
|
|
|
}, [clientData]);
|
2024-08-30 01:06:15 +02:00
|
|
|
useEffect(() => {
|
|
|
|
globalStore.set(atoms.modalOpen, rtn.length > 0);
|
2024-09-17 08:45:47 +02:00
|
|
|
}, [rtn]);
|
|
|
|
|
2024-07-30 20:44:19 +02:00
|
|
|
return <>{rtn}</>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export { ModalsRenderer };
|