Fix delay display and prevent refresh from resetting UI
- Store full proxies map (not just groups) so delay lookups find individual proxy nodes correctly - Display delay as plain number with color coding (green/yellow/red) instead of wrapped in parentheses - Separate initial load from background refresh: periodic refresh silently updates data without flashing "Loading..." state Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
75c644555b
commit
4327294e3c
@ -1,13 +1,15 @@
|
||||
use iocraft::prelude::*;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::clash::ProxyItem;
|
||||
|
||||
enum DataState {
|
||||
Init,
|
||||
Loading,
|
||||
Loaded(Vec<ProxyItem>),
|
||||
Loaded {
|
||||
groups: Vec<ProxyItem>,
|
||||
all_proxies: HashMap<String, ProxyItem>,
|
||||
},
|
||||
Error(String),
|
||||
}
|
||||
|
||||
@ -38,19 +40,20 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
let test_delay_url = ctx.test_delay_url.clone();
|
||||
let test_delay_timeout = ctx.test_delay_timeout;
|
||||
|
||||
let load = hooks.use_async_handler(move |_: ()| {
|
||||
// Initial load: shows "Loading..." state
|
||||
let load_init = hooks.use_async_handler(move |_: ()| {
|
||||
let client = client.clone();
|
||||
async move {
|
||||
state.set(DataState::Loading);
|
||||
match client.get_proxies().await {
|
||||
Ok(resp) => {
|
||||
let mut groups: Vec<ProxyItem> = resp
|
||||
.proxies
|
||||
.into_values()
|
||||
let all_proxies = resp.proxies;
|
||||
let mut groups: Vec<ProxyItem> = all_proxies
|
||||
.values()
|
||||
.filter(|p| p.is_group())
|
||||
.cloned()
|
||||
.collect();
|
||||
groups.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
state.set(DataState::Loaded(groups));
|
||||
state.set(DataState::Loaded { groups, all_proxies });
|
||||
}
|
||||
Err(e) => {
|
||||
state.set(DataState::Error(e.to_string()));
|
||||
@ -59,15 +62,38 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
}
|
||||
});
|
||||
|
||||
// Background refresh: silently updates data without resetting UI
|
||||
let client_refresh = ctx.client.clone();
|
||||
let load_refresh = hooks.use_async_handler(move |_: ()| {
|
||||
let client = client_refresh.clone();
|
||||
async move {
|
||||
match client.get_proxies().await {
|
||||
Ok(resp) => {
|
||||
let all_proxies = resp.proxies;
|
||||
let mut groups: Vec<ProxyItem> = all_proxies
|
||||
.values()
|
||||
.filter(|p| p.is_group())
|
||||
.cloned()
|
||||
.collect();
|
||||
groups.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
state.set(DataState::Loaded { groups, all_proxies });
|
||||
}
|
||||
Err(_) => {
|
||||
// Silently ignore background refresh errors
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if matches!(*state.read(), DataState::Init) {
|
||||
load(());
|
||||
load_init(());
|
||||
}
|
||||
|
||||
let load_refresh = load.clone();
|
||||
let refresh_timer = load_refresh.clone();
|
||||
hooks.use_future(async move {
|
||||
loop {
|
||||
smol::Timer::after(Duration::from_secs(refresh_interval)).await;
|
||||
load_refresh(());
|
||||
refresh_timer(());
|
||||
}
|
||||
});
|
||||
|
||||
@ -84,14 +110,16 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
});
|
||||
|
||||
let client_for_select = ctx.client.clone();
|
||||
let load_select = load.clone();
|
||||
let load_select = load_refresh.clone();
|
||||
let client_for_delay = ctx.client.clone();
|
||||
let load_after_delay = load.clone();
|
||||
let load_after_delay = load_refresh.clone();
|
||||
let testing_state = testing;
|
||||
hooks.use_terminal_events(move |event| {
|
||||
if let TerminalEvent::Key(key_event) = event {
|
||||
let state_read = state.read();
|
||||
if let DataState::Loaded(groups) = &*state_read {
|
||||
let DataState::Loaded { groups, .. } = &*state_read else {
|
||||
return;
|
||||
};
|
||||
let group_count = groups.len();
|
||||
if group_count == 0 {
|
||||
return;
|
||||
@ -143,7 +171,6 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
}
|
||||
let names: Vec<String> = members.to_vec();
|
||||
smol::spawn(async move {
|
||||
// Test all concurrently
|
||||
let mut tasks = Vec::new();
|
||||
for name in &names {
|
||||
let c = client.clone();
|
||||
@ -213,7 +240,6 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let body_height = if height > 4 { height - 3 } else { 1 };
|
||||
@ -225,7 +251,7 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
|
||||
let state_read = state.read();
|
||||
match &*state_read {
|
||||
DataState::Init | DataState::Loading => {
|
||||
DataState::Init => {
|
||||
element! {
|
||||
View(
|
||||
width: 100pct,
|
||||
@ -249,7 +275,7 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
DataState::Loaded(groups) => {
|
||||
DataState::Loaded { groups, all_proxies } => {
|
||||
let current_group_idx = selected_group.get().min(groups.len().saturating_sub(1));
|
||||
let current_group = &groups[current_group_idx];
|
||||
let members = current_group.all.as_deref().unwrap_or(&[]);
|
||||
@ -344,13 +370,13 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
|
||||
let is_testing = testing_read.contains(member.as_str());
|
||||
|
||||
// Look up delay from the full proxies map (not just groups)
|
||||
let (delay_text, delay_color) = if is_testing {
|
||||
(format!("{} testing", SPINNER_FRAMES[frame]), Color::Yellow)
|
||||
(SPINNER_FRAMES[frame].to_string(), Color::Yellow)
|
||||
} else {
|
||||
let delay = groups
|
||||
.iter()
|
||||
.find(|g| g.name == *member)
|
||||
.and_then(|g| g.latest_delay());
|
||||
let delay = all_proxies
|
||||
.get(member.as_str())
|
||||
.and_then(|p| p.latest_delay());
|
||||
match delay {
|
||||
Some(0) => ("timeout".to_string(), Color::Red),
|
||||
Some(d) => (format!("{}ms", d), delay_color_for(d)),
|
||||
@ -365,7 +391,11 @@ pub fn ProxyView(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
|
||||
padding_left: 1,
|
||||
) {
|
||||
Text(content: format!("{}{} ", marker, member), color: if is_current { Color::Green } else if is_selected { Color::White } else { Color::Grey })
|
||||
Text(content: format!("({})", delay_text), color: delay_color)
|
||||
#(if delay_text.is_empty() {
|
||||
element! { Fragment }.into_any()
|
||||
} else {
|
||||
element! { Text(content: format!(" {} ", delay_text), color: delay_color) }.into_any()
|
||||
})
|
||||
#(if is_current {
|
||||
element! { Text(content: " <--", color: Color::Green) }.into_any()
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user