refactor(copy-rule): extract context and improve batch clone modal

- Extract MultiCopyPopupContext to standalone context.ts file
- Add Task type and improve type safety in BatchCloneModal
- Use useRef/useCallback to stabilize batch clone handlers
- Add cleanup effect for selectRules in RuleCheckCard on unmount

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-15 23:05:56 +08:00
parent 16999ca583
commit 3c636bcaa2
6 changed files with 61 additions and 48 deletions

View File

@@ -1,22 +1,20 @@
import { CheckCard, ProCard } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max';
import { Col, Row, Spin } from 'antd';
import { useContext, useState } from 'react';
import { useContext, useEffect, useState } from 'react';
import { getRuleList } from '@/services/api';
import { $t } from '@/utils/i18n';
import { RuleCardBodyInfo } from '../../RuleCardBody';
import { MultiCopyPopupContext } from '../MultiCopyPopup';
import { MultiCopyPopupContext } from '../context';
type RuleCheckCardProps = {
dataSourceId: string | number;
onChange?: (id: string | number) => void;
};
const RuleCheckCard = ({ dataSourceId }: RuleCheckCardProps) => {
const [ruleList, setRuleList] = useState<API.RuleListItem[]>([]);
const { ruleMeta, currentRuleType, setSelectRules } = useContext(
MultiCopyPopupContext,
);
const [ruleList, setRuleList] = useState<API.RuleListItem[]>([]);
const { loading } = useRequest(
() =>
@@ -25,7 +23,6 @@ const RuleCheckCard = ({ dataSourceId }: RuleCheckCardProps) => {
dataSourceId,
}),
{
// manual: true,
refreshDeps: [dataSourceId, currentRuleType],
onSuccess: ({ data = [] }: { data: API.RuleListItem[] }) => {
setRuleList(data);
@@ -37,15 +34,17 @@ const RuleCheckCard = ({ dataSourceId }: RuleCheckCardProps) => {
},
);
useEffect(() => {
return () => setSelectRules([]);
}, []);
if (!dataSourceId) return <div>{$t('pages.clones.step1.tips')}</div>;
if (loading) return <Spin />;
// const
return (
<ProCard
title={$t('pages.clones.step1.sourceRules', {
count: ruleList?.length,
count: ruleList.length,
ruleType: ruleMeta?.title || '-',
})}
collapsible