feat: support update all profiles

This commit is contained in:
GyDi 2023-03-16 20:32:41 +08:00
parent 4fde644733
commit 6c0066dbfb
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A
5 changed files with 91 additions and 6 deletions

View File

@ -29,6 +29,7 @@
"axios": "^1.1.3", "axios": "^1.1.3",
"dayjs": "1.11.5", "dayjs": "1.11.5",
"i18next": "^22.0.4", "i18next": "^22.0.4",
"lodash-es": "^4.17.21",
"monaco-editor": "^0.34.1", "monaco-editor": "^0.34.1",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
@ -47,6 +48,7 @@
"@types/fs-extra": "^9.0.13", "@types/fs-extra": "^9.0.13",
"@types/js-cookie": "^3.0.2", "@types/js-cookie": "^3.0.2",
"@types/lodash": "^4.14.180", "@types/lodash": "^4.14.180",
"@types/lodash-es": "^4.17.7",
"@types/react": "^17.0.0", "@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0", "@types/react-dom": "^17.0.0",
"@vitejs/plugin-react": "^2.0.1", "@vitejs/plugin-react": "^2.0.1",

View File

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

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": "更新所有配置",
"View Runtime Config": "查看运行时配置",
"Reactivate Profiles": "重新激活配置",
"Location": "当前节点", "Location": "当前节点",
"Delay check": "延迟测试", "Delay check": "延迟测试",

View File

@ -1,8 +1,13 @@
import useSWR, { mutate } from "swr"; import useSWR, { mutate } from "swr";
import { useLockFn } from "ahooks";
import { useMemo, useRef, useState } from "react"; 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 { 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 { useTranslation } from "react-i18next";
import { import {
getProfiles, getProfiles,
@ -10,9 +15,11 @@ import {
enhanceProfiles, enhanceProfiles,
getRuntimeLogs, getRuntimeLogs,
deleteProfile, deleteProfile,
updateProfile,
} from "@/services/cmds"; } from "@/services/cmds";
import { atomLoadingCache } from "@/services/states";
import { closeAllConnections } from "@/services/api"; import { closeAllConnections } from "@/services/api";
import { BasePage, Notice } from "@/components/base"; import { BasePage, DialogRef, Notice } from "@/components/base";
import { import {
ProfileViewer, ProfileViewer,
ProfileViewerRef, ProfileViewerRef,
@ -20,6 +27,8 @@ import {
import { ProfileItem } from "@/components/profile/profile-item"; import { ProfileItem } from "@/components/profile/profile-item";
import { ProfileMore } from "@/components/profile/profile-more"; import { ProfileMore } from "@/components/profile/profile-more";
import { useProfiles } from "@/hooks/use-profiles"; import { useProfiles } from "@/hooks/use-profiles";
import { ConfigViewer } from "@/components/setting/mods/config-viewer";
import { throttle } from "lodash-es";
const ProfilePage = () => { const ProfilePage = () => {
const { t } = useTranslation(); const { t } = useTranslation();
@ -41,6 +50,7 @@ const ProfilePage = () => {
const chain = profiles.chain || []; const chain = profiles.chain || [];
const viewerRef = useRef<ProfileViewerRef>(null); const viewerRef = useRef<ProfileViewerRef>(null);
const configRef = useRef<DialogRef>(null);
// distinguish type // distinguish type
const { regularItems, enhanceItems } = useMemo(() => { const { regularItems, enhanceItems } = useMemo(() => {
@ -149,18 +159,65 @@ const ProfilePage = () => {
mutateLogs(); 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 ( return (
<BasePage <BasePage
title={t("Profiles")} title={t("Profiles")}
header={ header={
<Box sx={{ mt: 1, display: "flex", alignItems: "center" }}> <Box sx={{ mt: 1, display: "flex", alignItems: "center", gap: 1 }}>
<IconButton <IconButton
size="small" size="small"
color="inherit" 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} onClick={onEnhance}
> >
<CachedRounded /> <LocalFireDepartmentRounded />
</IconButton> </IconButton>
</Box> </Box>
} }
@ -232,6 +289,7 @@ const ProfilePage = () => {
)} )}
<ProfileViewer ref={viewerRef} onChange={() => mutateProfiles()} /> <ProfileViewer ref={viewerRef} onChange={() => mutateProfiles()} />
<ConfigViewer ref={configRef} />
</BasePage> </BasePage>
); );
}; };

View File

@ -869,6 +869,18 @@
resolved "https://registry.yarnpkg.com/@types/js-cookie/-/js-cookie-3.0.2.tgz#451eaeece64c6bdac8b2dde0caab23b085899e0d" resolved "https://registry.yarnpkg.com/@types/js-cookie/-/js-cookie-3.0.2.tgz#451eaeece64c6bdac8b2dde0caab23b085899e0d"
integrity sha512-6+0ekgfusHftJNYpihfkMu8BWdeHs9EOJuGcSofErjstGPfPGEu9yTu4t460lTzzAMl2cM5zngQJqPMHbbnvYA== integrity sha512-6+0ekgfusHftJNYpihfkMu8BWdeHs9EOJuGcSofErjstGPfPGEu9yTu4t460lTzzAMl2cM5zngQJqPMHbbnvYA==
"@types/lodash-es@^4.17.7":
version "4.17.7"
resolved "https://registry.npmmirror.com/@types/lodash-es/-/lodash-es-4.17.7.tgz#22edcae9f44aff08546e71db8925f05b33c7cc40"
integrity sha512-z0ptr6UI10VlU6l5MYhGwS4mC8DZyYer2mCoyysZtSF7p26zOX8UpbrV0YpNYLGS8K4PUFIyEr62IMFFjveSiQ==
dependencies:
"@types/lodash" "*"
"@types/lodash@*":
version "4.14.191"
resolved "https://registry.npmmirror.com/@types/lodash/-/lodash-4.14.191.tgz#09511e7f7cba275acd8b419ddac8da9a6a79e2fa"
integrity sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==
"@types/lodash@^4.14.180": "@types/lodash@^4.14.180":
version "4.14.180" version "4.14.180"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.180.tgz#4ab7c9ddfc92ec4a887886483bc14c79fb380670" resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.180.tgz#4ab7c9ddfc92ec4a887886483bc14c79fb380670"
@ -1722,6 +1734,11 @@ locate-path@^5.0.0:
dependencies: dependencies:
p-locate "^4.1.0" p-locate "^4.1.0"
lodash-es@^4.17.21:
version "4.17.21"
resolved "https://registry.npmmirror.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==
lodash@^4.17.21: lodash@^4.17.21:
version "4.17.21" version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"