Raw
1 import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
2 import { ArrowUturnLeft } from "@medusajs/icons"
3 import { clx, Divider, Text } from "@medusajs/ui"
4 import { useEffect, useMemo, useState } from "react"
5 import { useTranslation } from "react-i18next"
6 import { Link, useLocation } from "react-router-dom"
7
8 import { useSearchIndexes } from "../../../hooks/api/search-indexes"
9 import { useFeatureFlag } from "../../../providers/feature-flag-provider"
10 import { usePermissions } from "../../../providers/permissions-provider"
11 import { useExtension } from "../../../providers/extension-provider"
12 import { LayoutComposer } from "../../layout-composer"
13 import { CUSTOMIZE_IDS } from "../../layout-composer/constants"
14 import { INavItem, NavItem } from "../nav-item"
15 import { Shell } from "../shell"
16 import { UserMenu } from "../user-menu"
17
18 export const SettingsLayout = () => {
19 return (
20 <Shell>
21 <SettingsSidebar />
22 </Shell>
23 )
24 }
25
26 const useSettingRoutes = (): INavItem[] => {
27 const isTranslationsEnabled = useFeatureFlag("translation")
28 const isRbacEnabled = useFeatureFlag("rbac")
29 const isViewConfigEnabled = useFeatureFlag("view_configurations")
30 const { hasPermission } = usePermissions()
31 const { t } = useTranslation()
32
33 const canReadRoles = isRbacEnabled && hasPermission("rbac_role:read")
34 const canReadPolicies = isRbacEnabled && hasPermission("rbac_policy:read")
35
36 return useMemo(
37 () => [
38 {
39 label: t("store.domain"),
40 to: "/settings/store",
41 },
42 {
43 label: t("users.domain"),
44 to: "/settings/users",
45 },
46 ...(canReadRoles
47 ? [
48 {
49 label: t("roles.domain"),
50 to: "/settings/roles",
51 },
52 ]
53 : []),
54 ...(canReadPolicies
55 ? [
56 {
57 label: t("policies.domain"),
58 to: "/settings/policies",
59 },
60 ]
61 : []),
62 {
63 label: t("regions.domain"),
64 to: "/settings/regions",
65 },
66 {
67 label: t("taxRegions.domain"),
68 to: "/settings/tax-regions",
69 },
70 {
71 label: t("returnReasons.domain"),
72 to: "/settings/return-reasons",
73 },
74 {
75 label: t("refundReasons.domain"),
76 to: "/settings/refund-reasons",
77 },
78 {
79 label: t("salesChannels.domain"),
80 to: "/settings/sales-channels",
81 },
82 {
83 label: t("productTypes.domain"),
84 to: "/settings/product-types",
85 },
86 {
87 label: t("productTags.domain"),
88 to: "/settings/product-tags",
89 },
90 {
91 label: t("stockLocations.domain"),
92 to: "/settings/locations",
93 },
94 ...(isViewConfigEnabled
95 ? [
96 {
97 label: t("propertyLabels.domain", "Property Labels"),
98 to: "/settings/property-labels",
99 },
100 ]
101 : []),
102 ...(isTranslationsEnabled
103 ? [
104 {
105 label: t("translations.domain"),
106 to: "/settings/translations",
107 },
108 ]
109 : []),
110 ],
111 [
112 t,
113 isTranslationsEnabled,
114 isViewConfigEnabled,
115 canReadRoles,
116 canReadPolicies,
117 ]
118 )
119 }
120
121 const useDeveloperRoutes = (): INavItem[] => {
122 const { t } = useTranslation()
123 const { enabled: isSearchEnabled } = useSearchIndexes()
124
125 return useMemo(
126 () => [
127 {
128 label: t("apiKeyManagement.domain.publishable"),
129 to: "/settings/publishable-api-keys",
130 },
131 {
132 label: t("apiKeyManagement.domain.secret"),
133 to: "/settings/secret-api-keys",
134 },
135 {
136 label: t("workflowExecutions.domain"),
137 to: "/settings/workflows",
138 },
139 ...(isSearchEnabled
140 ? [
141 {
142 label: t("searchIndexes.domain"),
143 to: "/settings/search",
144 },
145 ]
146 : []),
147 ],
148 [t, isSearchEnabled]
149 )
150 }
151
152 const useMyAccountRoutes = (): INavItem[] => {
153 const { t } = useTranslation()
154
155 return useMemo(
156 () => [
157 {
158 label: t("profile.domain"),
159 to: "/settings/profile",
160 },
161 ],
162 [t]
163 )
164 }
165
166 /**
167 * Ensure that the `from` prop is not another settings route, to avoid
168 * the user getting stuck in a navigation loop.
169 */
170 const getSafeFromValue = (from: string) => {
171 if (from.startsWith("/settings")) {
172 return "/orders"
173 }
174
175 return from
176 }
177
178 const toNavEntries = (items: INavItem[]) =>
179 items.map((item) => (
180 <LayoutComposer.Entry id={`settings-nav:${item.to}`} key={item.to}>
181 <NavItem key={item.to} type="setting" {...item} />
182 </LayoutComposer.Entry>
183 ))
184
185 const SettingsSidebar = () => {
186 const { getMenu } = useExtension()
187
188 const routes = useSettingRoutes()
189 const developerRoutes = useDeveloperRoutes()
190 const myAccountRoutes = useMyAccountRoutes()
191 const extensionRoutes = getMenu("settingsExtensions")
192
193 return (
194 <aside className="relative flex flex-1 flex-col justify-between overflow-y-auto">
195 <div className="bg-ui-bg-subtle sticky top-0">
196 <Header />
197 <div className="flex items-center justify-center px-3">
198 <Divider variant="dashed" />
199 </div>
200 </div>
201 <div className="flex flex-1 flex-col">
202 <div className="flex flex-1 flex-col overflow-y-auto">
203 <LayoutComposer
204 widgetsZonePrefix="settings.sidebar"
205 preferredLayoutId={CORE_LAYOUT_IDS.SETTINGS_SIDEBAR}
206 hasOutlet={false}
207 disableWidgets
208 customizeId={CUSTOMIZE_IDS.SETTINGS_SIDEBAR}
209 controlSize="small"
210 sections={{
211 general: toNavEntries(routes),
212 developer: toNavEntries(developerRoutes),
213 myAccount: toNavEntries(myAccountRoutes),
214 extensions: toNavEntries(extensionRoutes),
215 }}
216 />
217 </div>
218 <div className="bg-ui-bg-subtle sticky bottom-0">
219 <UserSection />
220 </div>
221 </div>
222 </aside>
223 )
224 }
225
226 const Header = () => {
227 const [from, setFrom] = useState("/orders")
228
229 const { t } = useTranslation()
230 const location = useLocation()
231
232 useEffect(() => {
233 if (location.state?.from) {
234 setFrom(getSafeFromValue(location.state.from))
235 }
236 }, [location])
237
238 return (
239 <div className="bg-ui-bg-subtle p-3">
240 <Link
241 to={from}
242 replace
243 className={clx(
244 "bg-ui-bg-subtle transition-fg flex items-center rounded-md outline-none",
245 "hover:bg-ui-bg-subtle-hover",
246 "focus-visible:shadow-borders-focus"
247 )}
248 >
249 <div className="flex items-center gap-x-2.5 px-2 py-1">
250 <div className="flex items-center justify-center">
251 <ArrowUturnLeft className="text-ui-fg-subtle" />
252 </div>
253 <Text leading="compact" weight="plus" size="small">
254 {t("app.nav.settings.header")}
255 </Text>
256 </div>
257 </Link>
258 </div>
259 )
260 }
261
262 const UserSection = () => {
263 return (
264 <div>
265 <div className="px-3">
266 <Divider variant="dashed" />
267 </div>
268 <UserMenu />
269 </div>
270 )
271 }