Raw
1 import { clx } from "@medusajs/ui"
2 import { Children, ComponentPropsWithoutRef, ComponentType } from "react"
3 import { Outlet } from "react-router-dom"
4 import { JsonViewSection } from "../../../common/json-view-section"
5 import { MetadataSection } from "../../../common/metadata-section"
6 import { PageProps, WidgetProps } from "../types"
7 import { RequiredPermissionsSection } from "../../../common/required-permissions-section"
8
9 interface TwoColumnWidgetProps extends WidgetProps {
10 sideBefore: ComponentType<any>[]
11 sideAfter: ComponentType<any>[]
12 }
13
14 interface TwoColumnPageProps<TData> extends PageProps<TData> {
15 widgets: TwoColumnWidgetProps
16 /**
17 * Optional section rendered under the JSON section.
18 */
19 requiredPermissionsSection?: ComponentPropsWithoutRef<"div">["children"]
20 }
21
22 const Root = <TData,>({
23 children,
24 /**
25 * Widgets to be rendered in the main content area and sidebar.
26 */
27 widgets,
28 /**
29 * Data to be passed to widgets, JSON view, and Metadata view.
30 */
31 data,
32 /**
33 * Whether to show JSON view of the data. Defaults to false.
34 */
35 showJSON = false,
36 /**
37 * Whether to show metadata view of the data. Defaults to false.
38 */
39 showMetadata = false,
40 /**
41 * Optional section rendered under the JSON section.
42 */
43 requiredPermissionsSection = <RequiredPermissionsSection />,
44 /**
45 * Whether to render an outlet for children routes. Defaults to true.
46 */
47 hasOutlet = true,
48 }: TwoColumnPageProps<TData>) => {
49 const widgetProps = { data }
50 const { before, after, sideBefore, sideAfter } = widgets
51
52 if (showJSON && !data) {
53 if (process.env.NODE_ENV === "development") {
54 console.warn(
55 "`showJSON` is true but no data is provided. To display JSON, provide data prop."
56 )
57 }
58
59 showJSON = false
60 }
61
62 if (showMetadata && !data) {
63 if (process.env.NODE_ENV === "development") {
64 console.warn(
65 "`showMetadata` is true but no data is provided. To display metadata, provide data prop."
66 )
67 }
68
69 showMetadata = false
70 }
71
72 const childrenArray = Children.toArray(children)
73
74 if (childrenArray.length !== 2) {
75 throw new Error("TwoColumnPage expects exactly two children")
76 }
77
78 const [main, sidebar] = childrenArray
79 const showExtraData = showJSON || showMetadata
80
81 return (
82 <div className="flex w-full flex-col gap-y-3">
83 {before.map((Component, i) => {
84 return <Component {...widgetProps} key={i} />
85 })}
86 <div className="flex w-full flex-col items-start gap-x-4 gap-y-3 xl:grid xl:grid-cols-[minmax(0,_1fr)_440px]">
87 <div className="flex w-full min-w-0 flex-col gap-y-3">
88 {main}
89 {after.map((Component, i) => {
90 return <Component {...widgetProps} key={i} />
91 })}
92 {showExtraData && (
93 <div className="hidden flex-col gap-y-3 xl:flex">
94 {showMetadata && <MetadataSection data={data!} />}
95 {showJSON && <JsonViewSection data={data!} />}
96 {showJSON && requiredPermissionsSection}
97 </div>
98 )}
99 </div>
100 <div className="flex w-full flex-col gap-y-3 xl:mt-0">
101 {sideBefore.map((Component, i) => {
102 return <Component {...widgetProps} key={i} />
103 })}
104 {sidebar}
105 {sideAfter.map((Component, i) => {
106 return <Component {...widgetProps} key={i} />
107 })}
108 {showExtraData && (
109 <div className="flex flex-col gap-y-3 xl:hidden">
110 {showMetadata && <MetadataSection data={data!} />}
111 {showJSON && <JsonViewSection data={data!} />}
112 {showJSON && requiredPermissionsSection}
113 </div>
114 )}
115 </div>
116 </div>
117 {hasOutlet && <Outlet />}
118 </div>
119 )
120 }
121
122 const Main = ({
123 children,
124 className,
125 ...props
126 }: ComponentPropsWithoutRef<"div">) => {
127 return (
128 <div className={clx("flex w-full flex-col gap-y-3", className)} {...props}>
129 {children}
130 </div>
131 )
132 }
133
134 const Sidebar = ({
135 children,
136 className,
137 ...props
138 }: ComponentPropsWithoutRef<"div">) => {
139 return (
140 <div
141 className={clx(
142 "flex w-full max-w-[100%] flex-col gap-y-3 xl:mt-0 xl:max-w-[440px]",
143 className
144 )}
145 {...props}
146 >
147 {children}
148 </div>
149 )
150 }
151
152 export const TwoColumnPage = Object.assign(Root, { Main, Sidebar })