|
1
|
import { |
|
2
|
DndContext, |
|
3
|
DragEndEvent, |
|
4
|
PointerSensor, |
|
5
|
closestCenter, |
|
6
|
useSensor, |
|
7
|
useSensors, |
|
8
|
} from "@dnd-kit/core" |
|
9
|
import { |
|
10
|
SortableContext, |
|
11
|
arrayMove, |
|
12
|
useSortable, |
|
13
|
verticalListSortingStrategy, |
|
14
|
} from "@dnd-kit/sortable" |
|
15
|
import { CSS } from "@dnd-kit/utilities" |
|
16
|
import { DotsSix, Eye, EyeSlash } from "@medusajs/icons" |
|
17
|
import { IconButton, Kbd, Text, clx } from "@medusajs/ui" |
|
18
|
import { Collapsible as RadixCollapsible } from "radix-ui" |
|
19
|
import { |
|
20
|
PropsWithChildren, |
|
21
|
ReactNode, |
|
22
|
useCallback, |
|
23
|
useEffect, |
|
24
|
useState, |
|
25
|
} from "react" |
|
26
|
import { useTranslation } from "react-i18next" |
|
27
|
import { NavLink, To, useLocation } from "react-router-dom" |
|
28
|
import { useLayoutEdit } from "../../../hooks/use-layout-edit" |
|
29
|
import { useGlobalShortcuts } from "../../../providers/keybind-provider/hooks" |
|
30
|
import { ConditionalTooltip } from "../../common/conditional-tooltip" |
|
31
|
|
|
32
|
type ItemType = "core" | "extension" | "setting" |
|
33
|
|
|
34
|
type NestedItemProps = { |
|
35
|
label: string |
|
36
|
to: string |
|
37
|
translationNs?: string |
|
38
|
} |
|
39
|
|
|
40
|
export type INavItem = { |
|
41
|
icon?: ReactNode |
|
42
|
label: string |
|
43
|
to: string |
|
44
|
items?: NestedItemProps[] |
|
45
|
type?: ItemType |
|
46
|
from?: string |
|
47
|
nested?: string |
|
48
|
translationNs?: string |
|
49
|
} |
|
50
|
|
|
51
|
const BASE_NAV_LINK_CLASSES = |
|
52
|
"text-ui-fg-subtle transition-fg hover:bg-ui-bg-subtle-hover flex items-center gap-x-2 rounded-md py-0.5 pl-0.5 pr-2 outline-none [&>svg]:text-ui-fg-subtle focus-visible:shadow-borders-focus" |
|
53
|
const ACTIVE_NAV_LINK_CLASSES = |
|
54
|
"bg-ui-bg-base shadow-elevation-card-rest text-ui-fg-base hover:bg-ui-bg-base" |
|
55
|
const NESTED_NAV_LINK_CLASSES = "pl-[34px] pr-2 py-1 w-full text-ui-fg-muted" |
|
56
|
const SETTING_NAV_LINK_CLASSES = "pl-2 py-1" |
|
57
|
|
|
58
|
// Prefix match that respects path-segment boundaries, so that a route like |
|
59
|
// "/inventory-sources" is not considered a match for "/inventory". This keeps |
|
60
|
// nested-route highlighting (e.g. "/inventory/123") while avoiding false |
|
61
|
// positives for sibling routes that merely share a string prefix. |
|
62
|
const isPathActive = (pathname: string, to: string) => { |
|
63
|
return pathname === to || pathname.startsWith(to + "/") |
|
64
|
} |
|
65
|
|
|
66
|
const getIsOpen = ( |
|
67
|
to: string, |
|
68
|
items: NestedItemProps[] | undefined, |
|
69
|
pathname: string |
|
70
|
) => { |
|
71
|
return [to, ...(items?.map((i) => i.to) ?? [])].some((p) => |
|
72
|
isPathActive(pathname, p) |
|
73
|
) |
|
74
|
} |
|
75
|
|
|
76
|
const NavItemTooltip = ({ |
|
77
|
to, |
|
78
|
children, |
|
79
|
}: PropsWithChildren<{ to: string }>) => { |
|
80
|
const { t } = useTranslation() |
|
81
|
const globalShortcuts = useGlobalShortcuts() |
|
82
|
const shortcut = globalShortcuts.find((s) => s.to === to) |
|
83
|
|
|
84
|
return ( |
|
85
|
<ConditionalTooltip |
|
86
|
showTooltip={!!shortcut} |
|
87
|
maxWidth={9999} // Don't limit the width of the tooltip |
|
88
|
content={ |
|
89
|
<div className="txt-compact-xsmall flex h-5 items-center justify-between gap-x-2 whitespace-nowrap"> |
|
90
|
<span>{shortcut?.label}</span> |
|
91
|
<div className="flex items-center gap-x-1"> |
|
92
|
{shortcut?.keys.Mac?.map((key, index) => ( |
|
93
|
<div className="flex items-center gap-x-1" key={index}> |
|
94
|
<Kbd key={key}>{key}</Kbd> |
|
95
|
{index < (shortcut.keys.Mac?.length || 0) - 1 && ( |
|
96
|
<span className="text-ui-fg-muted txt-compact-xsmall"> |
|
97
|
{t("app.keyboardShortcuts.then")} |
|
98
|
</span> |
|
99
|
)} |
|
100
|
</div> |
|
101
|
))} |
|
102
|
</div> |
|
103
|
</div> |
|
104
|
} |
|
105
|
side="right" |
|
106
|
delayDuration={1500} |
|
107
|
> |
|
108
|
<div className="w-full">{children}</div> |
|
109
|
</ConditionalTooltip> |
|
110
|
) |
|
111
|
} |
|
112
|
|
|
113
|
type SubItemLinkProps = { |
|
114
|
item: NestedItemProps |
|
115
|
isSetting: boolean |
|
116
|
navLinkClassNames: (props: { |
|
117
|
to: string |
|
118
|
isActive: boolean |
|
119
|
isNested?: boolean |
|
120
|
isSetting?: boolean |
|
121
|
}) => string |
|
122
|
getLinkTarget: (target: string) => To |
|
123
|
} |
|
124
|
|
|
125
|
const SubItemLink = ({ |
|
126
|
item, |
|
127
|
isSetting, |
|
128
|
navLinkClassNames, |
|
129
|
getLinkTarget, |
|
130
|
}: SubItemLinkProps) => { |
|
131
|
const { t } = useTranslation(item.translationNs as any) |
|
132
|
const itemLabel: string = item.translationNs ? t(item.label) : item.label |
|
133
|
|
|
134
|
return ( |
|
135
|
<NavItemTooltip to={item.to}> |
|
136
|
<NavLink |
|
137
|
to={getLinkTarget(item.to)} |
|
138
|
end |
|
139
|
className={({ isActive }) => { |
|
140
|
return clx( |
|
141
|
navLinkClassNames({ |
|
142
|
to: item.to, |
|
143
|
isActive, |
|
144
|
isSetting, |
|
145
|
isNested: true, |
|
146
|
}) |
|
147
|
) |
|
148
|
}} |
|
149
|
> |
|
150
|
<Text size="small" weight="plus" leading="compact"> |
|
151
|
{itemLabel} |
|
152
|
</Text> |
|
153
|
</NavLink> |
|
154
|
</NavItemTooltip> |
|
155
|
) |
|
156
|
} |
|
157
|
|
|
158
|
const NavItemSubItem = (props: SubItemLinkProps) => { |
|
159
|
return ( |
|
160
|
<li className="flex h-7 items-center"> |
|
161
|
<SubItemLink {...props} /> |
|
162
|
</li> |
|
163
|
) |
|
164
|
} |
|
165
|
|
|
166
|
const SortableSubItem = ({ |
|
167
|
id, |
|
168
|
hidden, |
|
169
|
onToggleHidden, |
|
170
|
...props |
|
171
|
}: SubItemLinkProps & { |
|
172
|
id: string |
|
173
|
hidden: boolean |
|
174
|
onToggleHidden: () => void |
|
175
|
}) => { |
|
176
|
const { t } = useTranslation() |
|
177
|
const { |
|
178
|
attributes, |
|
179
|
listeners, |
|
180
|
setNodeRef, |
|
181
|
transform, |
|
182
|
transition, |
|
183
|
isDragging, |
|
184
|
} = useSortable({ id }) |
|
185
|
|
|
186
|
const style = { |
|
187
|
transform: CSS.Translate.toString(transform), |
|
188
|
transition, |
|
189
|
} |
|
190
|
|
|
191
|
return ( |
|
192
|
<li |
|
193
|
ref={setNodeRef} |
|
194
|
style={style} |
|
195
|
className={clx( |
|
196
|
"flex h-7 items-center gap-x-1", |
|
197
|
isDragging && "opacity-50" |
|
198
|
)} |
|
199
|
> |
|
200
|
<button |
|
201
|
type="button" |
|
202
|
className="text-ui-fg-muted cursor-grab touch-none rounded p-0.5 focus:outline-none [&>svg]:h-3.5 [&>svg]:w-3.5" |
|
203
|
{...attributes} |
|
204
|
{...listeners} |
|
205
|
aria-label={t("layout.dragToReorder")} |
|
206
|
> |
|
207
|
<DotsSix /> |
|
208
|
</button> |
|
209
|
<IconButton |
|
210
|
size="2xsmall" |
|
211
|
variant="transparent" |
|
212
|
onClick={onToggleHidden} |
|
213
|
aria-label={hidden ? t("actions.show") : t("actions.hide")} |
|
214
|
className="h-3.5 w-3.5 p-0.5" |
|
215
|
> |
|
216
|
{hidden ? <EyeSlash /> : <Eye />} |
|
217
|
</IconButton> |
|
218
|
<div className={clx("min-w-0 flex-1", hidden && "opacity-30 grayscale")}> |
|
219
|
<SubItemLink {...props} /> |
|
220
|
</div> |
|
221
|
</li> |
|
222
|
) |
|
223
|
} |
|
224
|
|
|
225
|
/** |
|
226
|
* Renders a route's nested children, applying the saved order. In layout edit |
|
227
|
* mode they become a self-contained vertical sortable (its own `DndContext`, so |
|
228
|
* it doesn't interfere with the composer's entry-level drag), scoped to this |
|
229
|
* route — child ids embed the parent path, so moves never cross routes. |
|
230
|
*/ |
|
231
|
const NavSubItems = ({ |
|
232
|
parentTo, |
|
233
|
items, |
|
234
|
isSetting, |
|
235
|
navLinkClassNames, |
|
236
|
getLinkTarget, |
|
237
|
}: { |
|
238
|
parentTo: string |
|
239
|
items: NestedItemProps[] |
|
240
|
isSetting: boolean |
|
241
|
navLinkClassNames: SubItemLinkProps["navLinkClassNames"] |
|
242
|
getLinkTarget: SubItemLinkProps["getLinkTarget"] |
|
243
|
}) => { |
|
244
|
const { editMode, orderChildren, setChildrenOrder, isHidden, toggleHidden } = |
|
245
|
useLayoutEdit() |
|
246
|
const sensors = useSensors( |
|
247
|
useSensor(PointerSensor, { activationConstraint: { distance: 4 } }) |
|
248
|
) |
|
249
|
const childId = useCallback( |
|
250
|
(to: string) => `nav-child:${parentTo}:${to}`, |
|
251
|
[parentTo] |
|
252
|
) |
|
253
|
|
|
254
|
const ordered = orderChildren(items, (it) => childId(it.to)) |
|
255
|
const shared = { isSetting, navLinkClassNames, getLinkTarget } |
|
256
|
|
|
257
|
if (!editMode) { |
|
258
|
return ( |
|
259
|
<> |
|
260
|
{ordered |
|
261
|
.filter((item) => !isHidden(childId(item.to))) |
|
262
|
.map((item) => ( |
|
263
|
<NavItemSubItem key={item.to} item={item} {...shared} /> |
|
264
|
))} |
|
265
|
</> |
|
266
|
) |
|
267
|
} |
|
268
|
|
|
269
|
const ids = ordered.map((it) => childId(it.to)) |
|
270
|
|
|
271
|
const handleDragEnd = (event: DragEndEvent) => { |
|
272
|
const { active, over } = event |
|
273
|
if (!over || active.id === over.id) { |
|
274
|
return |
|
275
|
} |
|
276
|
const oldIndex = ids.indexOf(active.id as string) |
|
277
|
const newIndex = ids.indexOf(over.id as string) |
|
278
|
if (oldIndex < 0 || newIndex < 0) { |
|
279
|
return |
|
280
|
} |
|
281
|
setChildrenOrder(arrayMove(ids, oldIndex, newIndex)) |
|
282
|
} |
|
283
|
|
|
284
|
return ( |
|
285
|
<DndContext |
|
286
|
sensors={sensors} |
|
287
|
collisionDetection={closestCenter} |
|
288
|
onDragEnd={handleDragEnd} |
|
289
|
> |
|
290
|
<SortableContext items={ids} strategy={verticalListSortingStrategy}> |
|
291
|
{ordered.map((item) => ( |
|
292
|
<SortableSubItem |
|
293
|
key={item.to} |
|
294
|
id={childId(item.to)} |
|
295
|
item={item} |
|
296
|
hidden={isHidden(childId(item.to))} |
|
297
|
onToggleHidden={() => toggleHidden(childId(item.to))} |
|
298
|
{...shared} |
|
299
|
/> |
|
300
|
))} |
|
301
|
</SortableContext> |
|
302
|
</DndContext> |
|
303
|
) |
|
304
|
} |
|
305
|
|
|
306
|
export const NavItem = ({ |
|
307
|
icon, |
|
308
|
label, |
|
309
|
to, |
|
310
|
items, |
|
311
|
type = "core", |
|
312
|
from, |
|
313
|
translationNs, |
|
314
|
}: INavItem) => { |
|
315
|
const { t } = useTranslation(translationNs as any) |
|
316
|
const { pathname, search } = useLocation() |
|
317
|
const [open, setOpen] = useState(getIsOpen(to, items, pathname)) |
|
318
|
// While the layout is being edited, force the nested list open so every |
|
319
|
// route's children are visible and reorderable, not just the active route's. |
|
320
|
const { editMode } = useLayoutEdit() |
|
321
|
|
|
322
|
// Use translation if translationNs is provided, otherwise use label as-is |
|
323
|
const displayLabel: string = translationNs ? t(label) : label |
|
324
|
|
|
325
|
useEffect(() => { |
|
326
|
setOpen(getIsOpen(to, items, pathname)) |
|
327
|
}, [pathname, to, items]) |
|
328
|
|
|
329
|
const navLinkClassNames = useCallback( |
|
330
|
({ |
|
331
|
to, |
|
332
|
isActive, |
|
333
|
isNested = false, |
|
334
|
isSetting = false, |
|
335
|
}: { |
|
336
|
to: string |
|
337
|
isActive: boolean |
|
338
|
isNested?: boolean |
|
339
|
isSetting?: boolean |
|
340
|
}) => { |
|
341
|
if (["core", "setting"].includes(type)) { |
|
342
|
isActive = isPathActive(pathname, to) |
|
343
|
} |
|
344
|
|
|
345
|
return clx(BASE_NAV_LINK_CLASSES, { |
|
346
|
[NESTED_NAV_LINK_CLASSES]: isNested, |
|
347
|
[ACTIVE_NAV_LINK_CLASSES]: isActive && !editMode, |
|
348
|
[SETTING_NAV_LINK_CLASSES]: isSetting, |
|
349
|
}) |
|
350
|
}, |
|
351
|
[type, pathname, editMode] |
|
352
|
) |
|
353
|
|
|
354
|
const getLinkTarget = useCallback( |
|
355
|
(target: string) => { |
|
356
|
if (pathname === target && search) { |
|
357
|
return { pathname: target, search } |
|
358
|
} |
|
359
|
|
|
360
|
return target |
|
361
|
}, |
|
362
|
[pathname, search] |
|
363
|
) |
|
364
|
|
|
365
|
const isSetting = type === "setting" |
|
366
|
|
|
367
|
return ( |
|
368
|
<div> |
|
369
|
<NavItemTooltip to={to}> |
|
370
|
<NavLink |
|
371
|
to={getLinkTarget(to)} |
|
372
|
end={items?.some((i) => i.to === pathname)} |
|
373
|
state={ |
|
374
|
from |
|
375
|
? { |
|
376
|
from, |
|
377
|
} |
|
378
|
: undefined |
|
379
|
} |
|
380
|
className={({ isActive }) => { |
|
381
|
return clx(navLinkClassNames({ isActive, isSetting, to }), { |
|
382
|
"max-lg:hidden": !!items?.length, |
|
383
|
}) |
|
384
|
}} |
|
385
|
> |
|
386
|
{type !== "setting" && ( |
|
387
|
<div className="flex size-6 items-center justify-center"> |
|
388
|
<Icon icon={icon} type={type} /> |
|
389
|
</div> |
|
390
|
)} |
|
391
|
<Text size="small" weight="plus" leading="compact"> |
|
392
|
{displayLabel} |
|
393
|
</Text> |
|
394
|
</NavLink> |
|
395
|
</NavItemTooltip> |
|
396
|
{items && items.length > 0 && ( |
|
397
|
<RadixCollapsible.Root open={editMode || open} onOpenChange={setOpen}> |
|
398
|
<RadixCollapsible.Trigger |
|
399
|
className={clx( |
|
400
|
"text-ui-fg-subtle hover:text-ui-fg-base transition-fg hover:bg-ui-bg-subtle-hover flex w-full items-center gap-x-2 rounded-md py-0.5 pl-0.5 pr-2 outline-none lg:hidden", |
|
401
|
{ "pl-2": isSetting } |
|
402
|
)} |
|
403
|
> |
|
404
|
<div className="flex size-6 items-center justify-center"> |
|
405
|
<Icon icon={icon} type={type} /> |
|
406
|
</div> |
|
407
|
<Text size="small" weight="plus" leading="compact"> |
|
408
|
{displayLabel} |
|
409
|
</Text> |
|
410
|
</RadixCollapsible.Trigger> |
|
411
|
<RadixCollapsible.Content> |
|
412
|
<div className="flex flex-col gap-y-0.5 pb-2 pt-0.5"> |
|
413
|
<ul className="flex flex-col gap-y-0.5"> |
|
414
|
<li className="flex w-full items-center gap-x-1 lg:hidden"> |
|
415
|
<NavItemTooltip to={to}> |
|
416
|
<NavLink |
|
417
|
to={getLinkTarget(to)} |
|
418
|
end |
|
419
|
className={({ isActive }) => { |
|
420
|
return clx( |
|
421
|
navLinkClassNames({ |
|
422
|
to, |
|
423
|
isActive, |
|
424
|
isSetting, |
|
425
|
isNested: true, |
|
426
|
}) |
|
427
|
) |
|
428
|
}} |
|
429
|
> |
|
430
|
<Text size="small" weight="plus" leading="compact"> |
|
431
|
{displayLabel} |
|
432
|
</Text> |
|
433
|
</NavLink> |
|
434
|
</NavItemTooltip> |
|
435
|
</li> |
|
436
|
<NavSubItems |
|
437
|
parentTo={to} |
|
438
|
items={items} |
|
439
|
isSetting={isSetting} |
|
440
|
navLinkClassNames={navLinkClassNames} |
|
441
|
getLinkTarget={getLinkTarget} |
|
442
|
/> |
|
443
|
</ul> |
|
444
|
</div> |
|
445
|
</RadixCollapsible.Content> |
|
446
|
</RadixCollapsible.Root> |
|
447
|
)} |
|
448
|
</div> |
|
449
|
) |
|
450
|
} |
|
451
|
|
|
452
|
const Icon = ({ icon, type }: { icon?: ReactNode; type: ItemType }) => { |
|
453
|
if (!icon) { |
|
454
|
return null |
|
455
|
} |
|
456
|
|
|
457
|
return type === "extension" ? ( |
|
458
|
<div className="shadow-borders-base bg-ui-bg-base flex h-5 w-5 items-center justify-center rounded-[4px]"> |
|
459
|
<div className="h-[15px] w-[15px] overflow-hidden rounded-sm">{icon}</div> |
|
460
|
</div> |
|
461
|
) : ( |
|
462
|
icon |
|
463
|
) |
|
464
|
} |