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

@@ -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>