(null);
const { enableLog = process.env.NODE_ENV === "development", ...rest } = props;
useEffect(() => {
setChild(null);
}, [resource]);
useEffect(() => {
if (record && !child) {
const inferredElements = getElementsFromRecords([record], showFieldTypes);
const inferredChild = new InferredElement(
showFieldTypes.show,
null,
inferredElements,
);
setChild(inferredChild.getElement());
if (!enableLog) return;
const representation = inferredChild.getRepresentation();
const components = ["Show"]
.concat(
Array.from(
new Set(
Array.from(representation.matchAll(/<([^/\s>]+)/g))
.map((match) => match[1])
.filter(
(component) => component !== "span" && component !== "div",
),
),
),
)
.sort();
// eslint-disable-next-line no-console
console.log(
`Guessed Show:
${components
.map(
(component) =>
`import { ${component} } from "@/components/admin/${kebabCase(
component,
)}";`,
)
.join("\n")}
export const ${capitalize(singularize(resource))}Show = () => (
${inferredChild.getRepresentation()}
);`,
);
}
}, [record, child, resource, enableLog]);
return {child};
};
const showFieldTypes: InferredTypeMap = {
show: {
component: (props: any) => (
{props.children}
),
representation: (
_props: any,
children: { getRepresentation: () => string }[],
) => `
${children
.map((child) => ` ${child.getRepresentation()}`)
.join("\n")}
`,
},
reference: {
component: (props: any) => (
),
representation: (props: any) =>
`
`,
},
array: {
component: ({ children, ...props }: any) => {
const childrenArray = Children.toArray(children);
return (
0 &&
isValidElement(childrenArray[0]) &&
(childrenArray[0].props as any).source
}
/>
);
},
representation: (props: any, children: any) =>
`
`,
},
referenceArray: {
component: (props: any) => (
),
representation: (props: any) =>
`
`,
},
boolean: {
component: (props: any) => (
(record[props.source] ? "Yes" : "No")}
/>
),
representation: (props: any) =>
` record[${props.source}] ? 'Yes' : 'No'} />`,
},
date: {
component: (props: any) => (
),
representation: (props: any) =>
`
`,
},
number: {
component: (props: any) => (
),
representation: (props: any) =>
`
`,
},
string: {
component: (props: any) => ,
representation: (props: any) => ``,
},
};
const kebabCase = (name: string) => {
return name
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/([A-Z])([A-Z][a-z])/g, "$1-$2")
.toLowerCase();
};