|
1
|
import { Key, PencilSquare, Trash } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { Container, Heading, toast, usePrompt } from "@medusajs/ui" |
|
4
|
import { useMemo } from "react" |
|
5
|
import { useTranslation } from "react-i18next" |
|
6
|
import { useNavigate } from "react-router-dom" |
|
7
|
|
|
8
|
import { |
|
9
|
ActionGroup, |
|
10
|
ActionMenu, |
|
11
|
} from "../../../../../components/common/action-menu" |
|
12
|
import { ListSummary } from "../../../../../components/common/list-summary" |
|
13
|
import { SectionRow } from "../../../../../components/common/section" |
|
14
|
import { useDeleteRbacRole } from "../../../../../hooks/api/rbac-roles" |
|
15
|
import { usePermissions } from "../../../../../providers/permissions-provider" |
|
16
|
|
|
17
|
type RoleWithUsers = HttpTypes.AdminRbacRole & { |
|
18
|
users_link?: { user?: HttpTypes.AdminUser | null }[] |
|
19
|
} |
|
20
|
|
|
21
|
type RoleGeneralSectionProps = { |
|
22
|
role: RoleWithUsers |
|
23
|
} |
|
24
|
|
|
25
|
export const RoleGeneralSection = ({ role }: RoleGeneralSectionProps) => { |
|
26
|
const { t } = useTranslation() |
|
27
|
const { hasPermission } = usePermissions() |
|
28
|
const prompt = usePrompt() |
|
29
|
const navigate = useNavigate() |
|
30
|
|
|
31
|
const { mutateAsync: deleteRole, isPending: isDeleting } = useDeleteRbacRole( |
|
32
|
role.id |
|
33
|
) |
|
34
|
|
|
35
|
const handleDelete = async () => { |
|
36
|
const confirmed = await prompt({ |
|
37
|
title: t("roles.delete.title"), |
|
38
|
description: t("roles.delete.description", { name: role.name }), |
|
39
|
confirmText: t("actions.delete"), |
|
40
|
cancelText: t("actions.cancel"), |
|
41
|
}) |
|
42
|
|
|
43
|
if (!confirmed) { |
|
44
|
return |
|
45
|
} |
|
46
|
|
|
47
|
try { |
|
48
|
await deleteRole() |
|
49
|
toast.success(t("roles.delete.successToast", { name: role.name })) |
|
50
|
navigate("/settings/roles", { replace: true }) |
|
51
|
} catch (error) { |
|
52
|
toast.error((error as Error).message) |
|
53
|
} |
|
54
|
} |
|
55
|
|
|
56
|
const users = useMemo(() => { |
|
57
|
return ( |
|
58
|
role.users_link |
|
59
|
?.map((link) => link.user) |
|
60
|
.filter((user): user is HttpTypes.AdminUser => !!user) ?? [] |
|
61
|
) |
|
62
|
}, [role.users_link]) |
|
63
|
|
|
64
|
const userLabels = useMemo(() => { |
|
65
|
return users.map((user) => { |
|
66
|
const fullName = `${user.first_name || ""} ${user.last_name || ""}`.trim() |
|
67
|
return fullName || user.email || user.id |
|
68
|
}) |
|
69
|
}, [users]) |
|
70
|
|
|
71
|
const permissionLabels = useMemo(() => { |
|
72
|
return ( |
|
73
|
role.policies?.map((policy) => { |
|
74
|
if (policy.key) { |
|
75
|
return policy.key |
|
76
|
} |
|
77
|
|
|
78
|
const resource = policy.resource ?? "" |
|
79
|
const operation = policy.operation ?? "" |
|
80
|
|
|
81
|
if (resource && operation) { |
|
82
|
return `${resource}:${operation}` |
|
83
|
} |
|
84
|
|
|
85
|
return policy.id |
|
86
|
}) ?? [] |
|
87
|
) |
|
88
|
}, [role.policies]) |
|
89
|
|
|
90
|
const canUpdate = hasPermission("rbac_role:update") |
|
91
|
const canDelete = hasPermission("rbac_role:delete") |
|
92
|
|
|
93
|
const groups: ActionGroup[] = [] |
|
94
|
|
|
95
|
if (canUpdate) { |
|
96
|
groups.push({ |
|
97
|
actions: [ |
|
98
|
{ |
|
99
|
icon: <PencilSquare />, |
|
100
|
label: t("actions.edit"), |
|
101
|
to: `edit`, |
|
102
|
}, |
|
103
|
{ |
|
104
|
icon: <Key />, |
|
105
|
label: t("roles.actions.managePermissions"), |
|
106
|
to: `permissions`, |
|
107
|
}, |
|
108
|
], |
|
109
|
}) |
|
110
|
} |
|
111
|
|
|
112
|
if (canDelete) { |
|
113
|
groups.push({ |
|
114
|
actions: [ |
|
115
|
{ |
|
116
|
icon: <Trash />, |
|
117
|
label: t("actions.delete"), |
|
118
|
onClick: handleDelete, |
|
119
|
disabled: isDeleting, |
|
120
|
}, |
|
121
|
], |
|
122
|
}) |
|
123
|
} |
|
124
|
|
|
125
|
return ( |
|
126
|
<Container className="divide-y p-0"> |
|
127
|
<div className="flex items-center justify-between px-6 py-4"> |
|
128
|
<Heading>{role.name}</Heading> |
|
129
|
{groups.length && <ActionMenu groups={groups} />} |
|
130
|
</div> |
|
131
|
<SectionRow title={t("fields.description")} value={role.description} /> |
|
132
|
{hasPermission("user:read") && ( |
|
133
|
<SectionRow |
|
134
|
title={t("users.domain")} |
|
135
|
value={ |
|
136
|
userLabels.length ? ( |
|
137
|
<div className="inline-flex"> |
|
138
|
<ListSummary list={userLabels} /> |
|
139
|
</div> |
|
140
|
) : ( |
|
141
|
"-" |
|
142
|
) |
|
143
|
} |
|
144
|
/> |
|
145
|
)} |
|
146
|
{hasPermission("rbac_policy:read") && ( |
|
147
|
<SectionRow |
|
148
|
title={t("roles.fields.permissions")} |
|
149
|
value={ |
|
150
|
permissionLabels.length ? ( |
|
151
|
<div className="inline-flex"> |
|
152
|
<ListSummary list={permissionLabels} /> |
|
153
|
</div> |
|
154
|
) : ( |
|
155
|
"-" |
|
156
|
) |
|
157
|
} |
|
158
|
/> |
|
159
|
)} |
|
160
|
</Container> |
|
161
|
) |
|
162
|
} |