Raw
1 import {
2 BookOpen,
3 CircleHalfSolid,
4 EllipsisHorizontal,
5 Keyboard,
6 OpenRectArrowOut,
7 TimelineVertical,
8 User as UserIcon,
9 XMark,
10 } from "@medusajs/icons"
11 import {
12 Avatar,
13 DropdownMenu,
14 Heading,
15 IconButton,
16 Input,
17 Kbd,
18 Text,
19 clx,
20 } from "@medusajs/ui"
21 import { Dialog as RadixDialog } from "radix-ui"
22 import { useTranslation } from "react-i18next"
23
24 import { Skeleton } from "../../common/skeleton"
25
26 import { useState } from "react"
27 import { Link, useLocation, useNavigate } from "react-router-dom"
28 import { useLogout, useMe } from "../../../hooks/api"
29 import { queryClient } from "../../../lib/query-client"
30 import { useGlobalShortcuts } from "../../../providers/keybind-provider/hooks"
31 import { useTheme } from "../../../providers/theme-provider"
32 import { useDocumentDirection } from "../../../hooks/use-document-direction"
33
34 export const UserMenu = () => {
35 const { t } = useTranslation()
36 const location = useLocation()
37 const direction = useDocumentDirection()
38
39 const [openMenu, setOpenMenu] = useState(false)
40 const [openModal, setOpenModal] = useState(false)
41
42 const toggleModal = () => {
43 setOpenMenu(false)
44 setOpenModal(!openModal)
45 }
46
47 return (
48 <div>
49 <DropdownMenu dir={direction} open={openMenu} onOpenChange={setOpenMenu}>
50 <UserBadge />
51 <DropdownMenu.Content className="min-w-[var(--radix-dropdown-menu-trigger-width)] max-w-[var(--radix-dropdown-menu-trigger-width)]">
52 <UserItem />
53 <DropdownMenu.Separator />
54 <DropdownMenu.Item asChild>
55 <Link to="/settings/profile" state={{ from: location.pathname }}>
56 <UserIcon className="text-ui-fg-subtle me-2" />
57 {t("app.menus.user.profileSettings")}
58 </Link>
59 </DropdownMenu.Item>
60 <DropdownMenu.Separator />
61 <DropdownMenu.Item asChild>
62 <Link to="https://docs.medusajs.com" target="_blank">
63 <BookOpen className="text-ui-fg-subtle me-2" />
64 {t("app.menus.user.documentation")}
65 </Link>
66 </DropdownMenu.Item>
67 <DropdownMenu.Item asChild>
68 <Link to="https://medusajs.com/changelog/" target="_blank">
69 <TimelineVertical className="text-ui-fg-subtle me-2" />
70 {t("app.menus.user.changelog")}
71 </Link>
72 </DropdownMenu.Item>
73 <DropdownMenu.Separator />
74 <DropdownMenu.Item onClick={toggleModal}>
75 <Keyboard className="text-ui-fg-subtle me-2" />
76 {t("app.menus.user.shortcuts")}
77 </DropdownMenu.Item>
78 <ThemeToggle />
79 <DropdownMenu.Separator />
80 <Logout />
81 </DropdownMenu.Content>
82 </DropdownMenu>
83 <GlobalKeybindsModal open={openModal} onOpenChange={setOpenModal} />
84 </div>
85 )
86 }
87
88 const UserBadge = () => {
89 const { user, isPending, isError, error } = useMe()
90
91 const name = [user?.first_name, user?.last_name].filter(Boolean).join(" ")
92 const displayName = name || user?.email
93
94 const fallback = displayName ? displayName[0].toUpperCase() : null
95
96 if (isPending) {
97 return (
98 <button className="shadow-borders-base flex max-w-[192px] select-none items-center gap-x-2 overflow-hidden text-ellipsis whitespace-nowrap rounded-full py-1 pe-2.5 ps-1">
99 <Skeleton className="h-5 w-5 rounded-full" />
100 <Skeleton className="h-[9px] w-[70px]" />
101 </button>
102 )
103 }
104
105 if (isError) {
106 throw error
107 }
108
109 return (
110 <div className="p-3">
111 <DropdownMenu.Trigger
112 disabled={!user}
113 className={clx(
114 "bg-ui-bg-subtle grid w-full cursor-pointer grid-cols-[24px_1fr_15px] items-center gap-2 rounded-md py-1 pe-2 ps-0.5 outline-none",
115 "hover:bg-ui-bg-subtle-hover",
116 "data-[state=open]:bg-ui-bg-subtle-hover",
117 "focus-visible:shadow-borders-focus"
118 )}
119 >
120 <div className="flex size-6 items-center justify-center">
121 {fallback ? (
122 <Avatar size="xsmall" fallback={fallback} />
123 ) : (
124 <Skeleton className="h-6 w-6 rounded-full" />
125 )}
126 </div>
127 <div className="flex items-center overflow-hidden">
128 {displayName ? (
129 <Text
130 size="xsmall"
131 weight="plus"
132 leading="compact"
133 className="truncate"
134 >
135 {displayName}
136 </Text>
137 ) : (
138 <Skeleton className="h-[9px] w-[70px]" />
139 )}
140 </div>
141 <EllipsisHorizontal className="text-ui-fg-muted" />
142 </DropdownMenu.Trigger>
143 </div>
144 )
145 }
146
147 const ThemeToggle = () => {
148 const { t } = useTranslation()
149 const { theme, setTheme } = useTheme()
150
151 return (
152 <DropdownMenu.SubMenu>
153 <DropdownMenu.SubMenuTrigger
154 dir="ltr"
155 className="rounded-md rtl:rotate-180"
156 >
157 <CircleHalfSolid className="text-ui-fg-subtle me-2" />
158 <span className="rtl:rotate-180">
159 {t("app.menus.user.theme.label")}
160 </span>
161 </DropdownMenu.SubMenuTrigger>
162 <DropdownMenu.SubMenuContent>
163 <DropdownMenu.RadioGroup value={theme}>
164 <DropdownMenu.RadioItem
165 value="system"
166 onClick={(e) => {
167 e.preventDefault()
168 setTheme("system")
169 }}
170 >
171 {t("app.menus.user.theme.system")}
172 </DropdownMenu.RadioItem>
173 <DropdownMenu.RadioItem
174 value="light"
175 onClick={(e) => {
176 e.preventDefault()
177 setTheme("light")
178 }}
179 >
180 {t("app.menus.user.theme.light")}
181 </DropdownMenu.RadioItem>
182 <DropdownMenu.RadioItem
183 value="dark"
184 onClick={(e) => {
185 e.preventDefault()
186 setTheme("dark")
187 }}
188 >
189 {t("app.menus.user.theme.dark")}
190 </DropdownMenu.RadioItem>
191 </DropdownMenu.RadioGroup>
192 </DropdownMenu.SubMenuContent>
193 </DropdownMenu.SubMenu>
194 )
195 }
196
197 const Logout = () => {
198 const { t } = useTranslation()
199 const navigate = useNavigate()
200
201 const { mutateAsync: logoutMutation } = useLogout()
202
203 const handleLogout = async () => {
204 await logoutMutation(undefined, {
205 onSuccess: () => {
206 /**
207 * When the user logs out, we want to clear the query cache
208 */
209 queryClient.clear()
210 navigate("/login")
211 },
212 })
213 }
214
215 return (
216 <DropdownMenu.Item onClick={handleLogout}>
217 <div className="flex items-center gap-x-2">
218 <OpenRectArrowOut className="text-ui-fg-subtle" />
219 <span>{t("app.menus.actions.logout")}</span>
220 </div>
221 </DropdownMenu.Item>
222 )
223 }
224
225 const GlobalKeybindsModal = (props: {
226 open: boolean
227 onOpenChange: (open: boolean) => void
228 }) => {
229 const { t } = useTranslation()
230 const globalShortcuts = useGlobalShortcuts()
231
232 const [searchValue, onSearchValueChange] = useState("")
233
234 const searchResults = searchValue
235 ? globalShortcuts.filter((shortcut) => {
236 return shortcut.label.toLowerCase().includes(searchValue?.toLowerCase())
237 })
238 : globalShortcuts
239
240 return (
241 <RadixDialog.Root {...props}>
242 <RadixDialog.Portal>
243 <RadixDialog.Overlay className="bg-ui-bg-overlay fixed inset-0" />
244 <RadixDialog.Content className="bg-ui-bg-subtle shadow-elevation-modal fixed left-[50%] top-[50%] flex h-full max-h-[612px] w-full max-w-[560px] translate-x-[-50%] translate-y-[-50%] flex-col divide-y overflow-hidden rounded-lg">
245 <div className="flex flex-col gap-y-3 px-6 py-4">
246 <div className="flex items-center justify-between">
247 <div>
248 <RadixDialog.Title asChild>
249 <Heading>{t("app.menus.user.shortcuts")}</Heading>
250 </RadixDialog.Title>
251 <RadixDialog.Description className="sr-only"></RadixDialog.Description>
252 </div>
253 <div className="flex items-center gap-x-2">
254 <Kbd>esc</Kbd>
255 <RadixDialog.Close asChild>
256 <IconButton variant="transparent" size="small">
257 <XMark />
258 </IconButton>
259 </RadixDialog.Close>
260 </div>
261 </div>
262 <div>
263 <Input
264 type="search"
265 value={searchValue}
266 onChange={(e) => onSearchValueChange(e.target.value)}
267 />
268 </div>
269 </div>
270 <div className="flex flex-col divide-y overflow-y-auto">
271 {searchResults.map((shortcut, index) => {
272 return (
273 <div
274 key={index}
275 className="text-ui-fg-subtle flex items-center justify-between px-6 py-3"
276 >
277 <Text size="small">{shortcut.label}</Text>
278 <div className="flex items-center gap-x-1">
279 {shortcut.keys.Mac?.map((key, index) => {
280 return (
281 <div className="flex items-center gap-x-1" key={index}>
282 <Kbd>{key}</Kbd>
283 {index < (shortcut.keys.Mac?.length || 0) - 1 && (
284 <span className="txt-compact-xsmall text-ui-fg-subtle">
285 {t("app.keyboardShortcuts.then")}
286 </span>
287 )}
288 </div>
289 )
290 })}
291 </div>
292 </div>
293 )
294 })}
295 </div>
296 </RadixDialog.Content>
297 </RadixDialog.Portal>
298 </RadixDialog.Root>
299 )
300 }
301
302 const UserItem = () => {
303 const { user, isPending, isError, error } = useMe()
304
305 const loaded = !isPending && !!user
306
307 if (!loaded) {
308 return <div></div>
309 }
310
311 const name = [user.first_name, user.last_name].filter(Boolean).join(" ")
312 const email = user.email
313 const fallback = name ? name[0].toUpperCase() : email[0].toUpperCase()
314 const avatar = user.avatar_url
315
316 if (isError) {
317 throw error
318 }
319
320 return (
321 <div className="flex items-center gap-x-3 overflow-hidden px-2 py-1">
322 <Avatar
323 size="small"
324 variant="rounded"
325 src={avatar || undefined}
326 fallback={fallback}
327 />
328 <div className="block w-full min-w-0 max-w-[187px] overflow-hidden whitespace-nowrap">
329 <Text
330 size="small"
331 weight="plus"
332 leading="compact"
333 className="overflow-hidden text-ellipsis whitespace-nowrap"
334 >
335 {name || email}
336 </Text>
337 {!!name && (
338 <Text
339 size="xsmall"
340 leading="compact"
341 className="text-ui-fg-subtle overflow-hidden text-ellipsis whitespace-nowrap"
342 >
343 {email}
344 </Text>
345 )}
346 </div>
347 </div>
348 )
349 }