1 import {
2 Buildings,
3 BuildingStorefront,
4 CogSixTooth,
5 CurrencyDollar,
6 EllipsisHorizontal,
7 MagnifyingGlass,
8 OpenRectArrowOut,
9 ReceiptPercent,
10 ShoppingCart,
11 SquaresPlus,
12 Tag,
13 Users,
14 } from "@medusajs/icons"
15 import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
16 import { Avatar, clx, Divider, DropdownMenu, Text } from "@medusajs/ui"
17 import { useTranslation } from "react-i18next"
18
19 import { useStore } from "../../../hooks/api/store"
20 import { LayoutComposer } from "../../layout-composer"
21 import { PermissionGuard } from "../../common/permission-guard"
22 import { Skeleton } from "../../common/skeleton"
23 import { INavItem, NavItem } from "../../layout/nav-item"
24 import { Shell } from "../../layout/shell"
25
26 import { Link, useLocation, useNavigate } from "react-router-dom"
27 import { useLogout } from "../../../hooks/api"
28 import { queryClient } from "../../../lib/query-client"
29 import { useExtension } from "../../../providers/extension-provider"
30 import { useSearch } from "../../../providers/search-provider"
31 import { UserMenu } from "../user-menu"
32 import { useDocumentDirection } from "../../../hooks/use-document-direction"
33 import { CUSTOMIZE_IDS } from "../../layout-composer/constants"
34
35 export const MainLayout = () => {
36 return (
37 <Shell>
38 <MainSidebar />
39 </Shell>
40 )
41 }
42
43 const MainSidebar = () => {
44 return (
45 <aside className="flex flex-1 flex-col justify-between overflow-y-auto">
46 <div className="flex flex-1 flex-col">
47 <PermissionGuard resource="store" operation="read">
48 <div className="bg-ui-bg-subtle sticky top-0">
49 <Header />
50 <div className="px-3">
51 <Divider variant="dashed" />
52 </div>
53 </div>
54 </PermissionGuard>
55 <div className="flex flex-1 flex-col justify-between">
56 <div className="flex flex-1 flex-col">
57 <SidebarRoutes />
58 </div>
59 <UtilitySection />
60 </div>
61 <div className="bg-ui-bg-subtle sticky bottom-0">
62 <UserSection />
63 </div>
64 </div>
65 </aside>
66 )
67 }
68
69 const Logout = () => {
70 const { t } = useTranslation()
71 const navigate = useNavigate()
72
73 const { mutateAsync: logoutMutation } = useLogout()
74
75 const handleLogout = async () => {
76 await logoutMutation(undefined, {
77 onSuccess: () => {
78 /**
79 * When the user logs out, we want to clear the query cache
80 */
81 queryClient.clear()
82 navigate("/login")
83 },
84 })
85 }
86
87 return (
88 <DropdownMenu.Item onClick={handleLogout}>
89 <div className="flex items-center gap-x-2">
90 <OpenRectArrowOut className="text-ui-fg-subtle" />
91 <span>{t("app.menus.actions.logout")}</span>
92 </div>
93 </DropdownMenu.Item>
94 )
95 }
96
97 const Header = () => {
98 const { t } = useTranslation()
99 const { store, isPending, isError, error } = useStore()
100 const direction = useDocumentDirection()
101 const name = store?.name
102 const fallback = store?.name?.slice(0, 1).toUpperCase()
103
104 const isLoaded = !isPending && !!store && !!name && !!fallback
105
106 if (isError) {
107 throw error
108 }
109
110 return (
111 <div className="w-full p-3">
112 <DropdownMenu dir={direction}>
113 <DropdownMenu.Trigger
114 disabled={!isLoaded}
115 className={clx(
116 "bg-ui-bg-subtle transition-fg grid w-full grid-cols-[24px_1fr_15px] items-center gap-x-3 rounded-md p-0.5 pe-2 outline-none",
117 "hover:bg-ui-bg-subtle-hover",
118 "data-[state=open]:bg-ui-bg-subtle-hover",
119 "focus-visible:shadow-borders-focus"
120 )}
121 >
122 {fallback ? (
123 <Avatar variant="squared" size="xsmall" fallback={fallback} />
124 ) : (
125 <Skeleton className="h-6 w-6 rounded-md" />
126 )}
127 <div className="block overflow-hidden text-start">
128 {name ? (
129 <Text
130 size="small"
131 weight="plus"
132 leading="compact"
133 className="truncate"
134 >
135 {store.name}
136 </Text>
137 ) : (
138 <Skeleton className="h-[9px] w-[120px]" />
139 )}
140 </div>
141 <EllipsisHorizontal className="text-ui-fg-muted" />
142 </DropdownMenu.Trigger>
143 {isLoaded && (
144 <DropdownMenu.Content className="w-[var(--radix-dropdown-menu-trigger-width)] min-w-0">
145 <div className="flex items-center gap-x-3 px-2 py-1">
146 <Avatar variant="squared" size="small" fallback={fallback} />
147 <div className="flex flex-col overflow-hidden">
148 <Text
149 size="small"
150 weight="plus"
151 leading="compact"
152 className="truncate"
153 >
154 {name}
155 </Text>
156 <Text
157 size="xsmall"
158 leading="compact"
159 className="text-ui-fg-subtle"
160 >
161 {t("app.nav.main.store")}
162 </Text>
163 </div>
164 </div>
165 <DropdownMenu.Separator />
166 <DropdownMenu.Item className="gap-x-2" asChild>
167 <Link to="/settings/store">
168 <BuildingStorefront className="text-ui-fg-subtle" />
169 {t("app.nav.main.storeSettings")}
170 </Link>
171 </DropdownMenu.Item>
172 <DropdownMenu.Separator />
173 <Logout />
174 </DropdownMenu.Content>
175 )}
176 </DropdownMenu>
177 </div>
178 )
179 }
180
181 const useCoreRoutes = (): Omit<INavItem, "pathname">[] => {
182 const { t } = useTranslation()
183
184 return [
185 {
186 icon: <ShoppingCart />,
187 label: t("orders.domain"),
188 to: "/orders",
189 items: [
190 // TODO: Enable when domin is introduced
191 // {
192 // label: t("draftOrders.domain"),
193 // to: "/draft-orders",
194 // },
195 ],
196 },
197 {
198 icon: <Tag />,
199 label: t("products.domain"),
200 to: "/products",
201 items: [
202 {
203 label: t("collections.domain"),
204 to: "/collections",
205 },
206 {
207 label: t("categories.domain"),
208 to: "/categories",
209 },
210 {
211 label: t("productOptions.domain"),
212 to: "/product-options",
213 },
214 // TODO: Enable when domin is introduced
215 // {
216 // label: t("giftCards.domain"),
217 // to: "/gift-cards",
218 // },
219 ],
220 },
221 {
222 icon: <Buildings />,
223 label: t("inventory.domain"),
224 to: "/inventory",
225 items: [
226 {
227 label: t("reservations.domain"),
228 to: "/reservations",
229 },
230 ],
231 },
232 {
233 icon: <Users />,
234 label: t("customers.domain"),
235 to: "/customers",
236 items: [
237 {
238 label: t("customerGroups.domain"),
239 to: "/customer-groups",
240 },
241 ],
242 },
243 {
244 icon: <ReceiptPercent />,
245 label: t("promotions.domain"),
246 to: "/promotions",
247 items: [
248 {
249 label: t("campaigns.domain"),
250 to: "/campaigns",
251 },
252 ],
253 },
254 {
255 icon: <CurrencyDollar />,
256 label: t("priceLists.domain"),
257 to: "/price-lists",
258 },
259 ]
260 }
261
262 const Searchbar = () => {
263 const { t } = useTranslation()
264 const { toggleSearch } = useSearch()
265
266 return (
267 <div>
268 <button
269 onClick={toggleSearch}
270 className={clx(
271 "bg-ui-bg-subtle text-ui-fg-subtle flex w-full items-center gap-x-2.5 rounded-md px-2 py-1 outline-none",
272 "hover:bg-ui-bg-subtle-hover",
273 "focus-visible:shadow-borders-focus"
274 )}
275 >
276 <MagnifyingGlass />
277 <div className="flex-1 text-start">
278 <Text size="small" leading="compact" weight="plus">
279 {t("app.search.label")}
280 </Text>
281 </div>
282 <Text size="small" leading="compact" className="text-ui-fg-muted">
283 ⌘K
284 </Text>
285 </button>
286 </div>
287 )
288 }
289
290 /**
291 * The customizable nav. Every top-level route(core and extensions) is
292 * fed to the `LayoutComposer` as its own entry, so each can be reordered/hidden
293 * (and its children reordered) independently in edit mode.
294 */
295 const SidebarRoutes = () => {
296 const coreRoutes = useCoreRoutes()
297
298 const { getMenu } = useExtension()
299
300 const menuItems = getMenu("coreExtensions")
301
302 menuItems.forEach((item) => {
303 if (item.nested) {
304 const route = coreRoutes.find((route) => route.to === item.nested)
305 if (route) {
306 route.items?.push(item)
307 }
308 }
309 })
310
311 const extensionItems = menuItems.filter((item) => !item.nested)
312
313 return (
314 <nav className="py-3">
315 <div className="px-3">
316 <LayoutComposer
317 widgetsZonePrefix="sidebar"
318 preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_COLUMN}
319 hasOutlet={false}
320 disableWidgets
321 customizeId={CUSTOMIZE_IDS.MAIN_SIDEBAR}
322 controlSize="small"
323 layoutProps={{
324 className: "gap-y-1",
325 }}
326 sections={{
327 main: (
328 <>
329 <LayoutComposer.Entry id="Searchbar">
330 <Searchbar />
331 </LayoutComposer.Entry>
332 {coreRoutes.map((route) => (
333 <LayoutComposer.Entry id={`nav:${route.to}`} key={route.to}>
334 <NavItem key={route.to} {...route} />
335 </LayoutComposer.Entry>
336 ))}
337 {extensionItems.map((item) => (
338 <LayoutComposer.Entry id={`nav:${item.to}`} key={item.to}>
339 <NavItem
340 key={item.to}
341 to={item.to}
342 label={item.label}
343 icon={item.icon ? item.icon : <SquaresPlus />}
344 items={item.items}
345 translationNs={item.translationNs}
346 type="extension"
347 />
348 </LayoutComposer.Entry>
349 ))}
350 </>
351 ),
352 }}
353 />
354 </div>
355 </nav>
356 )
357 }
358
359 const UtilitySection = () => {
360 const location = useLocation()
361 const { t } = useTranslation()
362
363 return (
364 <div className="flex flex-col gap-y-0.5 px-3 py-3">
365 <NavItem
366 label={t("app.nav.settings.header")}
367 to="/settings"
368 from={location.pathname}
369 icon={<CogSixTooth />}
370 />
371 </div>
372 )
373 }
374
375 const UserSection = () => {
376 return (
377 <div>
378 <div className="px-3">
379 <Divider variant="dashed" />
380 </div>
381 <UserMenu />
382 </div>
383 )
384 }