37 lines
1.4 KiB
React
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,
|
|
});
|
|
const resolvedRenderers = new Map();
|
|
|
|
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 key = workspace?.visualTemplate?.key;
|
|
if (typeof key !== "string") return null;
|
|
if (resolvedRenderers.has(key)) return resolvedRenderers.get(key);
|
|
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;
|
|
function TemplatePreview(props) {
|
|
return (
|
|
<TemplateAssetContext.Provider value={key}>
|
|
<TemplatePage {...props}>{pageContent(props)}</TemplatePage>
|
|
</TemplateAssetContext.Provider>
|
|
);
|
|
}
|
|
resolvedRenderers.set(key, TemplatePreview);
|
|
return TemplatePreview;
|
|
}
|