Kaynağa Gözat

fix: restart clash should update something

GyDi 3 yıl önce
ebeveyn
işleme
4b5b62c8ae
3 değiştirilmiş dosya ile 18 ekleme ve 3 silme
  1. 2 0
      src-tauri/src/main.rs
  2. 13 1
      src/pages/_layout.tsx
  3. 3 2
      src/services/api.ts

+ 2 - 0
src-tauri/src/main.rs

@@ -49,6 +49,8 @@ fn main() -> std::io::Result<()> {
               if let Err(err) = profiles.activate(clash.info.clone()) {
                 log::error!("{}", err);
               }
+              let window = app_handle.get_window("main").unwrap();
+              window.emit("restart_clash", "yes").unwrap();
             }
             Err(err) => log::error!("{}", err),
           }

+ 13 - 1
src/pages/_layout.tsx

@@ -1,11 +1,13 @@
-import useSWR, { SWRConfig } from "swr";
+import useSWR, { SWRConfig, useSWRConfig } from "swr";
 import { useEffect, useMemo } from "react";
 import { Route, Routes } from "react-router-dom";
 import { useRecoilState } from "recoil";
 import { alpha, createTheme, List, Paper, ThemeProvider } from "@mui/material";
+import { listen } from "@tauri-apps/api/event";
 import { appWindow } from "@tauri-apps/api/window";
 import { atomPaletteMode, atomThemeBlur } from "../states/setting";
 import { getVergeConfig } from "../services/cmds";
+import { getAxios } from "../services/api";
 import { routers } from "./_routers";
 import LogoSvg from "../assets/image/logo.svg";
 import Traffic from "../components/traffic";
@@ -14,6 +16,7 @@ import UpdateButton from "../components/update-button";
 import LayoutControl from "../components/layout-control";
 
 const Layout = () => {
+  const { mutate } = useSWRConfig();
   const [mode, setMode] = useRecoilState(atomPaletteMode);
   const [blur, setBlur] = useRecoilState(atomThemeBlur);
   const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig);
@@ -22,6 +25,15 @@ const Layout = () => {
     window.addEventListener("keydown", (e) => {
       if (e.key === "Escape") appWindow.hide();
     });
+
+    listen("restart_clash", async () => {
+      // the clash info may be updated
+      await getAxios(true);
+      // make sure that the clash is ok
+      setTimeout(() => mutate("getProxies"), 1000);
+      setTimeout(() => mutate("getProxies"), 2000);
+      mutate("getClashConfig");
+    });
   }, []);
 
   useEffect(() => {

+ 3 - 2
src/services/api.ts

@@ -7,8 +7,9 @@ let server = "";
 let secret = "";
 
 /// initialize some infomation
-export async function getAxios() {
-  if (axiosIns) return axiosIns;
+/// enable force update axiosIns
+export async function getAxios(force: boolean = false) {
+  if (axiosIns && !force) return axiosIns;
 
   try {
     const info = await getClashInfo();