|
1
|
import { Adjustments, AdjustmentsDone } from "@medusajs/icons" |
|
2
|
import { DropdownMenu, IconButton, Tooltip } from "@medusajs/ui" |
|
3
|
import { useCallback, useEffect } from "react" |
|
4
|
import { useTranslation } from "react-i18next" |
|
5
|
import { useLocation, useNavigate } from "react-router-dom" |
|
6
|
import { useHasLayoutCustomizations } from "../../hooks/api/layouts" |
|
7
|
import { |
|
8
|
useLayoutCustomizerActiveEditor, |
|
9
|
useLayoutEditRequest, |
|
10
|
} from "../../hooks/use-layout-customizer-trigger-host" |
|
11
|
import { CUSTOMIZE_IDS } from "./constants" |
|
12
|
|
|
13
|
type CustomizeHost = { |
|
14
|
/** Matches a `LayoutComposer`'s `customizeId`. */ |
|
15
|
id: string |
|
16
|
/** The key to translate the label. */ |
|
17
|
labelKey: string |
|
18
|
/** Route to open when the host isn't mounted on the current screen. */ |
|
19
|
navigateTo?: string |
|
20
|
/** Whether this host's composer is mounted for the given path. */ |
|
21
|
isMounted: (pathname: string) => boolean |
|
22
|
} |
|
23
|
|
|
24
|
const isSettingsPath = (pathname: string): boolean => |
|
25
|
pathname === "/settings" || pathname.startsWith("/settings/") |
|
26
|
|
|
27
|
/** |
|
28
|
* The customizable hosts, in menu order. Page and top bar are present on every |
|
29
|
* screen; the two sidebars are mounted by mutually exclusive route layouts |
|
30
|
* (settings routes vs. everything else), so customizing the off-screen one |
|
31
|
* navigates to it first — the request rides along in `location.state` and is |
|
32
|
* consumed once that layout mounts. |
|
33
|
*/ |
|
34
|
const HOSTS: CustomizeHost[] = [ |
|
35
|
{ |
|
36
|
id: CUSTOMIZE_IDS.PAGE, |
|
37
|
labelKey: "layout.customizePage", |
|
38
|
isMounted: () => true, |
|
39
|
}, |
|
40
|
{ |
|
41
|
id: CUSTOMIZE_IDS.MAIN_SIDEBAR, |
|
42
|
labelKey: "layout.customizeSidebar", |
|
43
|
navigateTo: "/", |
|
44
|
isMounted: (pathname) => !isSettingsPath(pathname), |
|
45
|
}, |
|
46
|
{ |
|
47
|
id: CUSTOMIZE_IDS.SETTINGS_SIDEBAR, |
|
48
|
labelKey: "layout.customizeSettingsSidebar", |
|
49
|
navigateTo: "/settings", |
|
50
|
isMounted: isSettingsPath, |
|
51
|
}, |
|
52
|
{ |
|
53
|
id: CUSTOMIZE_IDS.TOPBAR, |
|
54
|
labelKey: "layout.customizeTopbar", |
|
55
|
isMounted: () => true, |
|
56
|
}, |
|
57
|
] |
|
58
|
|
|
59
|
/** |
|
60
|
* The single entry point for layout customization, mounted in the shell's top |
|
61
|
* bar. Lists every customizable host; choosing one drives its `LayoutComposer` |
|
62
|
* into edit mode (navigating first if that host lives on another screen). While |
|
63
|
* a composer is editing, this menu hides and its Cancel/Save controls take the |
|
64
|
* adjacent slot. |
|
65
|
*/ |
|
66
|
export const CustomizerMenu = () => { |
|
67
|
const { t } = useTranslation() |
|
68
|
const navigate = useNavigate() |
|
69
|
const location = useLocation() |
|
70
|
const { activeEditor } = useLayoutCustomizerActiveEditor() |
|
71
|
const { requestEdit } = useLayoutEditRequest() |
|
72
|
const { has_customizations } = useHasLayoutCustomizations() |
|
73
|
|
|
74
|
// Consume a request carried across navigation: once the requested host's |
|
75
|
// layout has mounted here, fire its edit mode and strip the marker so a |
|
76
|
// refresh or back-navigation doesn't re-trigger it. |
|
77
|
useEffect(() => { |
|
78
|
const state = (location.state ?? null) as { customize?: string } | null |
|
79
|
const pending = state?.customize |
|
80
|
if (!pending) { |
|
81
|
return |
|
82
|
} |
|
83
|
requestEdit(pending) |
|
84
|
const { customize: _customize, ...rest } = state as Record<string, unknown> |
|
85
|
navigate(location.pathname, { |
|
86
|
replace: true, |
|
87
|
state: Object.keys(rest).length ? rest : undefined, |
|
88
|
}) |
|
89
|
}, [location, navigate, requestEdit]) |
|
90
|
|
|
91
|
const onSelect = useCallback( |
|
92
|
(host: CustomizeHost) => { |
|
93
|
if (host.isMounted(location.pathname)) { |
|
94
|
requestEdit(host.id) |
|
95
|
return |
|
96
|
} |
|
97
|
navigate(host.navigateTo ?? location.pathname, { |
|
98
|
state: { ...(location.state as object | null), customize: host.id }, |
|
99
|
}) |
|
100
|
}, |
|
101
|
[location, navigate, requestEdit] |
|
102
|
) |
|
103
|
|
|
104
|
// A composer is editing; its controls occupy the slot in place of this menu. |
|
105
|
if (activeEditor) { |
|
106
|
return null |
|
107
|
} |
|
108
|
|
|
109
|
return ( |
|
110
|
<DropdownMenu> |
|
111
|
<Tooltip content={t("layout.customizeLayout")}> |
|
112
|
<DropdownMenu.Trigger asChild> |
|
113
|
<IconButton |
|
114
|
size="small" |
|
115
|
variant="transparent" |
|
116
|
aria-label={t("layout.customizeLayout")} |
|
117
|
className="text-ui-fg-muted hover:text-ui-fg-subtle" |
|
118
|
> |
|
119
|
{has_customizations ? <AdjustmentsDone /> : <Adjustments />} |
|
120
|
</IconButton> |
|
121
|
</DropdownMenu.Trigger> |
|
122
|
</Tooltip> |
|
123
|
<DropdownMenu.Content> |
|
124
|
{HOSTS.map((host) => ( |
|
125
|
<DropdownMenu.Item key={host.id} onClick={() => onSelect(host)}> |
|
126
|
{t(host.labelKey as any)} |
|
127
|
</DropdownMenu.Item> |
|
128
|
))} |
|
129
|
</DropdownMenu.Content> |
|
130
|
</DropdownMenu> |
|
131
|
) |
|
132
|
} |