feat(labels): add state to track when labels are being fetched(in progress)

This commit is contained in:
ryan 2025-03-31 17:36:46 +03:00
parent ef7245b716
commit 40bb35d6a8

View file

@ -143,6 +143,10 @@ pub fn ItemsList(
// State to manage property cache
let (property_cache, set_property_cache) = create_signal(HashMap::<String, HashMap<String, String>>::new());
// State to track when labels are being fetched
let (is_fetching_labels, set_is_fetching_labels) = create_signal(false);
#[cfg(feature = "ssr")]
fn get_current_url() -> String {
use leptos::use_context;
@ -206,7 +210,11 @@ pub fn ItemsList(
// Fetch labels for the custom properties
let property_ids = custom_props_clone;
let labels = fetch_property_labels(property_ids).await;
let labels = fetch_property_labels(
property_ids,
set_property_labels.clone(),
move |value| set_is_fetching_labels.set(value),
).await;
set_property_labels.update(|labels_map| {
for (key, value) in labels {
labels_map.insert(key, value);
@ -445,7 +453,11 @@ pub fn ItemsList(
.collect();
if !missing_ids.is_empty() {
let new_labels = fetch_property_labels(missing_ids).await;
let new_labels = fetch_property_labels(
missing_ids,
set_property_labels.clone(),
move |value| { set_is_fetching_labels.set(value) },
).await;
set_property_labels.update(|labels| {
labels.extend(new_labels.clone());
});
@ -456,7 +468,7 @@ pub fn ItemsList(
for binding in bindings {
if let (Some(prop), Some(value_label)) = (
binding["propLabel"]["value"].as_str(),
binding["valueLabel"]["value"].as_str()
binding["valueLabel"]["value"].as_str(),
) {
let prop_id = prop.replace("http://www.wikidata.org/prop/", "");
if let Some(label) = property_labels.get().get(&prop_id).cloned() {
@ -480,7 +492,12 @@ pub fn ItemsList(
}
}
async fn fetch_property_labels(property_ids: Vec<String>) -> HashMap<String, String> {
async fn fetch_property_labels(
property_ids: Vec<String>,
set_property_labels: WriteSignal<HashMap<String, String>>,
set_is_fetching_labels: impl Fn(bool) + 'static,
) -> HashMap<String, String> {
set_is_fetching_labels(true);
log!("Fetching property labels for properties: {:?}", property_ids);
// Remove the "http://www.wikidata.org/prop/" prefix from property IDs
@ -532,7 +549,8 @@ pub fn ItemsList(
binding["prop"]["value"].as_str(),
binding["propLabel"]["value"].as_str()
) {
let prop_id = prop.split('/').last().unwrap_or("").to_string();
let prop_id =
prop.split('/').last().unwrap_or("").to_string();
result.insert(prop_id.clone(), label.to_string());
log!("Processed binding {}: prop_id = {}, label = {}", i, prop_id, label);
} else {
@ -543,6 +561,7 @@ pub fn ItemsList(
log!("Warning: No bindings found in the response");
}
log!("Fetched {} property labels", result.len());
set_is_fetching_labels(false);
result
}
Err(e) => {
@ -582,7 +601,12 @@ pub fn ItemsList(
let normalized_property = normalized_property.clone();
let set_property_labels = set_property_labels.clone();
async move {
let labels = fetch_property_labels(vec![normalized_property.clone()]).await;
let labels = fetch_property_labels(
vec![normalized_property.clone()],
set_property_labels.clone(),
move |value| set_is_fetching_labels.set(value),
)
.await;
set_property_labels.update(|map| {
map.extend(labels);
});
@ -666,7 +690,14 @@ pub fn ItemsList(
let set_property_labels = set_property_labels.clone();
let property_clone = property.clone();
spawn_local(async move {
let properties = fetch_item_properties(&wikidata_id, set_property_labels.clone(), property_cache.clone(), set_property_cache.clone(), property_labels.clone()).await;
let properties = fetch_item_properties(
&wikidata_id,
set_property_labels.clone(),
property_cache.clone(),
set_property_cache.clone(),
property_labels.clone(),
)
.await;
// Update fetched properties and property labels
set_fetched_properties.update(|fp| {
fp.insert(wikidata_id.clone(), properties.clone());
@ -687,7 +718,8 @@ pub fn ItemsList(
}
}
});
})};
})
};
// Update item fields
let update_item = {
@ -708,7 +740,14 @@ pub fn ItemsList(
if let Some(wikidata_id) = &item.wikidata_id {
let wikidata_id = wikidata_id.clone();
spawn_local(async move {
let properties = fetch_item_properties(&wikidata_id, set_property_labels.clone(), property_cache.clone(), set_property_cache.clone(), property_labels.clone()).await;
let properties = fetch_item_properties(
&wikidata_id,
set_property_labels.clone(),
property_cache.clone(),
set_property_cache.clone(),
property_labels.clone(),
)
.await;
log!("Fetched properties for index {}: {:?}", index, properties);
});
}
@ -754,7 +793,8 @@ pub fn ItemsList(
}
log!("Items updated: {:?}", items);
});
})};
})
};
// List of properties to display as rows
let properties = vec!["Name", "Description"];
@ -933,10 +973,20 @@ pub fn ItemsList(
let property_label = property_labels.get().get(&normalized_property).cloned().unwrap_or_else(|| normalized_property.clone());
log!("Rendering property: {} -> {}", normalized_property, property_label);
let property_clone_for_button = normalized_property.clone();
let normalized_property_clone = normalized_property.clone();
view! {
<tr>
<td>
{ property_label }
{move || {
let property_label = property_labels.get().get(&normalized_property).cloned();
let is_fetching = is_fetching_labels.get();
if is_fetching {
normalized_property.clone()
} else {
property_label.unwrap_or_else(|| normalized_property.clone())
}
}}
<button class="delete-property" on:click=move |_| {
log!("Deleting property: {}", property_clone_for_button);
remove_property_clone(property_clone_for_button.clone());
@ -953,9 +1003,11 @@ pub fn ItemsList(
});
}>{ "Delete" }</button>
</td>
let normalized_property = property.replace("http://www.wikidata.org/prop/", "");
let normalized_property_clone = normalized_property.clone();
{move || {
let update_item_cell = Arc::clone(&update_item_inner);
let property_clone_for_cells = normalized_property.clone();
let property_clone_for_cells = normalized_property_clone.clone();
items.get().iter().enumerate().map(move |(index, item)| {
let update_item_cell = Arc::clone(&update_item_cell);
let property_clone_for_closure = property_clone_for_cells.clone();