-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathUsersTable.tsx
More file actions
170 lines (145 loc) · 5.67 KB
/
Copy pathUsersTable.tsx
File metadata and controls
170 lines (145 loc) · 5.67 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2026 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { useCallback, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import { reaction } from 'mobx';
import { FieldCheckbox, Link, Placeholder, s, useS, useTranslate } from '@cloudbeaver/core-blocks';
import { useService } from '@cloudbeaver/core-di';
import type { AdminUserInfoFragment } from '@cloudbeaver/core-sdk';
import { UsersResource } from '@cloudbeaver/core-authentication';
import { NotificationService } from '@cloudbeaver/core-events';
import { ADMINISTRATION_TABLE_DEFAULT_ROW_HEIGHT, AdministrationTableStyles } from '@cloudbeaver/core-administration';
import { DataGrid, useCreateGridReactiveValue } from '@cloudbeaver/plugin-data-grid';
import { UsersTableOptionsPanelService } from './UsersTableOptionsPanelService.js';
import { UsersAdministrationService } from '../UsersAdministrationService.js';
import { Command } from '@dbeaver/ui-kit';
interface Props {
users: AdminUserInfoFragment[];
isManageable: boolean;
displayAuthRole: boolean;
onLoadMore?: () => void;
}
const ID_COLUMN = { key: 'id', label: 'authentication_user_name' };
const ROLE_COLUMN = { key: 'role', label: 'authentication_user_role' };
const TEAM_COLUMN = { key: 'team', label: 'authentication_user_team' };
const ENABLED_COLUMN = { key: 'enabled', label: 'authentication_user_enabled' };
const AUTH_COLUMN = { key: 'auth', label: 'authentication_administration_user_auth_methods' };
const COLUMNS = [ID_COLUMN, TEAM_COLUMN, ENABLED_COLUMN, AUTH_COLUMN];
export const UsersTable = observer<Props>(function UsersTable({ users, isManageable, displayAuthRole, onLoadMore }) {
const translate = useTranslate();
const styles = useS(AdministrationTableStyles);
const notificationService = useService(NotificationService);
const usersTableOptionsPanelService = useService(UsersTableOptionsPanelService);
const usersAdministrationService = useService(UsersAdministrationService);
const usersResource = useService(UsersResource);
const columns = useMemo(() => {
if (displayAuthRole) {
const result = [...COLUMNS];
const teamIndex = COLUMNS.findIndex(column => column.key === TEAM_COLUMN.key);
result.splice(teamIndex + 1, 0, ROLE_COLUMN);
return result;
}
return COLUMNS;
}, [displayAuthRole]);
const enableUser = useCallback(
async (userId: AdminUserInfoFragment['userId'], enabled: boolean) => {
try {
await usersResource.enableUser(userId, enabled);
} catch (error: any) {
notificationService.logException(error);
}
},
[usersResource, notificationService],
);
const columnsCount = useCreateGridReactiveValue(() => columns.length, null, [columns]);
const rowsCount = useCreateGridReactiveValue(
() => users.length,
onValueChange => reaction(() => users.length, onValueChange),
[users],
);
function getCell(rowIdx: number, colIdx: number) {
const row = users[rowIdx];
const column = columns[colIdx];
if (!row || !column) {
return null;
}
if (column.key === ID_COLUMN.key) {
return (
<Command
render={<div />}
tabIndex={0}
title={row.userId}
className="tw:flex tw:cursor-pointer tw:items-center tw:gap-2 tw:outline-none"
onClick={() => usersTableOptionsPanelService.open(row.userId)}
>
<Link truncate>{row.userId}</Link>
</Command>
);
}
if (column.key === ROLE_COLUMN.key) {
const role = row.authRole ?? '';
return <span title={role}>{role}</span>;
}
if (column.key === TEAM_COLUMN.key) {
const teams = row.grantedTeams.join(', ');
return <span title={teams}>{teams}</span>;
}
if (column.key === ENABLED_COLUMN.key) {
const isActive = usersResource.isActiveUser(row.userId);
const title = isActive ? translate('administration_teams_team_granted_users_permission_denied') : undefined;
return (
<div className="tw:flex tw:items-center tw:justify-center">
<FieldCheckbox
title={title}
checked={row.enabled}
disabled={isActive || !isManageable}
onChange={() => enableUser(row.userId, !row.enabled)}
/>
</div>
);
}
if (column.key === AUTH_COLUMN.key) {
return (
<div className="tw:flex tw:gap-2 tw:items-center">
<Placeholder container={usersAdministrationService.userDetailsInfoPlaceholder} user={row} />
</div>
);
}
return null;
}
const cell = useCreateGridReactiveValue(getCell, (onValueChange, rowIdx, colIdx) => reaction(() => getCell(rowIdx, colIdx), onValueChange), [
columns,
users,
usersResource,
usersTableOptionsPanelService,
usersAdministrationService,
enableUser,
]);
function getHeaderText(colIdx: number) {
return translate(columns[colIdx]?.label) ?? '';
}
const headerText = useCreateGridReactiveValue(getHeaderText, (onValueChange, colIdx) => reaction(() => getHeaderText(colIdx), onValueChange), [
columns,
translate,
]);
return (
<div className="tw:overflow-auto tw:h-full tw:max-w-full theme-text-on-surface">
<DataGrid
columnCount={columnsCount}
rowCount={rowsCount}
getHeaderResizable={colIdx => colIdx > 0}
getRowHeight={() => ADMINISTRATION_TABLE_DEFAULT_ROW_HEIGHT}
getHeaderPinned={colIdx => colIdx <= 0}
headerText={headerText}
cell={cell}
className={s(styles, { table: true })}
onScrollToBottom={onLoadMore}
/>
</div>
);
});