From 7372f330a41364fae26ce9c2f363d997fd8e0292 Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Tue, 2 Jul 2024 23:54:53 +0800 Subject: [PATCH] perf: optimize performance of the rule editor --- .../profile/rules-editor-viewer.tsx | 335 +++++++++--------- 1 file changed, 169 insertions(+), 166 deletions(-) diff --git a/src/components/profile/rules-editor-viewer.tsx b/src/components/profile/rules-editor-viewer.tsx index 99fa2d2b..6cf0f233 100644 --- a/src/components/profile/rules-editor-viewer.tsx +++ b/src/components/profile/rules-editor-viewer.tsx @@ -35,6 +35,7 @@ import { Notice, Switch } from "@/components/base"; import getSystem from "@/utils/get-system"; import { RuleItem } from "@/components/profile/rule-item"; import { BaseSearchBox } from "../base/base-search-box"; +import { Virtuoso } from "react-virtuoso"; interface Props { profileUid: string; @@ -350,92 +351,91 @@ export const RulesEditorViewer = (props: Props) => { {title ?? t("Edit Rules")} -
- - - - } - options={rules} - value={ruleType} - getOptionLabel={(option) => option.name} - renderOption={(props, option) => ( -
  • - {option.name} -
  • - )} - onChange={(_, value) => value && setRuleType(value)} - /> -
    - - + + + } + options={rules} + value={ruleType} + getOptionLabel={(option) => option.name} + renderOption={(props, option) => ( +
  • + {option.name} +
  • + )} + onChange={(_, value) => value && setRuleType(value)} + /> +
    + + - {ruleType.name === "RULE-SET" && ( - } - options={ruleSetList} - value={ruleContent} - onChange={(_, value) => value && setRuleContent(value)} - /> - )} - {ruleType.name === "SUB-RULE" && ( - } - options={subRuleList} - value={ruleContent} - onChange={(_, value) => value && setRuleContent(value)} - /> - )} - {ruleType.name !== "RULE-SET" && ruleType.name !== "SUB-RULE" && ( - setRuleContent(e.target.value)} - /> - )} - - - + {ruleType.name === "RULE-SET" && ( } - options={proxyPolicyList} - value={proxyPolicy} - renderOption={(props, option) => ( -
  • - {option} -
  • - )} - onChange={(_, value) => value && setProxyPolicy(value)} + options={ruleSetList} + value={ruleContent} + onChange={(_, value) => value && setRuleContent(value)} + /> + )} + {ruleType.name === "SUB-RULE" && ( + } + options={subRuleList} + value={ruleContent} + onChange={(_, value) => value && setRuleContent(value)} + /> + )} + {ruleType.name !== "RULE-SET" && ruleType.name !== "SUB-RULE" && ( + setRuleContent(e.target.value)} + /> + )} +
    + + + } + options={proxyPolicyList} + value={proxyPolicy} + renderOption={(props, option) => ( +
  • + {option} +
  • + )} + onChange={(_, value) => value && setProxyPolicy(value)} + /> +
    + {ruleType.noResolve && ( + + + setNoResolve(!noResolve)} /> - {ruleType.noResolve && ( - - - setNoResolve(!noResolve)} - /> - - )} -
    + )} -
    -
    -
    - setMatch(() => match)} - /> -
    - {prependSeq.length > 0 && ( - - - { - return x; - })} - > - {prependSeq.map((item, index) => { - return ( - { - setPrependSeq(prependSeq.filter((v) => v !== item)); - }} - /> - ); - })} - - - - )} + - - {ruleList - .filter((item) => match(item)) - .map((item, index) => { + + setMatch(() => match)} + /> + 0 ? 1 : 0) + + (appendSeq.length > 0 ? 1 : 0) + } + increaseViewportBy={256} + itemContent={(index) => { + let shift = prependSeq.length > 0 ? 1 : 0; + if (prependSeq.length > 0 && index === 0) { + return ( + + { + return x; + })} + > + {prependSeq.map((item, index) => { + return ( + { + setPrependSeq( + prependSeq.filter((v) => v !== item) + ); + }} + /> + ); + })} + + + ); + } else if (index < ruleList.length + shift) { + let newIndex = index - shift; return ( { - if (deleteSeq.includes(item)) { - setDeleteSeq(deleteSeq.filter((v) => v !== item)); + if (deleteSeq.includes(ruleList[newIndex])) { + setDeleteSeq( + deleteSeq.filter((v) => v !== ruleList[newIndex]) + ); } else { - setDeleteSeq([...deleteSeq, item]); + setDeleteSeq((prev) => [...prev, ruleList[newIndex]]); } }} /> ); - })} - - - {appendSeq.length > 0 && ( - - { - return x; - })} - > - - {appendSeq.map((item, index) => { - return ( - { - setAppendSeq(appendSeq.filter((v) => v !== item)); - }} - /> - ); - })} - - - - )} -
    + } else { + return ( + + { + return x; + })} + > + {appendSeq.map((item, index) => { + return ( + { + setAppendSeq(appendSeq.filter((v) => v !== item)); + }} + /> + ); + })} + + + ); + } + }} + /> +