feat: add state management for core switching, upgrading and restart functionality

This commit is contained in:
wonfen
2025-05-18 18:37:09 +08:00
parent 1d2fd06507
commit cc5ebec0cb
2 changed files with 24 additions and 5 deletions

View File

@@ -48,6 +48,7 @@
- 卸载服务的按钮 - 卸载服务的按钮
- 添加了Zashboard的一键跳转URL - 添加了Zashboard的一键跳转URL
- 使用操作系统默认的窗口管理器 - 使用操作系统默认的窗口管理器
- 切换、升级、重启内核的状态管理
#### 优化了: #### 优化了:
- 系统代理 Bypass 设置 - 系统代理 Bypass 设置

View File

@@ -11,8 +11,8 @@ import {
} from "@mui/icons-material"; } from "@mui/icons-material";
import { import {
Box, Box,
Button,
Chip, Chip,
CircularProgress,
List, List,
ListItemButton, ListItemButton,
ListItemText, ListItemText,
@@ -33,6 +33,8 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [upgrading, setUpgrading] = useState(false); const [upgrading, setUpgrading] = useState(false);
const [restarting, setRestarting] = useState(false);
const [changingCore, setChangingCore] = useState<string | null>(null);
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
open: () => setOpen(true), open: () => setOpen(true),
@@ -45,11 +47,13 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
if (core === clash_core) return; if (core === clash_core) return;
try { try {
setChangingCore(core);
closeAllConnections(); closeAllConnections();
const errorMsg = await changeClashCore(core); const errorMsg = await changeClashCore(core);
if (errorMsg) { if (errorMsg) {
showNotice('error', errorMsg); showNotice('error', errorMsg);
setChangingCore(null);
return; return;
} }
@@ -57,17 +61,22 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
setTimeout(() => { setTimeout(() => {
mutate("getClashConfig"); mutate("getClashConfig");
mutate("getVersion"); mutate("getVersion");
setChangingCore(null);
}, 500); }, 500);
} catch (err: any) { } catch (err: any) {
setChangingCore(null);
showNotice('error', err.message || err.toString()); showNotice('error', err.message || err.toString());
} }
}); });
const onRestart = useLockFn(async () => { const onRestart = useLockFn(async () => {
try { try {
setRestarting(true);
await restartCore(); await restartCore();
showNotice('success', t(`Clash Core Restarted`)); showNotice('success', t(`Clash Core Restarted`));
setRestarting(false);
} catch (err: any) { } catch (err: any) {
setRestarting(false);
showNotice('error', err.message || err.toString()); showNotice('error', err.message || err.toString());
} }
}); });
@@ -97,19 +106,23 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
startIcon={<SwitchAccessShortcutRounded />} startIcon={<SwitchAccessShortcutRounded />}
loadingPosition="start" loadingPosition="start"
loading={upgrading} loading={upgrading}
disabled={restarting || changingCore !== null}
sx={{ marginRight: "8px" }} sx={{ marginRight: "8px" }}
onClick={onUpgrade} onClick={onUpgrade}
> >
{t("Upgrade")} {t("Upgrade")}
</LoadingButton> </LoadingButton>
<Button <LoadingButton
variant="contained" variant="contained"
size="small" size="small"
onClick={onRestart}
startIcon={<RestartAltRounded />} startIcon={<RestartAltRounded />}
loadingPosition="start"
loading={restarting}
disabled={upgrading}
onClick={onRestart}
> >
{t("Restart")} {t("Restart")}
</Button> </LoadingButton>
</Box> </Box>
</Box> </Box>
} }
@@ -132,9 +145,14 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
key={each.core} key={each.core}
selected={each.core === clash_core} selected={each.core === clash_core}
onClick={() => onCoreChange(each.core)} onClick={() => onCoreChange(each.core)}
disabled={changingCore !== null || restarting || upgrading}
> >
<ListItemText primary={each.name} secondary={`/${each.core}`} /> <ListItemText primary={each.name} secondary={`/${each.core}`} />
<Chip label={t(`${each.chip}`)} size="small" /> {changingCore === each.core ? (
<CircularProgress size={20} sx={{ mr: 1 }} />
) : (
<Chip label={t(`${each.chip}`)} size="small" />
)}
</ListItemButton> </ListItemButton>
))} ))}
</List> </List>