-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTableCells.tsx
More file actions
148 lines (134 loc) · 4.51 KB
/
TableCells.tsx
File metadata and controls
148 lines (134 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { ContextType, useContext, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Icon, IconButton } from '@openedx/paragon';
import { AppContext } from '@edx/frontend-platform/react';
import {
RemoveRedEye,
Delete, ExpandMore,
} from '@openedx/paragon/icons';
import { UserRole } from '@src/types';
import type { TableCellValue } from '@src/paragon';
import { DJANGO_MANAGED_ROLES, MAP_ROLE_KEY_TO_LABEL } from '@src/authz-module/constants';
import { RESOURCE_ICONS } from './constants';
import messages from './messages';
import ViewMoreLink from './ViewMoreLink';
type CellProps = TableCellValue<UserRole>;
type CellPropsWithValue = CellProps & {
value: string;
};
type ExtendedCellProps = CellPropsWithValue & {
cell: {
getCellProps: (props?: Record<string, string>) => Record<string, string>;
};
};
const NameCell = ({ row }: CellProps) => {
const intl = useIntl();
const { authenticatedUser } = useContext(AppContext) as ContextType<typeof AppContext>;
const username = authenticatedUser?.username;
if (row.original.username === username) {
return (
<span>
{row.original.fullName || row.original.username}
<span className="text-gray-500">{intl.formatMessage(messages['authz.table.username.current'])}</span>
</span>
);
}
return <span>{row.original.fullName || row.original.username}</span>;
};
const ViewActionCell = ({ row }: CellProps) => {
const { formatMessage } = useIntl();
const navigate = useNavigate();
const viewPath = `/authz/user/${row.original.username}`;
return (
<IconButton
src={RemoveRedEye}
alt={formatMessage(messages['authz.table.column.actions.view.title'])}
size="sm"
onClick={() => navigate(viewPath)}
/>
);
};
const OrgCell = ({ value, row }: CellPropsWithValue) => {
const { formatMessage } = useIntl();
return (
<span>
{DJANGO_MANAGED_ROLES.includes(row.original.role) ? formatMessage(messages['authz.user.table.org.all.organizations.label']) : value}
</span>
);
};
const ScopeCell = ({ row }: CellProps) => {
const { formatMessage } = useIntl();
const { scopeText, iconSrc } = useMemo(() => {
if (DJANGO_MANAGED_ROLES.includes(row.original.role)) {
return {
scopeText: formatMessage(messages['authz.user.table.scope.global.label']),
iconSrc: RESOURCE_ICONS.GLOBAL,
};
}
const scopeIcon = row.original.role?.startsWith('lib') ? RESOURCE_ICONS.LIBRARY : RESOURCE_ICONS.COURSE;
return {
scopeText: row.original.scope,
iconSrc: scopeIcon,
};
}, [row.original.role, row.original.scope, formatMessage]);
return (
<span className="d-flex align-items-center">
{iconSrc && <Icon color="primary" src={iconSrc} className="mr-2" size="xs" />}
{scopeText}
</span>
);
};
const RoleCell = ({ value, cell }: ExtendedCellProps) => (
<span {...cell.getCellProps({ 'data-role': MAP_ROLE_KEY_TO_LABEL[value] || '' })}>
{MAP_ROLE_KEY_TO_LABEL[value] || ''}
</span>
);
const PermissionsCell = ({ row }: CellProps) => {
const { formatMessage } = useIntl();
const { role, permissionCount: count } = row.original;
const isDjangoRole = DJANGO_MANAGED_ROLES.includes(role);
return (
<span>
{ isDjangoRole
? formatMessage(
messages['authz.user.table.permissions.access.label'],
{ accessType: role === 'django.superuser' ? 'total' : 'partial' },
)
: formatMessage(messages['authz.user.table.permissions.available.count'], { count })}
</span>
);
};
const ActionsCell = ({ row }: CellProps) => {
const { formatMessage } = useIntl();
const handleDelete = () => {
// TODO: Implement delete functionality
// eslint-disable-next-line no-console
console.log('Delete clicked for row:', row);
};
return (
<IconButton variant="danger" onClick={handleDelete} alt={formatMessage(messages['authz.user.table.delete.action.alt'])} src={Delete} />
);
};
const ViewAllPermissionsCell = ({ row }: CellProps) => {
const { formatMessage } = useIntl();
return (
<ViewMoreLink
label={formatMessage(messages['authz.user.table.view_all_permissions.link.text'])}
// TODO: Implement view more functionality
// eslint-disable-next-line no-console
onClick={() => console.log('View more clicked for row:', row)}
iconSrc={ExpandMore}
/>
);
};
export {
NameCell,
ViewActionCell,
RoleCell,
OrgCell,
ScopeCell,
PermissionsCell,
ActionsCell,
ViewAllPermissionsCell,
};