From 974471f74502d41a4757dec15be63a03b1c978d6 Mon Sep 17 00:00:00 2001 From: luoxiao Date: Sat, 28 Dec 2024 22:10:28 +0800 Subject: [PATCH] fix: fallback avatar to initials on image load error --- thaw/src/avatar/mod.rs | 53 ++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/thaw/src/avatar/mod.rs b/thaw/src/avatar/mod.rs index 8a66aa38..0a3eac1d 100644 --- a/thaw/src/avatar/mod.rs +++ b/thaw/src/avatar/mod.rs @@ -43,21 +43,34 @@ pub fn Avatar( Some(style) }; - let name = StoredValue::new(name); - let src = StoredValue::new(src); - let initials = StoredValue::new(initials); + let image_hidden = RwSignal::new(false); let is_show_default_icon = Memo::new(move |_| { - if name.with_value(|n| n.with(|n| n.is_some())) { + if name.with(|n| n.is_some()) { false - } else if src.with_value(|s| s.with(|s| s.is_some())) { + } else if src.with(|s| s.is_some()) && !image_hidden.get() { false - } else if initials.with_value(|i| i.with(|i| i.is_some())) { + } else if initials.with(|i| i.is_some()) { false } else { true } }); + let on_load = move |_| { + image_hidden.maybe_update(|hidden| { + if *hidden { + *hidden = false; + true + } else { + true + } + }); + }; + + let on_error = move |_| { + image_hidden.set(true); + }; + view! { {move || { - let mut initials = initials.with_value(|i| i.get()); + let mut initials = initials.get(); if initials.is_none() { - name.with_value(|name| { - if let Some(name) = name.get() { - initials = Some(initials_name(name)); - } - }); + if let Some(name) = name.get() { + initials = Some(initials_name(name)); + } } view! { @@ -85,10 +96,17 @@ pub fn Avatar( } }} {move || { - let src = src.with_value(|s| s.get()); view! { - - + + } }} @@ -103,6 +121,7 @@ pub fn Avatar( width="1em" height="1em" viewBox="0 0 20 20" + xmlns="http://www.w3.org/2000/svg" >