2023-11-30 15:20:49 +00:00
|
|
|
import { Spinner } from "@patternfly/react-core";
|
|
|
|
import { Suspense, lazy, useMemo, useState } from "react";
|
|
|
|
import { useParams } from "react-router-dom";
|
2024-02-01 15:46:11 +00:00
|
|
|
import { useEnvironment } from "../root/KeycloakContext";
|
|
|
|
import { MenuItem } from "../root/PageNav";
|
2024-01-10 08:38:02 +00:00
|
|
|
import { ContentComponentParams } from "../routes";
|
2023-11-30 15:20:49 +00:00
|
|
|
import { joinPath } from "../utils/joinPath";
|
|
|
|
import { usePromise } from "../utils/usePromise";
|
2024-01-10 08:38:02 +00:00
|
|
|
import fetchContentJson from "./fetchContent";
|
2023-11-30 15:20:49 +00:00
|
|
|
|
|
|
|
function findComponent(
|
2024-01-10 08:38:02 +00:00
|
|
|
content: MenuItem[],
|
2023-11-30 15:20:49 +00:00
|
|
|
componentId: string,
|
|
|
|
): string | undefined {
|
|
|
|
for (const item of content) {
|
2024-01-19 09:46:08 +00:00
|
|
|
if (
|
|
|
|
"path" in item &&
|
|
|
|
item.path.endsWith(componentId) &&
|
|
|
|
"modulePath" in item
|
|
|
|
) {
|
2024-01-10 08:38:02 +00:00
|
|
|
return item.modulePath;
|
2023-11-30 15:20:49 +00:00
|
|
|
}
|
2024-01-10 08:38:02 +00:00
|
|
|
if ("children" in item) {
|
|
|
|
return findComponent(item.children, componentId);
|
2023-11-30 15:20:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-02-01 15:46:11 +00:00
|
|
|
export const ContentComponent = () => {
|
|
|
|
const context = useEnvironment();
|
|
|
|
|
2024-01-10 08:38:02 +00:00
|
|
|
const [content, setContent] = useState<MenuItem[]>();
|
2023-11-30 15:20:49 +00:00
|
|
|
const { componentId } = useParams<ContentComponentParams>();
|
|
|
|
|
2024-02-01 15:46:11 +00:00
|
|
|
usePromise((signal) => fetchContentJson({ signal, context }), setContent);
|
2023-11-30 15:20:49 +00:00
|
|
|
const modulePath = useMemo(
|
|
|
|
() => findComponent(content || [], componentId!),
|
|
|
|
[content, componentId],
|
|
|
|
);
|
|
|
|
|
2024-01-19 12:00:56 +00:00
|
|
|
return modulePath && <Component modulePath={modulePath} />;
|
2023-11-30 15:20:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type ComponentProps = {
|
|
|
|
modulePath: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Component = ({ modulePath }: ComponentProps) => {
|
2024-02-01 15:46:11 +00:00
|
|
|
const { environment } = useEnvironment();
|
|
|
|
|
2023-11-30 15:20:49 +00:00
|
|
|
const Element = lazy(
|
|
|
|
() => import(joinPath(environment.resourceUrl, modulePath)),
|
|
|
|
);
|
2024-01-19 12:00:56 +00:00
|
|
|
return (
|
|
|
|
<Suspense fallback={<Spinner />}>
|
|
|
|
<Element />
|
|
|
|
</Suspense>
|
|
|
|
);
|
2023-11-30 15:20:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ContentComponent;
|