import React from "react"; import { Body, UnorderedList, OrderedList, Button, Container, Heading, Heading2, Heading3, Heading4, Heading5, Heading6, ImageElement, Section, Span, Anchor, Bold, Marquee, } from "../IndividualComponents"; import { Some } from "../utils/Some"; import { getClassName } from "../utils/getClassName"; import { getBackgroundImage } from "../utils/getBackgroundImage"; export type Data = { type: string; content?: Data[]; attributes?: Record; }; export type Content = { id: string; textvalue: string; }; const getContentValue = (content: Content[], id: any) => { if (Some(content) && content.length !== 0) { const mappedContent = content.find((c) => c.id === id); //@ts-ignore return Some(mappedContent) ? mappedContent.textvalue : null; } return null; }; const getTextValue = (content: any) => { return Some(content) ? (content[0] as unknown as string) : ""; }; export const generateReactElements = (data: Data, content: Content[]) => { // const selectedTheme = Some(theme) ? theme : "light"; if (typeof data === "string") { return data; } if (data.type === "div") { const attributes = data.attributes || {}; const children = data.content || []; const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); return ( {children} ); } else if (data.type === "section") { const attributes = data.attributes || {}; const children = data.content || []; const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const backgroundImageUrl = getBackgroundImage(attributes.style); if (Some(backgroundImageUrl) && backgroundImageUrl !== "") { return (
{children.map((child, index) => ( {generateReactElements(child, content)} ))}
); } return (
{children}
); } else if (data.type === "h1") { const attributes = data.attributes || {}; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ; } return ; } return (

{data.content?.map((child, index) => ( {generateReactElements(child, content)} ))}

); } else if (data.type === "h2") { const attributes = data.attributes || {}; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ( ); } return ; } return (

{data?.content?.map((child, index) => ( {generateReactElements(child, content)} ))}

); } else if (data.type === "h3") { const attributes = data.attributes || {}; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ( ); } return ; } return (

{data.content?.map((child, index) => ( {generateReactElements(child, content)} ))}

); } else if (data.type === "h4") { const attributes = data.attributes || {}; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ( ); } return ; } return (

{data.content?.map((child, index) => ( {generateReactElements(child, content)} ))}

); } else if (data.type === "h5") { const attributes = data.attributes || {}; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ( ); } return ; } return (
{data.content?.map((child, index) => ( {generateReactElements(child, content)} ))}
); } else if (data.type === "h6") { const attributes = data.attributes || {}; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ( ); } return ; } return (
{data.content?.map((child, index) => ( {generateReactElements(child, content)} ))}
); } else if (data.type === "p") { const attributes = data.attributes || {}; const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { const color = fontColor ? fontColor[1] : null; if (color) { return ( ); } return ( ); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (typeof data?.content[0] === "string") { const color = fontColor ? fontColor[1] : null; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const text = getContentValue(content, attributes.id) ?? data?.content[0]; if (color) { return ; } return ; } return (

{data.content?.map((child, index) => ( {generateReactElements(child, content)} ))}

); } else if (data.type === "span") { const attributes = data.attributes || {}; const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); const styleAttribute = attributes.style; const fontColor = /color\s*:\s*([^;]*)/.exec(styleAttribute); const fontFamily = /font-family\s*:\s*([^;]*)/.exec(styleAttribute); const fontSize = /font-size\s*:\s*([^;]*)/.exec(styleAttribute); const color = fontColor ? fontColor[1] : null; const font = fontFamily ? fontFamily[1] : undefined; const fontsize = fontSize ? fontSize[1] : undefined; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore if (data.content === undefined) { const color = fontColor ? fontColor[1] : null; if (color) { return ( ); } return ( ); } //@ts-ignore if (typeof data?.content[0] === "string") { //@ts-ignore const text = Some(data.content) ? (data?.content[0] as unknown as string) : ""; if (color) { return ; } return ( ); } return ( {data.content?.map((child, index) => ( {generateReactElements(child, content)} ))} ); } else if (data.type === "ul") { const attributes = data.attributes || {}; const children = data.content || []; // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore const filteredChildern = children.filter((element) => !/^[\s\n]+$/.test(element)); const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); return ( {filteredChildern} ); } else if (data.type === "img") { const attributes = data.attributes || {}; const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); return ( ); } else if (data.type === "button") { const attributes = data.attributes || {}; const text = data.content ? (data?.content[0] as unknown as string) : ""; const className = Some(attributes.class) ? getClassName(attributes.class) : getClassName(attributes.classname); if (Some(attributes.onclick)) { const onClickFunc = attributes.onclick.split("(")[0]; //@ts-ignore const executeFunction = (funcName, event) => { if (typeof window[funcName] === "function") { //@ts-ignore window[funcName](event); } else { console.error(`Function ${funcName} does not exist`); } }; //@ts-ignore const handleButtonClick = (funcName, event) => { // Execute function based on the button clicked executeFunction(funcName, event); }; return (