Files
persionalBrand/apps/admin/src/features/brand/component-renderers.jsx
T

37 lines
1.4 KiB
React

import React from "react";
import { Inquiry } from "../../../../taro/src/components/Inquiry.tsx";
import { Records } from "../../../../taro/src/components/Records.tsx";
import { Success } from "../../../../taro/src/components/Success.tsx";
import { TemplateAssetContext } from "./taro-web-components.jsx";
const templateRenderers = import.meta.glob("../../../../../packages/template-*/taro/index.tsx", {
eager: true,
});
function pageContent({ workspace, page }) {
if (page === "inquiry") return <Inquiry workspace={workspace} preview />;
if (page === "records") return <Records workspace={workspace} preview />;
if (page === "success") return <Success workspace={workspace} preview />;
return null;
}
export function resolveTemplateRenderer(workspace) {
const config = workspace?.brand?.config;
const key =
config && typeof config === "object" && !Array.isArray(config)
? config.visualTemplateKey
: undefined;
if (typeof key !== "string") return null;
const suffix = `/template-${key}/taro/index.tsx`;
const entry = Object.entries(templateRenderers).find(([path]) => path.endsWith(suffix));
const TemplatePage = entry?.[1]?.TemplatePage;
if (!TemplatePage) return null;
return function TemplatePreview(props) {
return (
<TemplateAssetContext.Provider value={key}>
<TemplatePage {...props}>{pageContent(props)}</TemplatePage>
</TemplateAssetContext.Provider>
);
};
}