ResourceContent.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { apiInterceptors, getResource } from '@/client/api';
  2. import { useRequest } from 'ahooks';
  3. import { Form, Select, Switch } from 'antd';
  4. import cls from 'classnames';
  5. import React, { useEffect, useMemo } from 'react';
  6. import { useTranslation } from 'react-i18next';
  7. const ResourceContent: React.FC<{
  8. uid: string;
  9. initValue: any;
  10. updateData: (data: any) => void;
  11. classNames?: string;
  12. resourceTypeOptions: Record<string, any>[];
  13. setCurIcon: React.Dispatch<
  14. React.SetStateAction<{
  15. uid: string;
  16. icon: string;
  17. }>
  18. >;
  19. }> = ({ uid, initValue, updateData, classNames, resourceTypeOptions, setCurIcon }) => {
  20. const [form] = Form.useForm();
  21. const type = Form.useWatch('type', form);
  22. const isDynamic = Form.useWatch('is_dynamic', form);
  23. const value = Form.useWatch('value', form);
  24. const { t } = useTranslation();
  25. // 资源类型选项
  26. const options = useMemo(() => {
  27. return resourceTypeOptions?.filter(item => item.value !== 'all') || [];
  28. }, [resourceTypeOptions]);
  29. // 获取非动态情况下,知识库、数据库、插件、编排工作流参数列表
  30. const { run, data, loading } = useRequest(
  31. async type => {
  32. const [, res] = await apiInterceptors(getResource({ type }));
  33. form.setFieldsValue({
  34. value: initValue?.value || res?.[0]?.key,
  35. });
  36. return res || [];
  37. },
  38. {
  39. manual: true,
  40. },
  41. );
  42. useEffect(() => {
  43. if (type) {
  44. run(type);
  45. }
  46. }, [run, type]);
  47. // 动态参数value选项
  48. const dynamicOptions = useMemo(() => {
  49. return (
  50. data?.map(item => {
  51. return {
  52. ...item,
  53. label: item.label,
  54. value: item.key + '',
  55. };
  56. }) || []
  57. );
  58. }, [data]);
  59. // 参数根据选择的资源类型动态变化
  60. const renderParameter = () => {
  61. if (type === 'image_file') {
  62. return null;
  63. // return (
  64. // <Form.Item label="上传图片:" name="value" valuePropName="fileList" required>
  65. // <Upload listType="picture-card">
  66. // <button style={{ border: 0, background: 'none' }} type="button">
  67. // <PlusOutlined />
  68. // <div style={{ marginTop: 8 }}>上传图标</div>
  69. // </button>
  70. // </Upload>
  71. // </Form.Item>
  72. // );
  73. }
  74. if (type === 'internet') {
  75. return null;
  76. // return (
  77. // <Form.Item label={returnLabel('互联网', 'xxx')} name="value">
  78. // <Switch style={{ background: value ? '#1677ff' : '#ccc' }} />
  79. // </Form.Item>
  80. // );
  81. }
  82. if (['text_file', 'excel_file'].includes(type)) {
  83. return null;
  84. // return (
  85. // <Form.Item label="上传文件:" name="value" required valuePropName="fileList">
  86. // <Upload>
  87. // <Button icon={<UploadOutlined />}>上传文件</Button>
  88. // </Upload>
  89. // </Form.Item>
  90. // );
  91. }
  92. return (
  93. <Form.Item label={t('resource_value')} name='value' required>
  94. <Select
  95. placeholder={t('please_select_param')}
  96. options={dynamicOptions}
  97. loading={loading}
  98. className='w-3/5'
  99. allowClear
  100. />
  101. </Form.Item>
  102. );
  103. };
  104. useEffect(() => {
  105. const rawVal = form.getFieldsValue();
  106. // 如果动态为true,这里手动清除一下动态参数value
  107. const value = rawVal?.is_dynamic ? '' : rawVal?.value;
  108. updateData({ uid, ...rawVal, value });
  109. }, [uid, isDynamic, form, updateData, value, type]);
  110. return (
  111. <div className={cls('flex flex-1', classNames)}>
  112. <Form
  113. style={{ width: '100%' }}
  114. form={form}
  115. labelCol={{ span: 4 }}
  116. initialValues={{
  117. ...initValue,
  118. }}
  119. >
  120. <Form.Item label={t('resource_type')} name='type'>
  121. <Select
  122. className='w-2/5'
  123. options={options}
  124. onChange={(val: string) => {
  125. setCurIcon({ uid, icon: val });
  126. }}
  127. />
  128. </Form.Item>
  129. <Form.Item label={t('resource_dynamic')} name='is_dynamic'>
  130. <Switch style={{ background: isDynamic ? '#1677ff' : '#ccc' }} />
  131. </Form.Item>
  132. {/* 如果选择了动态参数这里就不需要参数了 */}
  133. {!isDynamic && <> {renderParameter()}</>}
  134. </Form>
  135. </div>
  136. );
  137. };
  138. export default ResourceContent;