diff --git a/src/pages/rules/components/CopyRule/Multi‌CopyPopup.tsx b/src/pages/rules/components/CopyRule/Multi‌CopyPopup.tsx index 82291b8..c8a16cb 100644 --- a/src/pages/rules/components/CopyRule/Multi‌CopyPopup.tsx +++ b/src/pages/rules/components/CopyRule/Multi‌CopyPopup.tsx @@ -232,22 +232,22 @@ const MultiCopyPopup = ({ onValuesChange={({ sourceDataSourceId }) => { if (sourceDataSourceId) { const curFormRef = formMapRef?.current?.[0]?.current; - const curTargetDataSourceId = - curFormRef?.getFieldValue('targetDataSourceId'); - if (curTargetDataSourceId === sourceDataSourceId) { - let targetDataSourceId: string | number | null = null; - for (const item of dataSourceList) { - if (item.id !== sourceDataSourceId) { - targetDataSourceId = item.id; - break; - } - } - if (targetDataSourceId) { - curFormRef?.setFieldsValue({ - targetDataSourceId, - }); - setTargetDataSourceId(targetDataSourceId); - } + const curTargetId = curFormRef?.getFieldValue('targetDataSourceId'); + const sourceItem = dataSourceList.find( + (item) => item.id === sourceDataSourceId, + ); + const targetItem = dataSourceList.find( + (item) => item.id === curTargetId, + ); + if (targetItem && sourceItem && targetItem.sourceType !== sourceItem.sourceType) { + const fallback = + dataSourceList.find( + (item) => + item.id !== sourceDataSourceId && + item.sourceType === sourceItem.sourceType, + )?.id || sourceDataSourceId; + curFormRef?.setFieldsValue({ targetDataSourceId: fallback }); + setTargetDataSourceId(fallback); } } }} diff --git a/src/pages/rules/components/CopyRule/SingleCopyPopup.tsx b/src/pages/rules/components/CopyRule/SingleCopyPopup.tsx index e096366..6a34012 100644 --- a/src/pages/rules/components/CopyRule/SingleCopyPopup.tsx +++ b/src/pages/rules/components/CopyRule/SingleCopyPopup.tsx @@ -17,7 +17,7 @@ import { Row, Typography, } from 'antd'; -import { useEffect, useRef, useState } from 'react'; +import { useEffect, useMemo, useRef, useState } from 'react'; import useRichFormat from '@/hooks/useRichI18n'; import { $t } from '@/utils/i18n'; import { type SchemaType, schemaMap } from '../../ruleFormSchema'; @@ -68,49 +68,83 @@ const SingleCopyPopup = ({ } = schemaMap[currentRuleType as unknown as SchemaType]?.({ richFormat }) || {}; - const rightDataSourceList = dataSourceList.filter( - (item) => item.id !== formValues?.dataSourceId, - ); + const sourceSourceType = useMemo(() => { + return dataSourceList.find((item) => item.id === formValues?.dataSourceId) + ?.sourceType; + }, [dataSourceList, formValues?.dataSourceId]); + + const dataSourceOptions = useMemo(() => { + return dataSourceList.map((item) => ({ + ...item, + disabled: item.sourceType !== sourceSourceType, + label: ( + + {item.sourceName} + + MT{item.sourceType} + + + ), + })); + }, [dataSourceList, sourceSourceType]); const initialValues = transformInitialValue?.(formValues) || formValues; - const rightFirstDataSource = rightDataSourceList[0]; + const rightFirstDataSource = + dataSourceOptions.find( + (item) => item.id !== formValues?.dataSourceId && !item.disabled, + ) || dataSourceOptions[0]; + const isSameSource = rightFirstDataSource?.id === formValues?.dataSourceId; const copyFormValues = { ...initialValues, name: initialValues?.name ? `${initialValues.name} - ${rightFirstDataSource?.sourceName}` : `${Date.now()} - ${rightFirstDataSource?.sourceName}`, - groupFilter: '', id: void 0, dataSourceId: rightFirstDataSource?.id || '', + ...(isSameSource + ? {} + : { + groupFilter: '', + ...(symbolFormShow ? { param2: '' } : {}), + ...(assetFormShow ? { param1: '' } : {}), + }), }; - if (symbolFormShow) { - copyFormValues.param2 = ''; - } - if (assetFormShow) { - copyFormValues.param1 = ''; - } const formContent = ({ - dataSourceList, + dataSourceList: options, formAttr, + onDataSourceChange, }: { - dataSourceList: API.DataSourceListItem[]; + dataSourceList: any[]; formAttr: Record; + onDataSourceChange?: (dataSourceId: string | number) => void; }) => { return ( - + { + if (changedValues.dataSourceId && onDataSourceChange) { + onDataSourceChange(changedValues.dataSourceId); + } + }} + > @@ -194,53 +228,64 @@ const SingleCopyPopup = ({ }, }} > - - -
- 📋 {$t('pages.rules.originalRule')} -
- {formContent({ - dataSourceList, - formAttr: { initialValues, disabled: true }, - })} - + {visible && ( + + +
+ 📋 {$t('pages.rules.originalRule')} +
+ {formContent({ + dataSourceList: dataSourceOptions, + formAttr: { initialValues, disabled: true }, + })} + - - - - - - + + + + + + - -
- -
- + +
+ +
+ - -
- ✏️ {$t('pages.rules.cloneRulePreview')} -
+ +
+ ✏️ {$t('pages.rules.cloneRulePreview')} +
- {formContent({ - dataSourceList: rightDataSourceList, - formAttr: { - initialValues: copyFormValues, - formRef: formRef, - disabled: confirmLoading, - }, - })} - {errMsg && ( - - )} - -
+ {formContent({ + dataSourceList: dataSourceOptions, + formAttr: { + initialValues: copyFormValues, + formRef: formRef, + disabled: confirmLoading, + }, + onDataSourceChange: (dataSourceId) => { + const isSame = dataSourceId === formValues?.dataSourceId; + if (!isSame) { + const fields: Record = { groupFilter: '' }; + if (symbolFormShow) fields.param2 = ''; + if (assetFormShow) fields.param1 = ''; + formRef.current?.setFieldsValue(fields); + } + }, + })} + {errMsg && ( + + )} + +
+ )} ); }; diff --git a/src/pages/rules/components/CopyRule/comp/Step1.tsx b/src/pages/rules/components/CopyRule/comp/Step1.tsx index c897168..1b8293e 100644 --- a/src/pages/rules/components/CopyRule/comp/Step1.tsx +++ b/src/pages/rules/components/CopyRule/comp/Step1.tsx @@ -1,7 +1,7 @@ import { ArrowDownOutlined } from '@ant-design/icons'; import { ProFormDependency, ProFormSelect } from '@ant-design/pro-components'; -import { Divider, Tag } from 'antd'; -import { useContext, useEffect } from 'react'; +import { Divider, Flex, Tag, Typography } from 'antd'; +import { useContext, useEffect, useMemo } from 'react'; import { $t } from '@/utils/i18n'; import { MultiCopyPopupContext } from '../Multi‌CopyPopup'; import RuleCheckCard from './RuleCheckCard'; @@ -13,11 +13,52 @@ type Step1Props = { const Step1 = ({ formMapRef, setTargetDataSourceId }: Step1Props) => { const { dataSourceList } = useContext(MultiCopyPopupContext); + const buildOptions = (sourceId?: string | number) => { + const sourceType = dataSourceList.find((item) => item.id === sourceId) + ?.sourceType; + return dataSourceList.map((item) => ({ + ...item, + disabled: item.sourceType !== sourceType, + label: ( + + {item.sourceName} + + MT{item.sourceType} + + + ), + })); + }; + + const sourceOptions = useMemo( + () => dataSourceList.map((item) => ({ + ...item, + label: ( + + {item.sourceName} + + MT{item.sourceType} + + + ), + })), + [dataSourceList], + ); + useEffect(() => { if (dataSourceList?.length) { - // waitTime(1000).then(() => { - const sourceDataSourceId = dataSourceList[0]?.id || ''; - const targetDataSourceId = dataSourceList[1]?.id || ''; + const sourceItem = dataSourceList[0]; + const sourceDataSourceId = sourceItem?.id || ''; + const sameTypeItem = dataSourceList.find( + (item) => item.id !== sourceDataSourceId && item.sourceType === sourceItem?.sourceType, + ); + const targetDataSourceId = sameTypeItem?.id || sourceDataSourceId; const curFormRef = formMapRef?.current?.[0]?.current; if (curFormRef) { curFormRef?.setFieldsValue({ @@ -26,7 +67,6 @@ const Step1 = ({ formMapRef, setTargetDataSourceId }: Step1Props) => { }); setTargetDataSourceId(targetDataSourceId); } - // }); } }, [dataSourceList]); @@ -35,14 +75,15 @@ const Step1 = ({ formMapRef, setTargetDataSourceId }: Step1Props) => { @@ -61,21 +102,20 @@ const Step1 = ({ formMapRef, setTargetDataSourceId }: Step1Props) => { {({ sourceDataSourceId }) => { if (!sourceDataSourceId) return null; - const dataSourceOptions = dataSourceList.filter( - (item) => item.id !== sourceDataSourceId, - ); + const targetOptions = buildOptions(sourceDataSourceId); return ( { setTargetDataSourceId(value || null); diff --git a/src/pages/rules/index.tsx b/src/pages/rules/index.tsx index b986537..bae3da6 100644 --- a/src/pages/rules/index.tsx +++ b/src/pages/rules/index.tsx @@ -295,10 +295,6 @@ const RuleCommon: React.FC = () => {