refactor: replace 'let' with 'const' for better variable scoping and immutability

This commit is contained in:
Tunglies
2025-09-18 23:07:18 +08:00
parent 9d96ac0f6a
commit 324628dd3d
17 changed files with 154 additions and 121 deletions

View File

@@ -81,8 +81,8 @@ export const BaseSearchBox = (props: SearchProps) => {
return (content: string) => {
if (!searchText) return true;
let item = !matchCase ? content.toLowerCase() : content;
let searchItem = !matchCase ? searchText.toLowerCase() : searchText;
const item = !matchCase ? content.toLowerCase() : content;
const searchItem = !matchCase ? searchText.toLowerCase() : searchText;
if (useRegularExpression) {
return new RegExp(searchItem).test(item);

View File

@@ -19,7 +19,7 @@ interface Props {
}
export const GroupItem = (props: Props) => {
let { type, group, onDelete } = props;
const { type, group, onDelete } = props;
const sortable = type === "prepend" || type === "append";
const {

View File

@@ -159,8 +159,8 @@ export const GroupsEditorViewer = (props: Props) => {
}
};
const fetchContent = async () => {
let data = await readProfileFile(property);
let obj = yaml.load(data) as ISeqProfileConfig | null;
const data = await readProfileFile(property);
const obj = yaml.load(data) as ISeqProfileConfig | null;
setPrependSeq(obj?.prepend || []);
setAppendSeq(obj?.append || []);
@@ -174,7 +174,7 @@ export const GroupsEditorViewer = (props: Props) => {
if (currData === "") return;
if (visualization !== true) return;
let obj = yaml.load(currData) as {
const obj = yaml.load(currData) as {
prepend: [];
append: [];
delete: [];
@@ -209,21 +209,21 @@ export const GroupsEditorViewer = (props: Props) => {
}, [prependSeq, appendSeq, deleteSeq]);
const fetchProxyPolicy = async () => {
let data = await readProfileFile(profileUid);
let proxiesData = await readProfileFile(proxiesUid);
let originGroupsObj = yaml.load(data) as {
const data = await readProfileFile(profileUid);
const proxiesData = await readProfileFile(proxiesUid);
const originGroupsObj = yaml.load(data) as {
"proxy-groups": IProxyGroupConfig[];
} | null;
let originProxiesObj = yaml.load(data) as { proxies: [] } | null;
let originProxies = originProxiesObj?.proxies || [];
let moreProxiesObj = yaml.load(proxiesData) as ISeqProfileConfig | null;
let morePrependProxies = moreProxiesObj?.prepend || [];
let moreAppendProxies = moreProxiesObj?.append || [];
let moreDeleteProxies =
const originProxiesObj = yaml.load(data) as { proxies: [] } | null;
const originProxies = originProxiesObj?.proxies || [];
const moreProxiesObj = yaml.load(proxiesData) as ISeqProfileConfig | null;
const morePrependProxies = moreProxiesObj?.prepend || [];
const moreAppendProxies = moreProxiesObj?.append || [];
const moreDeleteProxies =
moreProxiesObj?.delete || ([] as string[] | { name: string }[]);
let proxies = morePrependProxies.concat(
const proxies = morePrependProxies.concat(
originProxies.filter((proxy: any) => {
if (proxy.name) {
return !moreDeleteProxies.includes(proxy.name);
@@ -246,28 +246,30 @@ export const GroupsEditorViewer = (props: Props) => {
);
};
const fetchProfile = async () => {
let data = await readProfileFile(profileUid);
let mergeData = await readProfileFile(mergeUid);
let globalMergeData = await readProfileFile("Merge");
const data = await readProfileFile(profileUid);
const mergeData = await readProfileFile(mergeUid);
const globalMergeData = await readProfileFile("Merge");
let originGroupsObj = yaml.load(data) as {
const originGroupsObj = yaml.load(data) as {
"proxy-groups": IProxyGroupConfig[];
} | null;
let originProviderObj = yaml.load(data) as { "proxy-providers": {} } | null;
let originProvider = originProviderObj?.["proxy-providers"] || {};
let moreProviderObj = yaml.load(mergeData) as {
const originProviderObj = yaml.load(data) as {
"proxy-providers": {};
} | null;
let moreProvider = moreProviderObj?.["proxy-providers"] || {};
const originProvider = originProviderObj?.["proxy-providers"] || {};
let globalProviderObj = yaml.load(globalMergeData) as {
const moreProviderObj = yaml.load(mergeData) as {
"proxy-providers": {};
} | null;
let globalProvider = globalProviderObj?.["proxy-providers"] || {};
const moreProvider = moreProviderObj?.["proxy-providers"] || {};
let provider = Object.assign(
const globalProviderObj = yaml.load(globalMergeData) as {
"proxy-providers": {};
} | null;
const globalProvider = globalProviderObj?.["proxy-providers"] || {};
const provider = Object.assign(
{},
originProvider,
moreProvider,
@@ -278,7 +280,7 @@ export const GroupsEditorViewer = (props: Props) => {
setGroupList(originGroupsObj?.["proxy-groups"] || []);
};
const getInterfaceNameList = async () => {
let list = await getNetworkInterfaces();
const list = await getNetworkInterfaces();
setInterfaceNameList(list);
};
useEffect(() => {
@@ -293,7 +295,7 @@ export const GroupsEditorViewer = (props: Props) => {
}, [open]);
const validateGroup = () => {
let group = formIns.getValues();
const group = formIns.getValues();
if (group.name === "") {
throw new Error(t("Group Name Required"));
}
@@ -794,7 +796,7 @@ export const GroupsEditorViewer = (props: Props) => {
}
increaseViewportBy={256}
itemContent={(index) => {
let shift = filteredPrependSeq.length > 0 ? 1 : 0;
const shift = filteredPrependSeq.length > 0 ? 1 : 0;
if (filteredPrependSeq.length > 0 && index === 0) {
return (
<DndContext
@@ -827,7 +829,7 @@ export const GroupsEditorViewer = (props: Props) => {
</DndContext>
);
} else if (index < filteredGroupList.length + shift) {
let newIndex = index - shift;
const newIndex = index - shift;
return (
<GroupItem
key={`${filteredGroupList[newIndex].name}-${index}`}

View File

@@ -132,8 +132,8 @@ export const ProxiesEditorViewer = (props: Props) => {
};
// 优化异步分片解析避免主线程阻塞解析完成后批量setState
const handleParseAsync = (cb: (proxies: IProxyConfig[]) => void) => {
let proxies: IProxyConfig[] = [];
let names: string[] = [];
const proxies: IProxyConfig[] = [];
const names: string[] = [];
let uris = "";
try {
uris = atob(proxyUri);
@@ -148,7 +148,7 @@ export const ProxiesEditorViewer = (props: Props) => {
for (; idx < end; idx++) {
const uri = lines[idx];
try {
let proxy = parseUri(uri.trim());
const proxy = parseUri(uri.trim());
if (!names.includes(proxy.name)) {
proxies.push(proxy);
names.push(proxy.name);
@@ -171,9 +171,9 @@ export const ProxiesEditorViewer = (props: Props) => {
parseBatch();
};
const fetchProfile = async () => {
let data = await readProfileFile(profileUid);
const data = await readProfileFile(profileUid);
let originProxiesObj = yaml.load(data) as {
const originProxiesObj = yaml.load(data) as {
proxies: IProxyConfig[];
} | null;
@@ -181,8 +181,8 @@ export const ProxiesEditorViewer = (props: Props) => {
};
const fetchContent = async () => {
let data = await readProfileFile(property);
let obj = yaml.load(data) as ISeqProfileConfig | null;
const data = await readProfileFile(property);
const obj = yaml.load(data) as ISeqProfileConfig | null;
setPrependSeq(obj?.prepend || []);
setAppendSeq(obj?.append || []);
@@ -196,7 +196,7 @@ export const ProxiesEditorViewer = (props: Props) => {
if (currData === "") return;
if (visualization !== true) return;
let obj = yaml.load(currData) as {
const obj = yaml.load(currData) as {
prepend: [];
append: [];
delete: [];
@@ -342,7 +342,7 @@ export const ProxiesEditorViewer = (props: Props) => {
}
increaseViewportBy={256}
itemContent={(index) => {
let shift = filteredPrependSeq.length > 0 ? 1 : 0;
const shift = filteredPrependSeq.length > 0 ? 1 : 0;
if (filteredPrependSeq.length > 0 && index === 0) {
return (
<DndContext
@@ -375,7 +375,7 @@ export const ProxiesEditorViewer = (props: Props) => {
</DndContext>
);
} else if (index < filteredProxyList.length + shift) {
let newIndex = index - shift;
const newIndex = index - shift;
return (
<ProxyItem
key={`${filteredProxyList[newIndex].name}-${index}`}

View File

@@ -17,7 +17,7 @@ interface Props {
}
export const ProxyItem = (props: Props) => {
let { type, proxy, onDelete } = props;
const { type, proxy, onDelete } = props;
const sortable = type === "prepend" || type === "append";
const {

View File

@@ -16,7 +16,7 @@ interface Props {
}
export const RuleItem = (props: Props) => {
let { type, ruleRaw, onDelete } = props;
const { type, ruleRaw, onDelete } = props;
const sortable = type === "prepend" || type === "append";
const rule = ruleRaw.replace(",no-resolve", "");

View File

@@ -287,8 +287,8 @@ export const RulesEditorViewer = (props: Props) => {
const { active, over } = event;
if (over) {
if (active.id !== over.id) {
let activeIndex = prependSeq.indexOf(active.id.toString());
let overIndex = prependSeq.indexOf(over.id.toString());
const activeIndex = prependSeq.indexOf(active.id.toString());
const overIndex = prependSeq.indexOf(over.id.toString());
setPrependSeq(reorder(prependSeq, activeIndex, overIndex));
}
}
@@ -297,15 +297,15 @@ export const RulesEditorViewer = (props: Props) => {
const { active, over } = event;
if (over) {
if (active.id !== over.id) {
let activeIndex = appendSeq.indexOf(active.id.toString());
let overIndex = appendSeq.indexOf(over.id.toString());
const activeIndex = appendSeq.indexOf(active.id.toString());
const overIndex = appendSeq.indexOf(over.id.toString());
setAppendSeq(reorder(appendSeq, activeIndex, overIndex));
}
}
};
const fetchContent = async () => {
let data = await readProfileFile(property);
let obj = yaml.load(data) as ISeqProfileConfig | null;
const data = await readProfileFile(property);
const obj = yaml.load(data) as ISeqProfileConfig | null;
setPrependSeq(obj?.prepend || []);
setAppendSeq(obj?.append || []);
@@ -319,7 +319,7 @@ export const RulesEditorViewer = (props: Props) => {
if (currData === "") return;
if (visualization !== true) return;
let obj = yaml.load(currData) as ISeqProfileConfig | null;
const obj = yaml.load(currData) as ISeqProfileConfig | null;
setPrependSeq(obj?.prepend || []);
setAppendSeq(obj?.append || []);
setDeleteSeq(obj?.delete || []);
@@ -349,21 +349,21 @@ export const RulesEditorViewer = (props: Props) => {
}, [prependSeq, appendSeq, deleteSeq]);
const fetchProfile = async () => {
let data = await readProfileFile(profileUid); // 原配置文件
let groupsData = await readProfileFile(groupsUid); // groups配置文件
let mergeData = await readProfileFile(mergeUid); // merge配置文件
let globalMergeData = await readProfileFile("Merge"); // global merge配置文件
const data = await readProfileFile(profileUid); // 原配置文件
const groupsData = await readProfileFile(groupsUid); // groups配置文件
const mergeData = await readProfileFile(mergeUid); // merge配置文件
const globalMergeData = await readProfileFile("Merge"); // global merge配置文件
let rulesObj = yaml.load(data) as { rules: [] } | null;
const 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 ISeqProfileConfig | null;
let morePrependGroups = moreGroupsObj?.["prepend"] || [];
let moreAppendGroups = moreGroupsObj?.["append"] || [];
let moreDeleteGroups =
const originGroupsObj = yaml.load(data) as { "proxy-groups": [] } | null;
const originGroups = originGroupsObj?.["proxy-groups"] || [];
const moreGroupsObj = yaml.load(groupsData) as ISeqProfileConfig | null;
const morePrependGroups = moreGroupsObj?.["prepend"] || [];
const moreAppendGroups = moreGroupsObj?.["append"] || [];
const moreDeleteGroups =
moreGroupsObj?.["delete"] || ([] as string[] | { name: string }[]);
let groups = morePrependGroups.concat(
const groups = morePrependGroups.concat(
originGroups.filter((group: any) => {
if (group.name) {
return !moreDeleteGroups.includes(group.name);
@@ -374,27 +374,37 @@ export const RulesEditorViewer = (props: Props) => {
moreAppendGroups,
);
let originRuleSetObj = yaml.load(data) as { "rule-providers": {} } | null;
let originRuleSet = originRuleSetObj?.["rule-providers"] || {};
let moreRuleSetObj = yaml.load(mergeData) as {
const originRuleSetObj = yaml.load(data) as { "rule-providers": {} } | null;
const originRuleSet = originRuleSetObj?.["rule-providers"] || {};
const moreRuleSetObj = yaml.load(mergeData) as {
"rule-providers": {};
} | null;
let moreRuleSet = moreRuleSetObj?.["rule-providers"] || {};
let globalRuleSetObj = yaml.load(globalMergeData) as {
const moreRuleSet = moreRuleSetObj?.["rule-providers"] || {};
const globalRuleSetObj = yaml.load(globalMergeData) as {
"rule-providers": {};
} | null;
let globalRuleSet = globalRuleSetObj?.["rule-providers"] || {};
let ruleSet = Object.assign({}, originRuleSet, moreRuleSet, globalRuleSet);
const globalRuleSet = globalRuleSetObj?.["rule-providers"] || {};
const 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 {
const originSubRuleObj = yaml.load(data) as { "sub-rules": {} } | null;
const originSubRule = originSubRuleObj?.["sub-rules"] || {};
const moreSubRuleObj = yaml.load(mergeData) as { "sub-rules": {} } | null;
const moreSubRule = moreSubRuleObj?.["sub-rules"] || {};
const globalSubRuleObj = yaml.load(globalMergeData) as {
"sub-rules": {};
} | null;
let globalSubRule = globalSubRuleObj?.["sub-rules"] || {};
let subRule = Object.assign({}, originSubRule, moreSubRule, globalSubRule);
const globalSubRule = globalSubRuleObj?.["sub-rules"] || {};
const subRule = Object.assign(
{},
originSubRule,
moreSubRule,
globalSubRule,
);
setProxyPolicyList(
builtinProxyPolicies.concat(groups.map((group: any) => group.name)),
);
@@ -554,7 +564,7 @@ export const RulesEditorViewer = (props: Props) => {
startIcon={<VerticalAlignTopRounded />}
onClick={() => {
try {
let raw = validateRule();
const raw = validateRule();
if (prependSeq.includes(raw)) return;
setPrependSeq([raw, ...prependSeq]);
} catch (err: any) {
@@ -572,7 +582,7 @@ export const RulesEditorViewer = (props: Props) => {
startIcon={<VerticalAlignBottomRounded />}
onClick={() => {
try {
let raw = validateRule();
const raw = validateRule();
if (appendSeq.includes(raw)) return;
setAppendSeq([...appendSeq, raw]);
} catch (err: any) {
@@ -601,7 +611,7 @@ export const RulesEditorViewer = (props: Props) => {
}
increaseViewportBy={256}
itemContent={(index) => {
let shift = filteredPrependSeq.length > 0 ? 1 : 0;
const shift = filteredPrependSeq.length > 0 ? 1 : 0;
if (filteredPrependSeq.length > 0 && index === 0) {
return (
<DndContext
@@ -632,7 +642,7 @@ export const RulesEditorViewer = (props: Props) => {
</DndContext>
);
} else if (index < filteredRuleList.length + shift) {
let newIndex = index - shift;
const newIndex = index - shift;
return (
<RuleItem
key={`${filteredRuleList[newIndex]}-${index}`}

View File

@@ -379,7 +379,7 @@ export const DnsViewer = forwardRef<DialogRef>((props, ref) => {
const formatHosts = (hosts: any): string => {
if (!hosts || typeof hosts !== "object") return "";
let result: string[] = [];
const result: string[] = [];
Object.entries(hosts).forEach(([domain, value]) => {
if (Array.isArray(value)) {

View File

@@ -53,7 +53,7 @@ export const TunViewer = forwardRef<DialogRef>((props, ref) => {
const onSave = useLockFn(async () => {
try {
let tun = {
const tun = {
stack: values.stack,
device:
values.device === ""
@@ -97,7 +97,7 @@ export const TunViewer = forwardRef<DialogRef>((props, ref) => {
variant="outlined"
size="small"
onClick={async () => {
let tun = {
const tun = {
stack: "gvisor",
device: OS === "macos" ? "utun1024" : "Mihomo",
"auto-route": true,