1 import { SidebarLeft, TriangleRightMini, XMark } from "@medusajs/icons"
2 import { IconButton, clx } from "@medusajs/ui"
3 import { AnimatePresence } from "motion/react"
4 import { Dialog as RadixDialog } from "radix-ui"
5 import { PropsWithChildren, ReactNode, useEffect, useState } from "react"
6 import { useTranslation } from "react-i18next"
7 import {
8 Link,
9 Outlet,
10 UIMatch,
11 useMatches,
12 useNavigation,
13 } from "react-router-dom"
14
15 import { DocumentTitle } from "../../../hooks/use-document-title"
16 import { KeybindProvider } from "../../../providers/keybind-provider"
17 import { useGlobalShortcuts } from "../../../providers/keybind-provider/hooks"
18 import { useSidebar } from "../../../providers/sidebar-provider"
19 import { ProgressBar } from "../../common/progress-bar"
20 import {
21 LayoutCustomizerHostProvider,
22 LayoutCustomizerSlot,
23 } from "../../../providers/customizer-host-provider/customizer-host-provider"
24 import { Notifications } from "../notifications"
25 import { CustomizerMenu, LayoutComposer } from "../../layout-composer"
26 import {
27 CUSTOMIZE_IDS,
28 LAYOUT_CONTROLS_LOCATION,
29 } from "../../layout-composer/constants"
30 import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
31
32 export const Shell = ({ children }: PropsWithChildren) => {
33 const globalShortcuts = useGlobalShortcuts()
34 const navigation = useNavigation()
35
36 const loading = navigation.state === "loading"
37
38 return (
39 <KeybindProvider shortcuts={globalShortcuts}>
40 <DocumentTitle />
41 <LayoutCustomizerHostProvider>
42 <div className="relative flex h-screen flex-col items-start overflow-hidden lg:flex-row">
43 <NavigationBar loading={loading} />
44 <div>
45 <MobileSidebarContainer>{children}</MobileSidebarContainer>
46 <DesktopSidebarContainer>{children}</DesktopSidebarContainer>
47 </div>
48 <div className="flex h-screen w-full flex-col overflow-auto">
49 <Topbar />
50 <main
51 className={clx(
52 "flex h-full w-full flex-col items-center overflow-y-auto transition-opacity delay-200 duration-200",
53 {
54 "opacity-25": loading,
55 }
56 )}
57 >
58 <Gutter>
59 <Outlet />
60 </Gutter>
61 </main>
62 </div>
63 </div>
64 </LayoutCustomizerHostProvider>
65 </KeybindProvider>
66 )
67 }
68
69 const NavigationBar = ({ loading }: { loading: boolean }) => {
70 const [showBar, setShowBar] = useState(false)
71
72 /**
73 * If the loading state is true, we want to show the bar after a short delay.
74 * The delay is used to prevent the bar from flashing on quick navigations.
75 */
76 useEffect(() => {
77 let timeout: ReturnType<typeof setTimeout>
78
79 if (loading) {
80 timeout = setTimeout(() => {
81 setShowBar(true)
82 }, 200)
83 } else {
84 setShowBar(false)
85 }
86
87 return () => {
88 clearTimeout(timeout)
89 }
90 }, [loading])
91
92 return (
93 <div className="fixed inset-x-0 top-0 z-50 h-1">
94 <AnimatePresence>{showBar ? <ProgressBar /> : null}</AnimatePresence>
95 </div>
96 )
97 }
98
99 const Gutter = ({ children }: PropsWithChildren) => {
100 return (
101 <div className="flex w-full max-w-[1600px] flex-col gap-y-2 p-3">
102 {children}
103 </div>
104 )
105 }
106
107 const Breadcrumbs = () => {
108 const matches = useMatches() as unknown as UIMatch<
109 unknown,
110 {
111 breadcrumb?: (match?: UIMatch) => string | ReactNode
112 }
113 >[]
114
115 const crumbs = matches
116 .filter((match) => match.handle?.breadcrumb)
117 .map((match) => {
118 const handle = match.handle
119
120 let label: string | ReactNode | undefined = undefined
121
122 try {
123 label = handle.breadcrumb?.(match)
124 } catch (error) {
125 // noop
126 }
127
128 if (!label) {
129 return null
130 }
131
132 return {
133 label: label,
134 path: match.pathname,
135 }
136 })
137 .filter(Boolean) as { label: string | ReactNode; path: string }[]
138
139 return (
140 <ol
141 className={clx(
142 "text-ui-fg-muted txt-compact-small-plus flex select-none items-center"
143 )}
144 >
145 {crumbs.map((crumb, index) => {
146 const isLast = index === crumbs.length - 1
147 const isSingle = crumbs.length === 1
148
149 return (
150 <li key={index} className={clx("flex items-center")}>
151 {!isLast ? (
152 <Link
153 className="transition-fg hover:text-ui-fg-subtle"
154 to={crumb.path}
155 >
156 {crumb.label}
157 </Link>
158 ) : (
159 <div>
160 {!isSingle && <span className="block lg:hidden">...</span>}
161 <span
162 key={index}
163 className={clx({
164 "hidden lg:block": !isSingle,
165 })}
166 >
167 {crumb.label}
168 </span>
169 </div>
170 )}
171 {!isLast && (
172 <span className="mx-2">
173 <TriangleRightMini className="rtl:rotate-180" />
174 </span>
175 )}
176 </li>
177 )
178 })}
179 </ol>
180 )
181 }
182
183 const ToggleSidebar = () => {
184 const { toggle } = useSidebar()
185
186 return (
187 <div>
188 <IconButton
189 className="hidden lg:flex"
190 variant="transparent"
191 onClick={() => toggle("desktop")}
192 size="small"
193 >
194 <SidebarLeft className="text-ui-fg-muted rtl:rotate-180" />
195 </IconButton>
196 <IconButton
197 className="hidden max-lg:flex"
198 variant="transparent"
199 onClick={() => toggle("mobile")}
200 size="small"
201 >
202 <SidebarLeft className="text-ui-fg-muted rtl:rotate-180" />
203 </IconButton>
204 </div>
205 )
206 }
207
208 const Topbar = () => {
209 return (
210 <div className="grid w-full grid-cols-2 border-b p-3">
211 <div className="flex items-center gap-x-1.5">
212 <ToggleSidebar />
213 <Breadcrumbs />
214 </div>
215 <div className="flex items-center justify-end gap-x-3">
216 {/* Single trigger for every host; while editing, the active composer's
217 controls portal into the adjacent slot in its place. */}
218 <CustomizerMenu />
219 <LayoutCustomizerSlot location={LAYOUT_CONTROLS_LOCATION} />
220 <LayoutComposer
221 widgetsZonePrefix="topbar"
222 preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_ROW}
223 sections={{
224 main: (
225 <LayoutComposer.Entry id="Notifications">
226 <Notifications />
227 </LayoutComposer.Entry>
228 ),
229 }}
230 hasOutlet={false}
231 customizeId={CUSTOMIZE_IDS.TOPBAR}
232 controlSize="xsmall"
233 />
234 </div>
235 </div>
236 )
237 }
238
239 const DesktopSidebarContainer = ({ children }: PropsWithChildren) => {
240 const { desktop } = useSidebar()
241
242 return (
243 <div
244 className={clx("hidden h-screen w-[220px] border-e", {
245 "lg:flex": desktop,
246 })}
247 >
248 {children}
249 </div>
250 )
251 }
252
253 const MobileSidebarContainer = ({ children }: PropsWithChildren) => {
254 const { t } = useTranslation()
255 const { mobile, toggle } = useSidebar()
256
257 return (
258 <RadixDialog.Root open={mobile} onOpenChange={() => toggle("mobile")}>
259 <RadixDialog.Portal>
260 <RadixDialog.Overlay
261 className={clx(
262 "bg-ui-bg-overlay fixed inset-0",
263 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0"
264 )}
265 />
266 <RadixDialog.Content
267 className={clx(
268 "bg-ui-bg-subtle shadow-elevation-modal fixed inset-y-2 start-2 flex w-full max-w-[304px] flex-col overflow-hidden rounded-lg border-r",
269 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:slide-out-to-start-1/2 data-[state=open]:slide-in-from-start-1/2 duration-200"
270 )}
271 >
272 <div className="p-3">
273 <RadixDialog.Close asChild>
274 <IconButton
275 size="small"
276 variant="transparent"
277 className="text-ui-fg-subtle"
278 >
279 <XMark />
280 </IconButton>
281 </RadixDialog.Close>
282 <RadixDialog.Title className="sr-only">
283 {t("app.nav.accessibility.title")}
284 </RadixDialog.Title>
285 <RadixDialog.Description className="sr-only">
286 {t("app.nav.accessibility.description")}
287 </RadixDialog.Description>
288 </div>
289 {children}
290 </RadixDialog.Content>
291 </RadixDialog.Portal>
292 </RadixDialog.Root>
293 )
294 }