|
| 1 | +//! Shared loading/empty/error renderer for any seam-backed read. |
| 2 | +//! |
| 3 | +//! The caller maps its `use_resource` result to `AsyncState<T>` (plain Rust), |
| 4 | +//! renders the `Loaded(T)` case itself, and delegates the three non-loaded |
| 5 | +//! states to this component. Keeps every wired view from re-implementing the |
| 6 | +//! spinner / empty / error+retry markup. |
| 7 | +//! |
| 8 | +//! Calling convention (wired up by plan 01-04): |
| 9 | +//! ```ignore |
| 10 | +//! match AsyncState::from_value(resource.read().clone()) { |
| 11 | +//! AsyncState::Loaded(data) => rsx! { /* caller's own markup for `data` */ }, |
| 12 | +//! other => rsx! { |
| 13 | +//! AsyncView { |
| 14 | +//! loading: matches!(other, AsyncState::Loading), |
| 15 | +//! empty: matches!(other, AsyncState::Empty), |
| 16 | +//! error: match &other { AsyncState::Error(e) => Some(e.to_string()), _ => None }, |
| 17 | +//! retriable: matches!(&other, AsyncState::Error(e) if e.is_retriable()), |
| 18 | +//! on_retry: move |_| resource.restart(), |
| 19 | +//! } |
| 20 | +//! } |
| 21 | +//! } |
| 22 | +//! ``` |
| 23 | +//! Discrete props (rather than the generic `AsyncState<T>`) are used because |
| 24 | +//! Dioxus props must be `Clone + PartialEq`; `StudioError` is neither, and `T` |
| 25 | +//! is unconstrained — so the caller decodes the state and passes flags. |
| 26 | +
|
| 27 | +use dioxus::prelude::*; |
| 28 | + |
| 29 | +#[derive(Clone, PartialEq, Props)] |
| 30 | +pub struct AsyncViewProps { |
| 31 | + /// True while the first fetch is pending. |
| 32 | + pub loading: bool, |
| 33 | + /// True when the fetch returned an empty result. |
| 34 | + pub empty: bool, |
| 35 | + /// Some(message) when the fetch failed; None otherwise. |
| 36 | + pub error: Option<String>, |
| 37 | + /// Whether the failed fetch is retriable (gates the Retry button). |
| 38 | + #[props(default = false)] |
| 39 | + pub retriable: bool, |
| 40 | + /// Called when the user clicks Retry (wire to Resource::restart()). |
| 41 | + #[props(default)] |
| 42 | + pub on_retry: EventHandler<()>, |
| 43 | + /// Message shown in the empty state. |
| 44 | + #[props(default = "No records.".to_string())] |
| 45 | + pub empty_message: String, |
| 46 | +} |
| 47 | + |
| 48 | +// Consumed by the notification popover (01-04) and later wired views. |
| 49 | +#[component] |
| 50 | +pub fn AsyncView(props: AsyncViewProps) -> Element { |
| 51 | + if props.loading { |
| 52 | + return rsx! { div { class: "async-loading", "Loading…" } }; |
| 53 | + } |
| 54 | + if let Some(msg) = props.error.clone() { |
| 55 | + return rsx! { |
| 56 | + div { class: "async-error", |
| 57 | + div { class: "async-error-msg", "{msg}" } |
| 58 | + if props.retriable { |
| 59 | + button { |
| 60 | + class: "btn", |
| 61 | + onclick: move |_| props.on_retry.call(()), |
| 62 | + "Retry" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + if props.empty { |
| 69 | + return rsx! { div { class: "async-empty", "{props.empty_message}" } }; |
| 70 | + } |
| 71 | + // Loaded: the caller renders its own markup; AsyncView renders nothing. |
| 72 | + rsx! {} |
| 73 | +} |
0 commit comments