import { clx } from "@medusajs/ui" import { Children, ComponentPropsWithoutRef, ComponentType } from "react" import { Outlet } from "react-router-dom" import { JsonViewSection } from "../../../common/json-view-section" import { MetadataSection } from "../../../common/metadata-section" import { PageProps, WidgetProps } from "../types" interface TwoColumnWidgetProps extends WidgetProps { sideBefore: ComponentType[] sideAfter: ComponentType[] } interface TwoColumnPageProps extends PageProps { widgets: TwoColumnWidgetProps } const Root = ({ children, /** * Widgets to be rendered in the main content area and sidebar. */ widgets, /** * Data to be passed to widgets, JSON view, and Metadata view. */ data, /** * Whether to show JSON view of the data. Defaults to false. */ showJSON = false, /** * Whether to show metadata view of the data. Defaults to false. */ showMetadata = false, /** * Whether to render an outlet for children routes. Defaults to true. */ hasOutlet = true, }: TwoColumnPageProps) => { const widgetProps = { data } const { before, after, sideBefore, sideAfter } = widgets if (showJSON && !data) { if (process.env.NODE_ENV === "development") { console.warn( "`showJSON` is true but no data is provided. To display JSON, provide data prop." ) } showJSON = false } if (showMetadata && !data) { if (process.env.NODE_ENV === "development") { console.warn( "`showMetadata` is true but no data is provided. To display metadata, provide data prop." ) } showMetadata = false } const childrenArray = Children.toArray(children) if (childrenArray.length !== 2) { throw new Error("TwoColumnPage expects exactly two children") } const [main, sidebar] = childrenArray const showExtraData = showJSON || showMetadata return (
{before.map((Component, i) => { return })}
{main} {after.map((Component, i) => { return })} {showExtraData && (
{showMetadata && } {showJSON && }
)}
{sideBefore.map((Component, i) => { return })} {sidebar} {sideAfter.map((Component, i) => { return })} {showExtraData && (
{showMetadata && } {showJSON && }
)}
{hasOutlet && }
) } const Main = ({ children, className, ...props }: ComponentPropsWithoutRef<"div">) => { return (
{children}
) } const Sidebar = ({ children, className, ...props }: ComponentPropsWithoutRef<"div">) => { return (
{children}
) } export const TwoColumnPage = Object.assign(Root, { Main, Sidebar })