use iocraft::prelude::*;
use crate::api::CloudflareClient;
use crate::ui::state::AppState;
use crate::utils::{extract_unique_ips, format_records};
pub async fn fetch_all(
client: &CloudflareClient,
state: &AppState,
rd: &mut State<String>,
st: &mut State<String>,
) {
match client.get_zone_name().await {
Ok(name) => {
*state.zone_name.lock().unwrap() = name;
}
Err(_) => {
}
}
{
let cache = state.dns_cache.lock().unwrap();
if let Some(cached_records) = cache.get() {
let cached_records = cached_records.clone();
rd.set(format_records(&cached_records));
st.set(format!(
"Loaded {} DNS records (cached)",
cached_records.len()
));
*state.existing_ips.lock().unwrap() = extract_unique_ips(&cached_records);
*state.records.lock().unwrap() = cached_records;
return;
}
}
match client.list_dns_records().await {
Ok(f) => {
rd.set(format_records(&f));
st.set(format!("Loaded {} DNS records", f.len()));
*state.existing_ips.lock().unwrap() = extract_unique_ips(&f);
*state.records.lock().unwrap() = f.clone();
state.dns_cache.lock().unwrap().set(f);
}
Err(e) => {
rd.set(format!("Error: {}", e));
st.set(format!("Error: {}", e));
}
}
}
pub async fn refresh_task(
client: &CloudflareClient,
state: &AppState,
mut rd: State<String>,
mut st: State<String>,
) {
state.dns_cache.lock().unwrap().invalidate();
match client.list_dns_records().await {
Ok(f) => {
rd.set(format_records(&f));
st.set(format!("Loaded {} DNS records", f.len()));
*state.existing_ips.lock().unwrap() = extract_unique_ips(&f);
*state.records.lock().unwrap() = f.clone();
state.dns_cache.lock().unwrap().set(f);
}
Err(e) => st.set(format!("Error: {}", e)),
}
}