Skip to content

Commit

Permalink
feat: optimize config feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi authored Sep 26, 2022
1 parent 1b336d9 commit e30ba07
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src-tauri/src/core/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ impl Handle {
}
}

pub fn notice_message(&self, status: String, msg: String) {
if let Some(window) = self.get_window() {
log_if_err!(window.emit("verge://notice-message", (status, msg)));
}
}

// update system tray state (clash config)
pub fn update_systray_clash(&self) -> Result<()> {
if self.app_handle.is_none() {
Expand Down
9 changes: 7 additions & 2 deletions src-tauri/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,14 @@ impl Core {
match Service::set_config(clash_info, config).await {
Ok(_) => {
let handle = handle.lock();
handle.refresh_clash()
handle.refresh_clash();
handle.notice_message("set_config::ok".into(), "ok".into());
}
Err(err) => {
let handle = handle.lock();
handle.notice_message("set_config::error".into(), format!("{err}"));
log::error!(target: "app", "last {err}")
}
Err(err) => log::error!(target: "app", "{err}"),
}
});

Expand Down
19 changes: 13 additions & 6 deletions src-tauri/src/core/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,17 @@ impl Service {
let mut data = HashMap::new();
data.insert("path", temp_path.as_os_str().to_str().unwrap());

macro_rules! report_err {
($i: expr, $e: expr) => {
match $i {
4 => bail!($e),
_ => log::error!(target: "app", $e),
}
};
}

// retry 5 times
for _ in 0..5 {
for i in 0..5 {
let headers = headers.clone();
match reqwest::ClientBuilder::new().no_proxy().build() {
Ok(client) => {
Expand All @@ -223,14 +232,12 @@ impl Service {
204 => break,
// 配置有问题不重试
400 => bail!("failed to update clash config with status 400"),
status @ _ => {
log::error!(target: "app", "failed to activate clash with status \"{status}\"");
}
status @ _ => report_err!(i, "failed to activate clash with status \"{status}\""),
},
Err(err) => log::error!(target: "app", "{err}"),
Err(err) => report_err!(i, "{err}"),
}
}
Err(err) => log::error!(target: "app", "{err}"),
Err(err) => report_err!(i, "{err}"),
}
sleep(Duration::from_millis(500)).await;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/profile/enhanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const EnhancedMode = (props: Props) => {
try {
await enhanceProfiles();
mutateLogs();
Notice.success("Refresh clash config", 1000);
// Notice.success("Refresh clash config", 1000);
} catch (err: any) {
Notice.error(err.message || err.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/setting/mods/clash-field-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const ClashFieldViewer = ({ handler }: Props) => {
try {
await changeProfileValid([...curSet]);
mutateProfile();
Notice.success("Refresh clash config", 1000);
// Notice.success("Refresh clash config", 1000);
} catch (err: any) {
Notice.error(err?.message || err.toString());
}
Expand Down
16 changes: 16 additions & 0 deletions src/pages/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getAxios } from "@/services/api";
import { atomCurrentProfile } from "@/services/states";
import { getVergeConfig, getProfiles } from "@/services/cmds";
import { ReactComponent as LogoSvg } from "@/assets/image/logo.svg";
import Notice from "@/components/base/base-notice";
import LayoutItem from "@/components/layout/layout-item";
import LayoutControl from "@/components/layout/layout-control";
import LayoutTraffic from "@/components/layout/layout-traffic";
Expand Down Expand Up @@ -59,6 +60,21 @@ const Layout = () => {
// update the verge config
listen("verge://refresh-verge-config", () => mutate("getVergeConfig"));

// 设置提示监听
listen("verge://notice-message", ({ payload }) => {
const [status, msg] = payload as [string, string];
switch (status) {
case "set_config::ok":
Notice.success("Refresh clash config");
break;
case "set_config::error":
Notice.error(msg);
break;
default:
break;
}
});

// set current profile uid
getProfiles().then((data) => setCurrentProfile(data.current ?? ""));
}, []);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const ProfilePage = () => {
setCurrentProfile(uid);
mutate("getProfiles", { ...profiles, current: uid }, true);
mutate("getRuntimeLogs");
if (force) Notice.success("Refresh clash config", 1000);
// if (force) Notice.success("Refresh clash config", 1000);
} catch (err: any) {
Notice.error(err?.message || err.toString());
}
Expand Down

0 comments on commit e30ba07

Please sign in to comment.