import useSWR from "swr"; import { forwardRef, useImperativeHandle, useState } from "react"; import { useLockFn } from "ahooks"; import { useTranslation } from "react-i18next"; import { Button, Stack, Typography } from "@mui/material"; import { checkService, installService, uninstallService, patchVergeConfig, } from "@/services/cmds"; import { BaseDialog, DialogRef, Notice } from "@/components/base"; interface Props { enable: boolean; } export const ServiceViewer = forwardRef((props, ref) => { const { enable } = props; const { t } = useTranslation(); const [open, setOpen] = useState(false); const { data: status, mutate: mutateCheck } = useSWR( "checkService", checkService, { revalidateIfStale: false, shouldRetryOnError: false } ); useImperativeHandle(ref, () => ({ open: () => setOpen(true), close: () => setOpen(false), })); const state = status != null ? status : "pending"; const onInstall = useLockFn(async () => { try { await installService(); mutateCheck(); setOpen(false); Notice.success("Service installed successfully"); } catch (err: any) { mutateCheck(); Notice.error(err.message || err.toString()); } }); const onUninstall = useLockFn(async () => { try { if (state === "active" && enable) { await patchVergeConfig({ enable_service_mode: false }); } await uninstallService(); mutateCheck(); setOpen(false); Notice.success("Service uninstalled successfully"); } catch (err: any) { mutateCheck(); Notice.error(err.message || err.toString()); } }); // fix unhandled error of the service mode const onDisable = useLockFn(async () => { await patchVergeConfig({ enable_service_mode: false }); mutateCheck(); setOpen(false); }); return ( setOpen(false)} > Current State: {state} {(state === "unknown" || state === "uninstall") && ( Information: Please make sure the Clash Verge Service is installed and enabled )} {state === "uninstall" && enable && ( )} {state === "uninstall" && ( )} {(state === "active" || state === "installed") && ( )} ); });