1 import { EllipsisHorizontal } from "@medusajs/icons";
2 import { DropdownMenu, IconButton, clx } from "@medusajs/ui";
3 import { Link } from "react-router-dom";
4
5 export type Action = {
6 icon: React.ReactNode;
7 label: string;
8 disabled?: boolean;
9 } & (
10 | {
11 to: string;
12 onClick?: never;
13 }
14 | {
15 onClick: () => void;
16 to?: never;
17 }
18 );
19
20 export type ActionGroup = {
21 actions: Action[];
22 };
23
24 export type ActionMenuProps = {
25 groups: ActionGroup[];
26 };
27
28 export const ActionMenu = ({ groups }: ActionMenuProps) => {
29 return (
30 <DropdownMenu>
31 <DropdownMenu.Trigger asChild>
32 <IconButton size="small" variant="transparent">
33 <EllipsisHorizontal />
34 </IconButton>
35 </DropdownMenu.Trigger>
36 <DropdownMenu.Content>
37 {groups.map((group, index) => {
38 if (!group.actions.length) {
39 return null;
40 }
41
42 const isLast = index === groups.length - 1;
43
44 return (
45 <DropdownMenu.Group key={index}>
46 {group.actions.map((action, index) => {
47 if (action.onClick) {
48 return (
49 <DropdownMenu.Item
50 disabled={action.disabled}
51 key={index}
52 onClick={(e) => {
53 e.stopPropagation();
54 action.onClick();
55 }}
56 className={clx(
57 "[&_svg]:text-ui-fg-subtle flex items-center gap-x-2",
58 {
59 "[&_svg]:text-ui-fg-disabled": action.disabled,
60 }
61 )}
62 >
63 {action.icon}
64 <span>{action.label}</span>
65 </DropdownMenu.Item>
66 );
67 }
68
69 return (
70 <div key={index}>
71 <DropdownMenu.Item
72 className={clx(
73 "[&_svg]:text-ui-fg-subtle flex items-center gap-x-2",
74 {
75 "[&_svg]:text-ui-fg-disabled": action.disabled,
76 }
77 )}
78 asChild
79 disabled={action.disabled}
80 >
81 <Link to={action.to} onClick={(e) => e.stopPropagation()}>
82 {action.icon}
83 <span>{action.label}</span>
84 </Link>
85 </DropdownMenu.Item>
86 </div>
87 );
88 })}
89 {!isLast && <DropdownMenu.Separator />}
90 </DropdownMenu.Group>
91 );
92 })}
93 </DropdownMenu.Content>
94 </DropdownMenu>
95 );
96 };