From 4b5b62c8aee383a96607fc763cacfb7022de748b Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Fri, 21 Jan 2022 00:29:33 +0800
Subject: [PATCH] fix: restart clash should update something

---
 src-tauri/src/main.rs |  2 ++
 src/pages/_layout.tsx | 14 +++++++++++++-
 src/services/api.ts   |  5 +++--
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 7097d52..4953f1e 100644
--- a/src-tauri/src/main.rs
+++ b/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),
           }
diff --git a/src/pages/_layout.tsx b/src/pages/_layout.tsx
index ed9b2e7..f787754 100644
--- a/src/pages/_layout.tsx
+++ b/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(() => {
diff --git a/src/services/api.ts b/src/services/api.ts
index 8e0273f..ad034cc 100644
--- a/src/services/api.ts
+++ b/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();
-- 
GitLab