feat: support update all profiles

This commit is contained in:
GyDi
2023-03-16 20:32:41 +08:00
parent 4fde644733
commit 6c0066dbfb
5 changed files with 91 additions and 6 deletions

View File

@@ -15,6 +15,7 @@
"global": "global",
"direct": "direct",
"script": "script",
"Profiles": "Profiles",
"Profile URL": "Profile URL",
"Import": "Import",
@@ -34,6 +35,9 @@
"Refresh": "Refresh",
"To Top": "To Top",
"To End": "To End",
"Update All Profiles": "Update All Profiles",
"View Runtime Config": "View Runtime Config",
"Reactivate Profiles": "Reactivate Profiles",
"Location": "Location",
"Delay check": "Delay check",

View File

@@ -15,6 +15,7 @@
"global": "全局",
"direct": "直连",
"script": "脚本",
"Profiles": "配置",
"Profile URL": "配置文件链接",
"Import": "导入",
@@ -34,6 +35,9 @@
"Refresh": "刷新",
"To Top": "移到最前",
"To End": "移到末尾",
"Update All Profiles": "更新所有配置",
"View Runtime Config": "查看运行时配置",
"Reactivate Profiles": "重新激活配置",
"Location": "当前节点",
"Delay check": "延迟测试",

View File

@@ -1,8 +1,13 @@
import useSWR, { mutate } from "swr";
import { useLockFn } from "ahooks";
import { useMemo, useRef, useState } from "react";
import { useLockFn } from "ahooks";
import { useSetRecoilState } from "recoil";
import { Box, Button, Grid, IconButton, Stack, TextField } from "@mui/material";
import { CachedRounded } from "@mui/icons-material";
import {
LocalFireDepartmentRounded,
RefreshRounded,
TextSnippetOutlined,
} from "@mui/icons-material";
import { useTranslation } from "react-i18next";
import {
getProfiles,
@@ -10,9 +15,11 @@ import {
enhanceProfiles,
getRuntimeLogs,
deleteProfile,
updateProfile,
} from "@/services/cmds";
import { atomLoadingCache } from "@/services/states";
import { closeAllConnections } from "@/services/api";
import { BasePage, Notice } from "@/components/base";
import { BasePage, DialogRef, Notice } from "@/components/base";
import {
ProfileViewer,
ProfileViewerRef,
@@ -20,6 +27,8 @@ import {
import { ProfileItem } from "@/components/profile/profile-item";
import { ProfileMore } from "@/components/profile/profile-more";
import { useProfiles } from "@/hooks/use-profiles";
import { ConfigViewer } from "@/components/setting/mods/config-viewer";
import { throttle } from "lodash-es";
const ProfilePage = () => {
const { t } = useTranslation();
@@ -41,6 +50,7 @@ const ProfilePage = () => {
const chain = profiles.chain || [];
const viewerRef = useRef<ProfileViewerRef>(null);
const configRef = useRef<DialogRef>(null);
// distinguish type
const { regularItems, enhanceItems } = useMemo(() => {
@@ -149,18 +159,65 @@ const ProfilePage = () => {
mutateLogs();
});
// 更新所有配置
const setLoadingCache = useSetRecoilState(atomLoadingCache);
const onUpdateAll = useLockFn(async () => {
const throttleMutate = throttle(mutateProfiles, 2000, {
trailing: true,
});
const updateOne = async (uid: string) => {
try {
await updateProfile(uid);
throttleMutate();
} finally {
setLoadingCache((cache) => ({ ...cache, [uid]: false }));
}
};
return new Promise((resolve) => {
setLoadingCache((cache) => {
// 获取没有正在更新的配置
const items = regularItems.filter(
(e) => e.type === "remote" && !cache[e.uid]
);
const change = Object.fromEntries(items.map((e) => [e.uid, true]));
Promise.allSettled(items.map((e) => updateOne(e.uid))).then(resolve);
return { ...cache, ...change };
});
});
});
return (
<BasePage
title={t("Profiles")}
header={
<Box sx={{ mt: 1, display: "flex", alignItems: "center" }}>
<Box sx={{ mt: 1, display: "flex", alignItems: "center", gap: 1 }}>
<IconButton
size="small"
color="inherit"
title={t("Refresh profiles")}
title={t("Update All Profiles")}
onClick={onUpdateAll}
>
<RefreshRounded />
</IconButton>
<IconButton
size="small"
color="inherit"
title={t("View Runtime Config")}
onClick={() => configRef.current?.open()}
>
<TextSnippetOutlined />
</IconButton>
<IconButton
size="small"
color="primary"
title={t("Reactivate Profiles")}
onClick={onEnhance}
>
<CachedRounded />
<LocalFireDepartmentRounded />
</IconButton>
</Box>
}
@@ -232,6 +289,7 @@ const ProfilePage = () => {
)}
<ProfileViewer ref={viewerRef} onChange={() => mutateProfiles()} />
<ConfigViewer ref={configRef} />
</BasePage>
);
};