mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 04:43:44 +08:00
fix: after importing a subscription, it cannot be automatically switched to the current subscription.
This commit is contained in:
parent
983d1ea361
commit
a0b266fef8
@ -32,12 +32,8 @@ pub async fn enhance_profiles() -> CmdResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn import_profile(
|
pub async fn import_profile(url: String, option: Option<PrfOption>) -> CmdResult {
|
||||||
url: String,
|
let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
|
||||||
name: Option<String>,
|
|
||||||
option: Option<PrfOption>,
|
|
||||||
) -> CmdResult {
|
|
||||||
let item = wrap_err!(PrfItem::from_url(&url, name, None, option).await)?;
|
|
||||||
wrap_err!(Config::profiles().data().append_item(item))
|
wrap_err!(Config::profiles().data().append_item(item))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use crate::cmds::import_profile;
|
|
||||||
use crate::config::IVerge;
|
use crate::config::IVerge;
|
||||||
use crate::utils::error;
|
use crate::utils::error;
|
||||||
use crate::{config::Config, core::*, utils::init, utils::server};
|
use crate::{config::Config, config::PrfItem, core::*, utils::init, utils::server};
|
||||||
use crate::{error as er, log_err, trace_err};
|
use crate::{log_err, trace_err, wrap_err};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use percent_encoding::percent_decode_str;
|
use percent_encoding::percent_decode_str;
|
||||||
@ -265,13 +264,17 @@ pub async fn resolve_scheme(param: String) -> Result<()> {
|
|||||||
|
|
||||||
match encode_url {
|
match encode_url {
|
||||||
Some(url) => {
|
Some(url) => {
|
||||||
let decoded_url = percent_decode_str(url.as_ref()).decode_utf8_lossy();
|
let url = percent_decode_str(url.as_ref())
|
||||||
|
.decode_utf8_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
let handle = handle::Handle::global();
|
let handle = handle::Handle::global();
|
||||||
let app_handle = handle.app_handle.lock().clone();
|
let app_handle = handle.app_handle.lock().clone();
|
||||||
if let Some(app_handle) = app_handle.as_ref() {
|
if let Some(app_handle) = app_handle.as_ref() {
|
||||||
er!(format!("decode_url: {}", decoded_url));
|
match PrfItem::from_url(url.as_ref(), name, None, None).await {
|
||||||
match import_profile(decoded_url.to_string(), name.clone(), None).await {
|
Ok(item) => {
|
||||||
Ok(_) => {
|
let uid = item.uid.clone().unwrap();
|
||||||
|
let _ = wrap_err!(Config::profiles().data().append_item(item));
|
||||||
app_handle
|
app_handle
|
||||||
.notification()
|
.notification()
|
||||||
.builder()
|
.builder()
|
||||||
@ -279,7 +282,8 @@ pub async fn resolve_scheme(param: String) -> Result<()> {
|
|||||||
.body("Import profile success")
|
.body("Import profile success")
|
||||||
.show()
|
.show()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
handle::Handle::notice_message("import_sub_url::ok", "ok");
|
|
||||||
|
handle::Handle::notice_message("import_sub_url::ok", uid);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
app_handle
|
app_handle
|
||||||
@ -289,8 +293,8 @@ pub async fn resolve_scheme(param: String) -> Result<()> {
|
|||||||
.body(format!("Import profile failed: {e}"))
|
.body(format!("Import profile failed: {e}"))
|
||||||
.show()
|
.show()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
handle::Handle::notice_message("import_sub_url::error", e.clone());
|
handle::Handle::notice_message("import_sub_url::error", e.to_string());
|
||||||
bail!("Import profile failed: {e}");
|
bail!("Failed to add subscriptions: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import relativeTime from "dayjs/plugin/relativeTime";
|
|||||||
import { SWRConfig, mutate } from "swr";
|
import { SWRConfig, mutate } from "swr";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useLocation, useRoutes } from "react-router-dom";
|
import { useLocation, useRoutes, useNavigate } from "react-router-dom";
|
||||||
import { List, Paper, ThemeProvider, SvgIcon } from "@mui/material";
|
import { List, Paper, ThemeProvider, SvgIcon } from "@mui/material";
|
||||||
import { listen } from "@tauri-apps/api/event";
|
import { listen } from "@tauri-apps/api/event";
|
||||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||||
@ -25,7 +25,6 @@ import getSystem from "@/utils/get-system";
|
|||||||
import "dayjs/locale/ru";
|
import "dayjs/locale/ru";
|
||||||
import "dayjs/locale/zh-cn";
|
import "dayjs/locale/zh-cn";
|
||||||
import { getPortableFlag } from "@/services/cmds";
|
import { getPortableFlag } from "@/services/cmds";
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { TransitionGroup, CSSTransition } from "react-transition-group";
|
import { TransitionGroup, CSSTransition } from "react-transition-group";
|
||||||
const appWindow = getCurrentWebviewWindow();
|
const appWindow = getCurrentWebviewWindow();
|
||||||
@ -73,9 +72,12 @@ const Layout = () => {
|
|||||||
const [status, msg] = payload as [string, string];
|
const [status, msg] = payload as [string, string];
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case "import_sub_url::ok":
|
case "import_sub_url::ok":
|
||||||
|
navigate("/profile", { state: { current: msg } });
|
||||||
|
|
||||||
Notice.success(t("Import Subscription Successful"));
|
Notice.success(t("Import Subscription Successful"));
|
||||||
break;
|
break;
|
||||||
case "import_sub_url::error":
|
case "import_sub_url::error":
|
||||||
|
navigate("/profile");
|
||||||
Notice.error(msg);
|
Notice.error(msg);
|
||||||
break;
|
break;
|
||||||
case "set_config::ok":
|
case "set_config::ok":
|
||||||
|
@ -50,9 +50,11 @@ import { BaseStyledTextField } from "@/components/base/base-styled-text-field";
|
|||||||
import { listen } from "@tauri-apps/api/event";
|
import { listen } from "@tauri-apps/api/event";
|
||||||
import { readTextFile } from "@tauri-apps/plugin-fs";
|
import { readTextFile } from "@tauri-apps/plugin-fs";
|
||||||
import { readText } from "@tauri-apps/plugin-clipboard-manager";
|
import { readText } from "@tauri-apps/plugin-clipboard-manager";
|
||||||
|
import { useLocation } from "react-router-dom";
|
||||||
|
|
||||||
const ProfilePage = () => {
|
const ProfilePage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
const [url, setUrl] = useState("");
|
const [url, setUrl] = useState("");
|
||||||
const [disabled, setDisabled] = useState(false);
|
const [disabled, setDisabled] = useState(false);
|
||||||
@ -64,6 +66,7 @@ const ProfilePage = () => {
|
|||||||
coordinateGetter: sortableKeyboardCoordinates,
|
coordinateGetter: sortableKeyboardCoordinates,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
const { current } = location.state || {};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unlisten = listen("tauri://file-drop", async (event) => {
|
const unlisten = listen("tauri://file-drop", async (event) => {
|
||||||
@ -138,11 +141,11 @@ const ProfilePage = () => {
|
|||||||
|
|
||||||
const remoteItem = newProfiles.items?.find((e) => e.type === "remote");
|
const remoteItem = newProfiles.items?.find((e) => e.type === "remote");
|
||||||
|
|
||||||
const profilesCount = newProfiles.items?.filter(
|
const itemsCount = newProfiles.items?.filter(
|
||||||
(e) => e.type === "remote" || e.type === "local"
|
(e) => e.type === "remote" || e.type === "local"
|
||||||
).length as number;
|
).length as number;
|
||||||
|
|
||||||
if (remoteItem && (profilesCount == 1 || !newProfiles.current)) {
|
if (remoteItem && (itemsCount == 1 || !newProfiles.current)) {
|
||||||
const current = remoteItem.uid;
|
const current = remoteItem.uid;
|
||||||
await patchProfiles({ current });
|
await patchProfiles({ current });
|
||||||
mutateLogs();
|
mutateLogs();
|
||||||
@ -168,27 +171,40 @@ const ProfilePage = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSelect = useLockFn(async (current: string, force: boolean) => {
|
const activateProfile = async (profile: string) => {
|
||||||
if (!force && current === profiles.current) return;
|
|
||||||
// 避免大多数情况下loading态闪烁
|
// 避免大多数情况下loading态闪烁
|
||||||
|
|
||||||
const reset = setTimeout(() => {
|
const reset = setTimeout(() => {
|
||||||
setActivatings([...currentActivatings(), current]);
|
setActivatings((prev) => [...prev, profile]);
|
||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await patchProfiles({ current });
|
await patchProfiles({ current: profile });
|
||||||
await mutateLogs();
|
await mutateLogs();
|
||||||
closeAllConnections();
|
closeAllConnections();
|
||||||
activateSelected().then(() => {
|
await activateSelected();
|
||||||
Notice.success(t("Profile Switched"), 1000);
|
Notice.success(t("Profile Switched"), 1000);
|
||||||
});
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
Notice.error(err?.message || err.toString(), 4000);
|
Notice.error(err?.message || err.toString(), 4000);
|
||||||
} finally {
|
} finally {
|
||||||
clearTimeout(reset);
|
clearTimeout(reset);
|
||||||
setActivatings([]);
|
setActivatings([]);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
const onSelect = useLockFn(async (current: string, force: boolean) => {
|
||||||
|
if (!force && current === profiles.current) return;
|
||||||
|
await activateProfile(current);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
if (current && current !== profiles.current) {
|
||||||
|
console.log("current:", current);
|
||||||
|
await activateProfile(current);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, current);
|
||||||
|
|
||||||
const onEnhance = useLockFn(async () => {
|
const onEnhance = useLockFn(async () => {
|
||||||
setActivatings(currentActivatings());
|
setActivatings(currentActivatings());
|
||||||
try {
|
try {
|
||||||
|
@ -63,7 +63,6 @@ export async function saveProfileFile(index: string, fileData: string) {
|
|||||||
export async function importProfile(url: string) {
|
export async function importProfile(url: string) {
|
||||||
return invoke<void>("import_profile", {
|
return invoke<void>("import_profile", {
|
||||||
url,
|
url,
|
||||||
name: null,
|
|
||||||
option: { with_proxy: true },
|
option: { with_proxy: true },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user