import { apiInterceptors, getResource } from '@/client/api'; import { useRequest } from 'ahooks'; import { Form, Select, Switch } from 'antd'; import cls from 'classnames'; import React, { useEffect, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; const ResourceContent: React.FC<{ uid: string; initValue: any; updateData: (data: any) => void; classNames?: string; resourceTypeOptions: Record[]; setCurIcon: React.Dispatch< React.SetStateAction<{ uid: string; icon: string; }> >; }> = ({ uid, initValue, updateData, classNames, resourceTypeOptions, setCurIcon }) => { const [form] = Form.useForm(); const type = Form.useWatch('type', form); const isDynamic = Form.useWatch('is_dynamic', form); const value = Form.useWatch('value', form); const { t } = useTranslation(); // 资源类型选项 const options = useMemo(() => { return resourceTypeOptions?.filter(item => item.value !== 'all') || []; }, [resourceTypeOptions]); // 获取非动态情况下,知识库、数据库、插件、编排工作流参数列表 const { run, data, loading } = useRequest( async type => { const [, res] = await apiInterceptors(getResource({ type })); form.setFieldsValue({ value: initValue?.value || res?.[0]?.key, }); return res || []; }, { manual: true, }, ); useEffect(() => { if (type) { run(type); } }, [run, type]); // 动态参数value选项 const dynamicOptions = useMemo(() => { return ( data?.map(item => { return { ...item, label: item.label, value: item.key + '', }; }) || [] ); }, [data]); // 参数根据选择的资源类型动态变化 const renderParameter = () => { if (type === 'image_file') { return null; // return ( // // // // // // ); } if (type === 'internet') { return null; // return ( // // // // ); } if (['text_file', 'excel_file'].includes(type)) { return null; // return ( // // // // // // ); } return ( { setCurIcon({ uid, icon: val }); }} /> {/* 如果选择了动态参数这里就不需要参数了 */} {!isDynamic && <> {renderParameter()}} ); }; export default ResourceContent;