import { Card, Label, Switch, Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from 'ui' import { ALL_PRIVILEGE_TYPES, COLUMN_PRIVILEGE_TYPES, TABLE_PRIVILEGE_TYPES, } from './Privileges.constants' import { usePrivilegesState } from './Privileges.utils' import type { ColumnPrivilege } from '@/data/privileges/column-privileges-query' export interface PrivilegesTableProps extends Pick< ReturnType, 'tableCheckedStates' | 'columnCheckedStates' | 'toggleTablePrivilege' | 'toggleColumnPrivilege' > { columnPrivileges: ColumnPrivilege[] disabled: boolean isApplyingChanges?: boolean } export const PrivilegesTable = ({ columnPrivileges, tableCheckedStates, columnCheckedStates, toggleTablePrivilege, toggleColumnPrivilege, disabled, isApplyingChanges = false, }: PrivilegesTableProps) => { const handleClickColumnName = (columnId: string) => { const hasAllPrivileges = COLUMN_PRIVILEGE_TYPES.every( (privilege) => columnCheckedStates[columnId][privilege] ) const privilegesToToggle = COLUMN_PRIVILEGE_TYPES.filter((privilege) => hasAllPrivileges ? columnCheckedStates[columnId][privilege] : !columnCheckedStates[columnId][privilege] ) privilegesToToggle.forEach((privilege) => { toggleColumnPrivilege(columnId, privilege) }) } return ( Column {ALL_PRIVILEGE_TYPES.map((privilege) => { const checked = tableCheckedStates[privilege] const switchThId = `privilege-th-${privilege}` return (
{ toggleTablePrivilege(privilege) }} disabled={disabled || isApplyingChanges} />
) })}
{columnPrivileges.map((column) => ( {COLUMN_PRIVILEGE_TYPES.map((privilege) => { const checked = columnCheckedStates[column.column_id][privilege] return ( {COLUMN_PRIVILEGE_TYPES.includes(privilege) && (
{ toggleColumnPrivilege(column.column_id, privilege) }} disabled={disabled || isApplyingChanges} />
)}
) })} {TABLE_PRIVILEGE_TYPES.map((privilege) => { return ( N/A ) })}
))}
) }