import React, { useMemo, useState } from 'react'; import type { ListPermissionGroupsResponse, ListPermissionUsersResponse } from '@/api/asset_interfaces'; import { useDatasetUpdatePermissionUsers } from '@/api/buster_rest/datasets'; import { ListUserItem } from '@/components/features/list'; import { type BusterListColumn, type BusterListRowItem, EmptyStateList, InfiniteListContainer } from '@/components/ui/list'; import { BusterInfiniteList } from '@/components/ui/list/BusterInfiniteList'; import { Select } from '@/components/ui/select'; import { useMemoizedFn } from '@/hooks'; import { BusterRoutes, createBusterRoute } from '@/routes'; import { PERMISSION_USERS_OPTIONS } from './config'; import { PermissionUsersSelectedPopup } from './PermissionUsersSelectedPopup'; export const PermissionListUsersContainer: React.FC<{ filteredPermissionUsers: ListPermissionUsersResponse[]; datasetId: string; }> = React.memo(({ filteredPermissionUsers, datasetId }) => { const { mutateAsync: updatePermissionUsers } = useDatasetUpdatePermissionUsers(datasetId); const [selectedRowKeys, setSelectedRowKeys] = useState([]); 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) => { return ; } }, { title: 'Assigned', dataIndex: 'assigned', render: (assigned: boolean, permissionGroup: ListPermissionGroupsResponse) => { return (
); } } ], [] ); 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.SETTINGS_USERS_ID, userId: permissionUser.id }) }; if (permissionUser.assigned) { acc.canQueryPermissionUsers.push(user); } else { acc.cannotQueryPermissionUsers.push(user); } 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 ], [canQueryPermissionUsers, cannotQueryPermissionUsers, numberOfPermissionUsers] ); return ( }> ( ), [] )} /> ); }); PermissionListUsersContainer.displayName = 'PermissionListUsersContainer'; const PermissionGroupAssignedCell: React.FC<{ id: string; assigned: boolean; onSelect: (value: { id: string; assigned: boolean }) => void; }> = ({ id, assigned, onSelect }) => { return (
e.stopPropagation()} onKeyUp={(e) => e.stopPropagation()}>