service-switcher.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { KeyedMutator } from "swr";
  2. import { useState } from "react";
  3. import { useLockFn } from "ahooks";
  4. import { useTranslation } from "react-i18next";
  5. import { installService, uninstallService } from "@/services/cmds";
  6. import { Notice } from "@/components/base";
  7. import { LoadingButton } from "@mui/lab";
  8. import { PasswordInput } from "./password-input";
  9. import getSystem from "@/utils/get-system";
  10. interface Props {
  11. status: "active" | "installed" | "unknown" | "uninstall";
  12. mutate: KeyedMutator<"active" | "installed" | "unknown" | "uninstall">;
  13. patchVerge: (value: Partial<IVergeConfig>) => Promise<void>;
  14. onChangeData: (patch: Partial<IVergeConfig>) => void;
  15. }
  16. export const ServiceSwitcher = (props: Props) => {
  17. const { status, mutate, patchVerge, onChangeData } = props;
  18. const isWindows = getSystem() === "windows";
  19. const isActive = status === "active";
  20. const isInstalled = status === "installed";
  21. const isUninstall = status === "uninstall" || status === "unknown";
  22. const { t } = useTranslation();
  23. const [serviceLoading, setServiceLoading] = useState(false);
  24. const [uninstallServiceLoaing, setUninstallServiceLoading] = useState(false);
  25. const [openInstall, setOpenInstall] = useState(false);
  26. const [openUninstall, setOpenUninstall] = useState(false);
  27. async function install(passwd: string) {
  28. try {
  29. setOpenInstall(false);
  30. await installService(passwd);
  31. await mutate();
  32. setTimeout(() => {
  33. mutate();
  34. }, 2000);
  35. Notice.success(t("Service Installed Successfully"));
  36. setServiceLoading(false);
  37. } catch (err: any) {
  38. await mutate();
  39. setTimeout(() => {
  40. mutate();
  41. }, 2000);
  42. Notice.error(err.message || err.toString());
  43. setServiceLoading(false);
  44. }
  45. }
  46. async function uninstall(passwd: string) {
  47. try {
  48. setOpenUninstall(false);
  49. await uninstallService(passwd);
  50. await mutate();
  51. setTimeout(() => {
  52. mutate();
  53. }, 2000);
  54. Notice.success(t("Service Uninstalled Successfully"));
  55. setUninstallServiceLoading(false);
  56. } catch (err: any) {
  57. await mutate();
  58. setTimeout(() => {
  59. mutate();
  60. }, 2000);
  61. Notice.error(err.message || err.toString());
  62. setUninstallServiceLoading(false);
  63. }
  64. }
  65. const onInstallOrEnableService = useLockFn(async () => {
  66. setServiceLoading(true);
  67. if (isUninstall) {
  68. // install service
  69. if (isWindows) {
  70. await install("");
  71. } else {
  72. setOpenInstall(true);
  73. }
  74. } else {
  75. try {
  76. // enable or disable service
  77. await patchVerge({ enable_service_mode: !isActive });
  78. onChangeData({ enable_service_mode: !isActive });
  79. await mutate();
  80. setTimeout(() => {
  81. mutate();
  82. }, 2000);
  83. setServiceLoading(false);
  84. } catch (err: any) {
  85. await mutate();
  86. Notice.error(err.message || err.toString());
  87. setServiceLoading(false);
  88. }
  89. }
  90. });
  91. const onUninstallService = useLockFn(async () => {
  92. setUninstallServiceLoading(true);
  93. if (isWindows) {
  94. await uninstall("");
  95. } else {
  96. setOpenUninstall(true);
  97. }
  98. });
  99. return (
  100. <>
  101. {openInstall && <PasswordInput onConfirm={install} />}
  102. {openUninstall && <PasswordInput onConfirm={uninstall} />}
  103. <LoadingButton
  104. size="small"
  105. variant={isUninstall ? "outlined" : "contained"}
  106. onClick={onInstallOrEnableService}
  107. loading={serviceLoading}
  108. >
  109. {isActive ? t("Disable") : isInstalled ? t("Enable") : t("Install")}
  110. </LoadingButton>
  111. {isInstalled && (
  112. <LoadingButton
  113. size="small"
  114. variant="outlined"
  115. color="error"
  116. sx={{ ml: 1 }}
  117. onClick={onUninstallService}
  118. loading={uninstallServiceLoaing}
  119. >
  120. {t("Uninstall")}
  121. </LoadingButton>
  122. )}
  123. </>
  124. );
  125. };