import { DownOutlined } from '@ant-design/icons';
import { Descriptions, Spin, Tooltip, Tree, Typography } from 'antd';
import _, { isUndefined } from 'lodash';
import type { ReactNode } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import { FormattedMessage, useIntl, useRequest } from 'umi';
import { formatUserRender } from '../utils';
import publicConst from '../utils/const';
import './index.less';
const { Text } = Typography;
const formartTreeData = (data: any[], showApply: any) => {
let result: any;
let child: any;
data?.forEach((item, i) => {
if (!item) return;
const { id, name, isAllowApply } = item;
if (!result) {
result = {
title:
showApply || isAllowApply ? (
{name}
) : (
),
key: id,
children: [],
};
child = result;
} else {
const children = {
title:
showApply || isAllowApply ? (
{name}
) : (
),
key: id,
children: [],
};
child.children = [children];
child = children;
}
});
return result ? [result] : [];
};
export type PermissionParentDataType = {
name?: string;
uniqueKey?: string;
id?: number;
nameZh?: string;
nameEn?: string;
ownerName?: string;
ownerId?: number;
ownerAccount?: string;
ownerState?: number;
userState?: number;
remark?: string;
dataSensitive?: number;
sensitiveLevel?: number;
isAllowApply?: any;
};
export type PermissionParentShowProps = {
type?: 'feature' | 'option';
id?: number;
name?: string | ReactNode;
appId?: number;
uniqueKey?: string;
showApply?: boolean;
children: React.ReactNode;
data?: PermissionParentDataType;
};
const PermissionParentShow = (props: PermissionParentShowProps) => {
const { type, id, name, showApply = false, appId, uniqueKey, children, data: itemData } = props;
const [open, setOpen] = useState(false);
const { formatMessage } = useIntl();
const { DataSensitive, DataSensitiveLevel } = publicConst(formatMessage);
const { loading, data, run } = useRequest(
(pId, pAppId, pUniqueKey) => ({
url: `/goapi/${type}/path`,
method: 'POST',
data: { id: pId, appId: pAppId, uniqueKey: pUniqueKey },
}),
{
manual: true,
},
);
const detailRender = (node: ReactNode, item?: PermissionParentDataType) => (
{Boolean(id) && (
}>
{node}
)}
}>
{formatUserRender({
name: item?.ownerName || item?.nameZh,
id: item?.ownerId,
account: item?.ownerAccount || item?.nameEn,
state: !_.isUndefined(item?.ownerState) ? item?.ownerState : item?.userState,
})}
{item?.uniqueKey && (
}
>
{item?.uniqueKey}
)}
{!isUndefined(item?.sensitiveLevel) && (
}
>
{DataSensitiveLevel.find((v) => v.value === item?.dataSensitive)?.label}
)}
{!isUndefined(item?.dataSensitive) && (
}
>
{DataSensitive.find((v) => v.value === item?.sensitiveLevel)?.label}
)}
}>
{item?.remark}
);
const treeData = useMemo(() => _.reverse(data || []), [data]);
useEffect(() => {
if (open && id) {
run(id, appId, uniqueKey);
}
}, [id, appId, uniqueKey, run, open]);
return (
setOpen(val)}
title={detailRender(
loading ? (
) : (
}
disabled
blockNode
selectable={false}
treeData={formartTreeData(
[
...treeData,
{
id,
name: name || uniqueKey || itemData?.name || itemData?.uniqueKey,
isAllowApply:
itemData?.isAllowApply === '' || _.isUndefined(itemData?.isAllowApply)
? 1
: itemData?.isAllowApply,
},
],
showApply,
)}
expandedKeys={data?.map((item: any) => item.id)}
/>
),
itemData,
)}
>
{children}
);
};
export default PermissionParentShow;