import type { ProColumns } from '@ant-design/pro-table'; import type { TableProps } from 'antd'; import { Button, Popconfirm, Space, Tooltip, Typography } from 'antd'; import _ from 'lodash'; import type { Dispatch, Key, SetStateAction } from 'react'; import { useCallback } from 'react'; import { FormattedMessage, useIntl } from 'umi'; import AuditCountersign from '../AuditCountersign'; import AuditPassOn from '../AuditPassOn'; import AuditProgress from '../AuditProgress'; import BindAllPermission from '../BindAllPermission'; import CustomTable from '../CustomTable'; import ExpandIcon from '../ExpandIcon'; import Tips from '../Tips'; import { treeLeafNode } from '../utils/array'; import publicConst from '../utils/const'; import { useAllCity } from '../utils/hooks'; import AgentPermission from './AgentPermission'; import { AduitStatus } from './const'; import type { RowType } from './hooks'; import lessStyle from './index.less'; const { DataSensitiveLevelTips, DataSensitiveTips } = Tips; const { Text } = Typography; export interface UacTableProps { auditDetail: any; rowSelectionKeys?: RowType; chainId: string; pageReadOnly?: boolean; setSelectionKeys?: Dispatch>; approvalHandler?: (id: Key[], state: number, type: string) => Promise; requestDetail?: () => Promise; } const UacTable = (props: UacTableProps) => { const { auditDetail, rowSelectionKeys, chainId, pageReadOnly, setSelectionKeys, approvalHandler, requestDetail, } = props; const { auditList, appInfo } = auditDetail || {}; const { formatMessage } = useIntl(); const isAgent = chainId?.startsWith('ff_'); const columns = useCallback( (type: string): ProColumns[] => { const { DataSensitiveMap, DataSensitiveLevelMap } = publicConst(formatMessage); const cols: ProColumns[] = [ { title: ( ), dataIndex: 'name', search: false, hideInTable: false, className: lessStyle.nameColumn, render: (__, record: any) => { const { id, name } = record; return ( <> {name} {type === 'role' && ( )} ); }, }, { title: , dataIndex: 'dataSensitive', search: false, tooltip: DataSensitiveLevelTips, valueType: 'select', valueEnum: DataSensitiveLevelMap, }, { title: , dataIndex: 'sensitiveLevel', search: false, tooltip: DataSensitiveTips, valueType: 'select', valueEnum: DataSensitiveMap, }, { title: , dataIndex: 'expiredAt', search: false, ellipsis: true, render: (__: any, record: any) => { if (type === 'city' && ['country', 'region'].includes(record.levelType)) return '-'; const expiredAt = type === 'city' ? 0 : record.expiredAt; return ( _.isNumber(expiredAt) && ( {expiredAt === 0 ? formatMessage({ id: 'pages.setting.base.defaultExpirationTime.forever' }) : expiredAt / 86400} ) ); }, }, { title: , dataIndex: 'uacAuditState', search: false, render: (__: any, record: any) => { if (type === 'city' && ['country', 'region'].includes(record.levelType)) return '-'; const { uacAuditState } = record; return ( _.isNumber(uacAuditState) && uacAuditState !== 0 && ( {formatMessage({ id: AduitStatus[uacAuditState] })} ) ); }, }, { title: , dataIndex: 'uacAuditId', search: false, render: (__: any, record: any) => { if (type === 'city' && ['country', 'region'].includes(record.levelType)) return '-'; const { uacAuditId } = record; return ( Boolean(uacAuditId) && ( ) ); }, }, { title: , dataIndex: 'permissionOption', valueType: 'option', fixed: 'right', align: 'center', width: 240, search: false, hideInTable: pageReadOnly, render: (__: any, record: any) => { const { uacAuditId, uacCanAudit } = record; return ( Boolean(uacAuditId && uacCanAudit) && ( { approvalHandler?.([uacAuditId], 1, 'uac'); }} > { approvalHandler?.([uacAuditId], 3, 'uac'); }} > ) ); }, }, ]; if (isAgent) { return [ { title: , dataIndex: 'ffFeature', search: false, tooltip: { title: , }, hideInTable: false, render: (__, record) => ( ), }, ...cols, ]; } return cols; }, [ formatMessage, pageReadOnly, isAgent, appInfo?.id, chainId, requestDetail, auditDetail, approvalHandler, ], ); const rowSelection = useCallback( (type: string): TableProps['rowSelection'] | false => { if (pageReadOnly) return false; return { checkStrictly: type !== 'city', getCheckboxProps: (record: any) => { if (type === 'city' && ['country', 'region'].includes(record.levelType)) { const check = (l: any[]) => treeLeafNode(l).some((o) => { const { uacAuditId, uacCanAudit } = o; return Boolean(uacAuditId && uacCanAudit); }); return { disabled: !check([record]) }; } return { disabled: !record.uacCanAudit, }; }, selectedRowKeys: rowSelectionKeys?.[type], onChange: (rowKey: Key[]) => setSelectionKeys?.((oldVal: RowType) => ({ ...oldVal, [type]: rowKey, })), }; }, [pageReadOnly, rowSelectionKeys, setSelectionKeys], ); const { build: buildLeafNode } = useAllCity(undefined, { numberId: true }); const render = (l: any[]) => { if (isAgent) { const treeSet = (tree: any[], o: Record) => tree?.map((v) => ({ ...v, ...o, children: treeSet(v.children, o), })); const options = l.reduce((pre, cur) => { const { option, ffFeature } = cur; const newOpts = option?.map((v: any) => ({ ...v, children: treeSet(v?.optionTree, { ffFeature }), ffFeature, })) ?? []; pre.push(...newOpts); return pre; }, []); return ( [record.uacAuditId ?? record.uniqueKey, record.ffFeature?.id].join('-') } rowSelection={rowSelection('option')} columns={columns('option')} dataSource={options} /> ); } return l.map((item) => { const { feature, option, policy, role, city } = item; const reactKey = _.uniqueId(); const map = { feature, policy, role, city, option: option?.map((v: any) => ({ ...v, children: v?.optionTree, })), }; return Object.keys(map) .filter((key) => !!map[key]?.length) .map((key) => ( { if (key === 'city' && ['country', 'region'].includes(record.levelType)) { return [record.levelType, record.id].join('-'); } return record.uacAuditId ?? record.uniqueKey; }} rowSelection={rowSelection(key)} columns={columns(key)} dataSource={key === 'city' ? buildLeafNode(map[key]) : map[key]} /> )); }); }; return <>{Array.isArray(auditList) && render(auditList)}; }; export default UacTable;