feat: detect admin mode and warn about auto-start unavailability

This commit is contained in:
wonfen
2025-03-31 03:22:24 +08:00
parent b092f74c88
commit 52a15bb281
8 changed files with 103 additions and 17 deletions

View File

@@ -1,10 +1,10 @@
import { useTranslation } from "react-i18next";
import { Typography, Stack, Divider, Chip, IconButton } from "@mui/material";
import { InfoOutlined, SettingsOutlined } from "@mui/icons-material";
import { Typography, Stack, Divider, Chip, IconButton, Tooltip } from "@mui/material";
import { InfoOutlined, SettingsOutlined, WarningOutlined } from "@mui/icons-material";
import { useVerge } from "@/hooks/use-verge";
import { EnhancedCard } from "./enhanced-card";
import useSWR from "swr";
import { getRunningMode, getSystemInfo, installService } from "@/services/cmds";
import { getRunningMode, getSystemInfo, installService, isAdmin } from "@/services/cmds";
import { useNavigate } from "react-router-dom";
import { version as appVersion } from "@root/package.json";
import { useCallback, useEffect, useMemo, useState } from "react";
@@ -30,6 +30,13 @@ export const SystemInfoCard = () => {
{ suspense: false, revalidateOnFocus: false },
);
// 获取管理员状态
const { data: isAdminMode = false } = useSWR(
"isAdmin",
isAdmin,
{ suspense: false, revalidateOnFocus: false },
);
// 是否以sidecar模式运行
const isSidecarMode = runningMode === "Sidecar";
@@ -107,13 +114,13 @@ export const SystemInfoCard = () => {
// 切换自启动状态
const toggleAutoLaunch = useCallback(async () => {
if (!verge) return;
if (!verge || isAdminMode) return;
try {
await patchVerge({ enable_auto_launch: !verge.enable_auto_launch });
} catch (err) {
console.error("切换开机自启动状态失败:", err);
}
}, [verge, patchVerge]);
}, [verge, patchVerge, isAdminMode]);
// 安装系统服务
const onInstallService = useLockFn(async () => {
@@ -191,18 +198,26 @@ export const SystemInfoCard = () => {
</Typography>
</Stack>
<Divider />
<Stack direction="row" justifyContent="space-between">
<Stack direction="row" justifyContent="space-between" alignItems="center">
<Typography variant="body2" color="text.secondary">
{t("Auto Launch")}
</Typography>
<Chip
size="small"
label={autoLaunchEnabled ? t("Enabled") : t("Disabled")}
color={autoLaunchEnabled ? "success" : "default"}
variant={autoLaunchEnabled ? "filled" : "outlined"}
onClick={toggleAutoLaunch}
sx={{ cursor: "pointer" }}
/>
<Stack direction="row" spacing={1} alignItems="center">
{isAdminMode && (
<Tooltip title={t("Administrator mode does not support auto launch")}>
<WarningOutlined sx={{ color: "warning.main", fontSize: 20 }} />
</Tooltip>
)}
<Chip
size="small"
label={autoLaunchEnabled ? t("Enabled") : t("Disabled")}
color={autoLaunchEnabled ? "success" : "default"}
variant={autoLaunchEnabled ? "filled" : "outlined"}
onClick={toggleAutoLaunch}
disabled={isAdminMode}
sx={{ cursor: isAdminMode ? "not-allowed" : "pointer" }}
/>
</Stack>
</Stack>
<Divider />
<Stack direction="row" justifyContent="space-between">

View File

@@ -21,6 +21,7 @@ import {
getRunningMode,
installService,
getAutoLaunchStatus,
isAdmin,
} from "@/services/cmds";
import { useLockFn } from "ahooks";
import { Box, Button, Tooltip } from "@mui/material";
@@ -45,6 +46,11 @@ const SettingSystem = ({ onError }: Props) => {
getAutoLaunchStatus,
{ revalidateOnFocus: false }
);
const { data: isAdminMode = false } = useSWR(
"isAdmin",
isAdmin,
{ revalidateOnFocus: false }
);
// 当实际自启动状态与配置不同步时更新配置
useEffect(() => {
@@ -192,14 +198,32 @@ const SettingSystem = ({ onError }: Props) => {
</GuardState>
</SettingItem>
<SettingItem label={t("Auto Launch")}>
<SettingItem
label={t("Auto Launch")}
extra={
isAdminMode && (
<Tooltip title={t("Administrator mode does not support auto launch")}>
<WarningRounded sx={{ color: "warning.main", mr: 1 }} />
</Tooltip>
)
}
>
<GuardState
value={enable_auto_launch ?? false}
valueProps="checked"
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_auto_launch: e })}
onChange={(e) => {
// 在管理员模式下禁用更改
if (isAdminMode) return;
onChangeData({ enable_auto_launch: e });
}}
onGuard={async (e) => {
if (isAdminMode) {
Notice.error(t("Administrator mode does not support auto launch"), 2000);
return Promise.reject(new Error(t("Administrator mode does not support auto launch")));
}
try {
// 在应用更改之前先触发UI更新让用户立即看到反馈
onChangeData({ enable_auto_launch: e });
@@ -214,7 +238,7 @@ const SettingSystem = ({ onError }: Props) => {
}
}}
>
<Switch edge="end" />
<Switch edge="end" disabled={isAdminMode} />
</GuardState>
</SettingItem>