1 import { c } from 'ttag';
3 import Field from '@proton/components/components/container/Field';
4 import Row from '@proton/components/components/container/Row';
5 import Checkbox from '@proton/components/components/input/Checkbox';
6 import Option from '@proton/components/components/option/Option';
7 import SelectTwo from '@proton/components/components/selectTwo/SelectTwo';
8 import type { SelectChangeEvent } from '@proton/components/components/selectTwo/select';
9 import { SortingTableHeader } from '@proton/components/components/table/SortingTableHeader';
10 import Table from '@proton/components/components/table/Table';
11 import TableBody from '@proton/components/components/table/TableBody';
12 import TableRow from '@proton/components/components/table/TableRow';
13 import useSortedList from '@proton/components/hooks/useSortedList';
14 import { SERVER_FEATURES, SORT_DIRECTION, USER_ROLES } from '@proton/shared/lib/constants';
15 import noop from '@proton/utils/noop';
17 import type { GatewayDto } from './GatewayDto';
18 import type { GatewayUser } from './GatewayUser';
20 type PartialGateway = Pick<GatewayDto, 'features' | 'userIds'>;
24 users: readonly GatewayUser[];
25 model: PartialGateway;
26 changeModel: (model: Partial<PartialGateway>) => void;
29 export const GatewayUserSelection = ({ loading, users, model, changeModel }: Props) => {
30 const { sortConfig, sortedList, toggleSort } = useSortedList(users as GatewayUser[], {
32 direction: SORT_DIRECTION.DESC,
35 const handleFeatureChange = ({ value }: SelectChangeEvent<string>) => changeModel({ features: Number(value) });
37 const getSelectToggle = (id: string) =>
43 model.userIds.indexOf(id) !== -1
44 ? model.userIds.filter((selected) => selected !== id)
45 : [...model.userIds, id],
48 const selectAllToggle = loading
52 userIds: users.every(({ ID }) => model.userIds.indexOf(ID) !== -1) ? [] : users.map(({ ID }) => ID),
60 value={`${model.features & SERVER_FEATURES.DOUBLE_RESTRICTION}`}
61 onChange={handleFeatureChange}
63 <Option value="0" title={c('Title').t`Every member of the organization can access`}>
64 {c('Option').t`The whole organization`}
67 value={`${SERVER_FEATURES.DOUBLE_RESTRICTION}`}
68 title={c('Title').t`Custom selection of users`}
70 {c('Option').t`Select who can access...`}
75 {model.features & SERVER_FEATURES.DOUBLE_RESTRICTION ? (
79 onToggleSort={toggleSort as any}
85 checked={users.every(({ ID }) => model.userIds.indexOf(ID) !== -1)}
86 onChange={selectAllToggle}
89 className: 'w-custom',
90 style: { '--w-custom': '5%' },
92 { key: 'Name', content: c('TableHeader').t`Name`, sorting: true },
93 { key: 'Email', content: c('TableHeader').t`Email`, sorting: true },
96 content: c('TableHeader').t`Role`,
102 <TableBody loading={!users} colSpan={4}>
103 {sortedList.map(({ ID, Name, Email, Role, Subscriber }: GatewayUser) => (
105 key={`select-user--${ID}`}
109 checked={model.userIds.indexOf(ID) !== -1}
110 onChange={getSelectToggle(ID)}
112 <div className="text-ellipsis">{Name}</div>,
113 <div className="text-ellipsis">{Email}</div>,
114 Role === USER_ROLES.ADMIN_ROLE
116 ? /* Current status of a user being admin of their organization */ c('Role')
118 : /* Current status of a user being admin of their organization */ c('Role')
120 : /* Current status of a user being simple member in their organization */ c(
133 export default GatewayUserSelection;