Simplify memos

This commit is contained in:
Aaron Fenyes 2024-11-01 04:43:30 -07:00
parent ce9b114dd6
commit ec1911b889

View File

@ -43,13 +43,9 @@ fn ConstraintOutlineItem(constraint_key: ConstraintKey, element_key: ElementKey)
constraint.subjects.0 constraint.subjects.0
}; };
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone()); let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
let class = create_memo(move || { let class = constraint.lorentz_prod_valid.map(
if constraint.lorentz_prod_valid.get() { |&lorentz_prod_valid| if lorentz_prod_valid { "cst" } else { "cst invalid" }
"cst" );
} else {
"cst invalid"
}
});
view! { view! {
li(class=class.get()) { li(class=class.get()) {
input(r#type="checkbox", bind:checked=constraint.active) input(r#type="checkbox", bind:checked=constraint.active)
@ -64,13 +60,9 @@ fn ConstraintOutlineItem(constraint_key: ConstraintKey, element_key: ElementKey)
#[component(inline_props)] #[component(inline_props)]
fn ElementOutlineItem(key: ElementKey, element: assembly::Element) -> View { fn ElementOutlineItem(key: ElementKey, element: assembly::Element) -> View {
let state = use_context::<AppState>(); let state = use_context::<AppState>();
let class = create_memo(move || { let class = state.selection.map(
if state.selection.with(|sel| sel.contains(&key)) { move |sel| if sel.contains(&key) { "selected" } else { "" }
"selected" );
} else {
""
}
});
let label = element.label.clone(); let label = element.label.clone();
let rep_components = element.representation.iter().map(|u| { let rep_components = element.representation.iter().map(|u| {
let u_coord = format!("{:.3}", u).replace("-", "\u{2212}"); let u_coord = format!("{:.3}", u).replace("-", "\u{2212}");
@ -176,15 +168,16 @@ fn ElementOutlineItem(key: ElementKey, element: assembly::Element) -> View {
// //
#[component] #[component]
pub fn Outline() -> View { pub fn Outline() -> View {
// sort the elements alphabetically by ID
let elements_sorted = create_memo(|| {
let state = use_context::<AppState>(); let state = use_context::<AppState>();
state.assembly.elements
.get_clone() // list the elements alphabetically by ID
let element_list = state.assembly.elements.map(
|elts| elts
.clone()
.into_iter() .into_iter()
.sorted_by_key(|(_, elt)| elt.id.clone()) .sorted_by_key(|(_, elt)| elt.id.clone())
.collect() .collect()
}); );
view! { view! {
ul( ul(
@ -195,7 +188,7 @@ pub fn Outline() -> View {
} }
) { ) {
Keyed( Keyed(
list=elements_sorted, list=element_list,
view=|(key, elt)| view! { view=|(key, elt)| view! {
ElementOutlineItem(key=key, element=elt) ElementOutlineItem(key=key, element=elt)
}, },