buster/web/src/controllers/DatasetPermissionUser/PermissionListUsersContaine...

166 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-02-02 12:53:01 +08:00
import type {
2025-01-11 07:10:50 +08:00
ListPermissionGroupsResponse,
2025-02-02 12:53:01 +08:00
ListPermissionUsersResponse
} from '@/api/asset_interfaces';
import { useDatasetUpdatePermissionUsers } from '@/api/buster_rest/datasets';
import {
BusterListColumn,
BusterListRowItem,
EmptyStateList,
InfiniteListContainer
2025-02-20 10:53:49 +08:00
} from '@/components/ui/list';
import { BusterInfiniteList } from '@/components/ui/list/BusterInfiniteList';
2025-01-11 07:10:50 +08:00
import { useMemoizedFn } from 'ahooks';
import { Select } from 'antd';
import React, { useMemo, useState } from 'react';
import { PermissionUsersSelectedPopup } from './PermissionUsersSelectedPopup';
import { PERMISSION_USERS_OPTIONS } from './config';
import { BusterRoutes, createBusterRoute } from '@/routes';
2025-03-01 03:05:32 +08:00
import { ListUserItem } from '@/components/features/list';
2025-01-11 07:10:50 +08:00
export const PermissionListUsersContainer: React.FC<{
filteredPermissionUsers: ListPermissionUsersResponse[];
datasetId: string;
}> = React.memo(({ filteredPermissionUsers, datasetId }) => {
2025-01-15 01:30:13 +08:00
const { mutateAsync: updatePermissionUsers } = useDatasetUpdatePermissionUsers(datasetId);
2025-01-11 07:10:50 +08:00
const [selectedRowKeys, setSelectedRowKeys] = useState<string[]>([]);
const numberOfPermissionUsers = filteredPermissionUsers.length;
const onSelectAssigned = useMemoizedFn(async (params: { id: string; assigned: boolean }) => {
updatePermissionUsers([params]);
});
const columns: BusterListColumn[] = useMemo(
() => [
{
title: 'Name',
dataIndex: 'name',
width: 270,
render: (name: string, user: ListPermissionUsersResponse) => {
2025-01-21 06:32:59 +08:00
return <ListUserItem name={name} email={user.email} />;
2025-01-11 07:10:50 +08:00
}
},
{
title: 'Assigned',
dataIndex: 'assigned',
render: (assigned: boolean, permissionGroup: ListPermissionGroupsResponse) => {
return (
<div className="flex justify-end">
<PermissionGroupAssignedCell
id={permissionGroup.id}
assigned={assigned}
onSelect={onSelectAssigned}
/>
</div>
);
}
}
],
[]
);
const { cannotQueryPermissionUsers, canQueryPermissionUsers } = useMemo(() => {
const result: {
cannotQueryPermissionUsers: BusterListRowItem[];
canQueryPermissionUsers: BusterListRowItem[];
} = filteredPermissionUsers.reduce<{
cannotQueryPermissionUsers: BusterListRowItem[];
canQueryPermissionUsers: BusterListRowItem[];
}>(
(acc, permissionUser) => {
const user: BusterListRowItem = {
id: permissionUser.id,
data: permissionUser,
link: createBusterRoute({
route: BusterRoutes.APP_SETTINGS_USERS_ID,
userId: permissionUser.id
})
};
2025-01-11 07:10:50 +08:00
if (permissionUser.assigned) {
acc.canQueryPermissionUsers.push(user);
2025-01-11 07:10:50 +08:00
} else {
acc.cannotQueryPermissionUsers.push(user);
2025-01-11 07:10:50 +08:00
}
return acc;
},
{
cannotQueryPermissionUsers: [] as BusterListRowItem[],
canQueryPermissionUsers: [] as BusterListRowItem[]
}
);
return result;
}, [filteredPermissionUsers]);
const rows = useMemo(
() => [
{
id: 'header-assigned',
data: {},
hidden: canQueryPermissionUsers.length === 0,
rowSection: {
title: 'Assigned',
secondaryTitle: canQueryPermissionUsers.length.toString()
}
},
...canQueryPermissionUsers,
{
id: 'header-not-assigned',
data: {},
hidden: cannotQueryPermissionUsers.length === 0,
rowSection: {
title: 'Not Assigned',
secondaryTitle: cannotQueryPermissionUsers.length.toString()
}
},
...cannotQueryPermissionUsers
],
2025-01-11 07:10:50 +08:00
[canQueryPermissionUsers, cannotQueryPermissionUsers, numberOfPermissionUsers]
);
return (
<InfiniteListContainer
popupNode={
<PermissionUsersSelectedPopup
datasetId={datasetId}
selectedRowKeys={selectedRowKeys}
onSelectChange={setSelectedRowKeys}
/>
}>
2025-01-11 07:10:50 +08:00
<BusterInfiniteList
columns={columns}
rows={rows}
showHeader={false}
showSelectAll={false}
selectedRowKeys={selectedRowKeys}
onSelectChange={setSelectedRowKeys}
useRowClickSelectChange={false}
emptyState={<EmptyStateList text="No users found" />}
2025-01-11 07:10:50 +08:00
/>
</InfiniteListContainer>
2025-01-11 07:10:50 +08:00
);
});
PermissionListUsersContainer.displayName = 'PermissionListUsersContainer';
const PermissionGroupAssignedCell: React.FC<{
id: string;
assigned: boolean;
onSelect: (value: { id: string; assigned: boolean }) => void;
}> = ({ id, assigned, onSelect }) => {
return (
<Select
options={PERMISSION_USERS_OPTIONS}
value={assigned}
popupMatchSelectWidth
onSelect={(value) => {
onSelect({ id, assigned: value });
}}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
}}
/>
);
};