import { useState, useRef, useEffect, forwardRef, useImperativeHandle, type ReactNode } from 'react'; import { Row, Col, App, Space, Flex, Tooltip, Dropdown, } from 'antd'; import { useTranslation } from 'react-i18next'; import type { ToolItem, CustomToolModalRef, CustomRef } from './types'; import CustomToolModal from './components/CustomToolModal'; import BodyWrapper from '@/components/Empty/BodyWrapper' import RbCard from '@/components/RbCard' import { getTools, deleteTool } from '@/api/tools' import { formatDateTime } from '@/utils/format' const Custom = forwardRef ReactNode; keyword?: string | undefined }>(({ getStatusTag, keyword }, ref) => { const { t } = useTranslation(); const { message, modal } = App.useApp() const [loading, setLoading] = useState(false); const [data, setData] = useState([]); const customToolModalRef = useRef(null); useEffect(() => { getData() }, [keyword]) const getData = () => { setLoading(true) getTools({ tool_type: 'custom', name: keyword }) .then((res) => { setData(res as ToolItem[]) }) .finally(() => { setLoading(false) }) } // 打开添加服务弹窗 const handleEdit = (data?: ToolItem) => { customToolModalRef.current?.handleOpen(data); }; useImperativeHandle(ref, () => ({ handleEdit })); // 删除服务 const handleDeleteService = (item: ToolItem) => { modal.confirm({ title: t('common.confirmDeleteDesc', { name: item.name }), okText: t('common.delete'), cancelText: t('common.cancel'), okType: 'danger', onOk: () => { deleteTool(item.id).then(() => { message.success(t('common.deleteSuccess')); getData() }) } }) }; return ( <> {data.map((item) => (
{item.name}
{getStatusTag(item.status)}
, label: t('common.edit'), onClick: () => handleEdit(item), }, { key: 'delete', className: 'rb:text-[#FF5D34]!', icon:
, label: t('common.delete'), onClick: () => handleDeleteService(item), }, ] }} placement="bottomRight" >
} isNeedTooltip={false} > {item.tags?.length > 0 ? {item.tags?.slice(0, 2).map((type, i) => (
{type}
))}
{item.tags.length > 2 && ( {item.tags?.slice(2, item.tags.length).map((type, i) => (
{type}
))}
} color="white" placement="bottom" >
+{item.tags.length - 2}
)} :
{t('tool.noTags')}
}
{t('tool.auth_type')}
{(item.config_data as any)?.auth_type}
{t('tool.created_at')}
{formatDateTime(item.created_at)}
))} {/* 添加服务弹窗组件 */} ); }); export default Custom;