From e8440e06a1071501cdab4d2c10e1d235c0d73c04 Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Wed, 3 Jul 2024 22:13:24 +0800 Subject: [PATCH] feat: support get merged rule-set name --- src/components/profile/profile-item.tsx | 2 + .../profile/rules-editor-viewer.tsx | 88 +++++++++++++------ 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/components/profile/profile-item.tsx b/src/components/profile/profile-item.tsx index 1ce8587..88243ad 100644 --- a/src/components/profile/profile-item.tsx +++ b/src/components/profile/profile-item.tsx @@ -483,6 +483,8 @@ export const ProfileItem = (props: Props) => { onClose={() => setFileOpen(false)} /> { - const { title, profileUid, property, open, onClose, onSave } = props; + const { + title, + groupsUid, + mergeUid, + profileUid, + property, + open, + onClose, + onSave, + } = props; const { t } = useTranslation(); const themeMode = useThemeMode(); @@ -291,11 +302,11 @@ export const RulesEditorViewer = (props: Props) => { }; const fetchContent = async () => { let data = await readProfileFile(property); - let obj = yaml.load(data) as { prepend: []; append: []; delete: [] }; + let obj = yaml.load(data) as { prepend: []; append: []; delete: [] } | null; - setPrependSeq(obj.prepend || []); - setAppendSeq(obj.append || []); - setDeleteSeq(obj.delete || []); + setPrependSeq(obj?.prepend || []); + setAppendSeq(obj?.append || []); + setDeleteSeq(obj?.delete || []); setPrevData(data); setCurrData(data); @@ -305,10 +316,14 @@ export const RulesEditorViewer = (props: Props) => { if (currData === "") return; if (visible !== true) return; - let obj = yaml.load(currData) as { prepend: []; append: []; delete: [] }; - setPrependSeq(obj.prepend || []); - setAppendSeq(obj.append || []); - setDeleteSeq(obj.delete || []); + let obj = yaml.load(currData) as { + prepend: []; + append: []; + delete: []; + } | null; + setPrependSeq(obj?.prepend || []); + setAppendSeq(obj?.append || []); + setDeleteSeq(obj?.delete || []); }, [visible]); useEffect(() => { @@ -320,26 +335,45 @@ export const RulesEditorViewer = (props: Props) => { const fetchProfile = async () => { let data = await readProfileFile(profileUid); - let groupsObj = yaml.load(data) as { "proxy-groups": [] }; - let rulesObj = yaml.load(data) as { rules: [] }; - let ruleSetObj = yaml.load(data) as { "rule-providers": [] }; - let subRuleObj = yaml.load(data) as { "sub-rules": [] }; + let groupsData = await readProfileFile(groupsUid); + let mergeData = await readProfileFile(mergeUid); + let globalMergeData = await readProfileFile("Merge"); + + let rulesObj = yaml.load(data) as { rules: [] } | null; + + let originGroupsObj = yaml.load(data) as { "proxy-groups": [] } | null; + let originGroups = originGroupsObj?.["proxy-groups"] || []; + let moreGroupsObj = yaml.load(groupsData) as { "proxy-groups": [] } | null; + let moreGroups = moreGroupsObj?.["proxy-groups"] || []; + let groups = originGroups.concat(moreGroups); + + let originRuleSetObj = yaml.load(data) as { "rule-providers": {} } | null; + let originRuleSet = originRuleSetObj?.["rule-providers"] || {}; + let moreRuleSetObj = yaml.load(mergeData) as { + "rule-providers": {}; + } | null; + let moreRuleSet = moreRuleSetObj?.["rule-providers"] || {}; + let globalRuleSetObj = yaml.load(globalMergeData) as { + "rule-providers": {}; + } | null; + let globalRuleSet = globalRuleSetObj?.["rule-providers"] || {}; + let ruleSet = Object.assign({}, originRuleSet, moreRuleSet, globalRuleSet); + + let originSubRuleObj = yaml.load(data) as { "sub-rules": {} } | null; + let originSubRule = originSubRuleObj?.["sub-rules"] || {}; + let moreSubRuleObj = yaml.load(mergeData) as { "sub-rules": {} } | null; + let moreSubRule = moreSubRuleObj?.["sub-rules"] || {}; + let globalSubRuleObj = yaml.load(globalMergeData) as { + "sub-rules": {}; + } | null; + let globalSubRule = globalSubRuleObj?.["sub-rules"] || {}; + let subRule = Object.assign({}, originSubRule, moreSubRule, globalSubRule); setProxyPolicyList( - builtinProxyPolicies.concat( - groupsObj["proxy-groups"] - ? groupsObj["proxy-groups"].map((item: any) => item.name) - : [] - ) - ); - setRuleList(rulesObj.rules || []); - setRuleSetList( - ruleSetObj["rule-providers"] - ? Object.keys(ruleSetObj["rule-providers"]) - : [] - ); - setSubRuleList( - subRuleObj["sub-rules"] ? Object.keys(subRuleObj["sub-rules"]) : [] + builtinProxyPolicies.concat(groups.map((group: any) => group.name)) ); + setRuleSetList(Object.keys(ruleSet)); + setSubRuleList(Object.keys(subRule)); + setRuleList(rulesObj?.rules || []); }; useEffect(() => {