public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH datacenter-manager 13/15] ui: introduce `LoadResult` helper type
Date: Tue, 21 Oct 2025 16:03:29 +0200	[thread overview]
Message-ID: <20251021140801.3611022-14-d.csapak@proxmox.com> (raw)
In-Reply-To: <20251021140801.3611022-1-d.csapak@proxmox.com>

this factors out some common pattern when loading data, such as saving
the last valid data even when an error occurs, and a check if anything
has been set yet.

This saves a few lines when we use it vs duplicating that pattern
everywhere.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 ui/src/lib.rs               |  3 +++
 ui/src/load_result.rs       | 42 +++++++++++++++++++++++++++++++++++++
 ui/src/pbs/remote.rs        | 30 +++++++++-----------------
 ui/src/pve/lxc.rs           | 28 +++++++------------------
 ui/src/pve/node/overview.rs | 29 +++++++++----------------
 ui/src/pve/qemu.rs          | 28 +++++++------------------
 ui/src/pve/storage.rs       | 29 +++++++------------------
 7 files changed, 89 insertions(+), 100 deletions(-)
 create mode 100644 ui/src/load_result.rs

diff --git a/ui/src/lib.rs b/ui/src/lib.rs
index a2b79b05..de76e1c0 100644
--- a/ui/src/lib.rs
+++ b/ui/src/lib.rs
@@ -39,6 +39,9 @@ pub mod sdn;
 
 pub mod renderer;
 
+mod load_result;
+pub use load_result::LoadResult;
+
 mod tasks;
 pub use tasks::register_pve_tasks;
 
diff --git a/ui/src/load_result.rs b/ui/src/load_result.rs
new file mode 100644
index 00000000..4f3e6d5a
--- /dev/null
+++ b/ui/src/load_result.rs
@@ -0,0 +1,42 @@
+/// Helper wrapper to factor out some common api loading behavior
+pub struct LoadResult<T, E> {
+    pub data: Option<T>,
+    pub error: Option<E>,
+}
+
+impl<T, E> LoadResult<T, E> {
+    /// Creates a new empty result that contains no data or error.
+    pub fn new() -> Self {
+        Self {
+            data: None,
+            error: None,
+        }
+    }
+
+    /// Update the current value with the given result
+    ///
+    /// On `Ok`, the previous error will be deleted.
+    /// On `Err`, the previous valid date is kept.
+    pub fn update(&mut self, result: Result<T, E>) {
+        match result {
+            Ok(data) => {
+                self.error = None;
+                self.data = Some(data);
+            }
+            Err(err) => {
+                self.error = Some(err);
+            }
+        }
+    }
+
+    /// If any of data or err has any value
+    pub fn has_data(&self) -> bool {
+        self.data.is_some() || self.error.is_some()
+    }
+}
+
+impl<T, E> Default for LoadResult<T, E> {
+    fn default() -> Self {
+        Self::new()
+    }
+}
diff --git a/ui/src/pbs/remote.rs b/ui/src/pbs/remote.rs
index 7cf7c7e2..67c9172f 100644
--- a/ui/src/pbs/remote.rs
+++ b/ui/src/pbs/remote.rs
@@ -17,7 +17,7 @@ use pwt::{
 use pbs_api_types::NodeStatus;
 use pdm_api_types::rrddata::PbsNodeDataPoint;
 
-use crate::renderer::separator;
+use crate::{renderer::separator, LoadResult};
 
 #[derive(Clone, Debug, Eq, PartialEq, Properties)]
 pub struct RemoteOverviewPanel {
@@ -59,12 +59,11 @@ pub struct RemoteOverviewPanelComp {
     load_data: Rc<Series>,
     mem_data: Rc<Series>,
     mem_total_data: Rc<Series>,
-    status: Option<NodeStatus>,
+    status: LoadResult<NodeStatus, proxmox_client::Error>,
 
     rrd_time_frame: RRDTimeframe,
 
     last_error: Option<proxmox_client::Error>,
-    last_status_error: Option<proxmox_client::Error>,
 
     async_pool: AsyncPool,
     _timeout: Option<gloo_timers::callback::Timeout>,
@@ -100,9 +99,8 @@ impl yew::Component for RemoteOverviewPanelComp {
             mem_data: Rc::new(Series::new("", Vec::new())),
             mem_total_data: Rc::new(Series::new("", Vec::new())),
             rrd_time_frame: RRDTimeframe::load(),
-            status: None,
+            status: LoadResult::new(),
             last_error: None,
-            last_status_error: None,
             async_pool: AsyncPool::new(),
             _timeout: None,
             _status_timeout: None,
@@ -160,15 +158,7 @@ impl yew::Component for RemoteOverviewPanelComp {
                 Err(err) => self.last_error = Some(err),
             },
             Msg::StatusLoadFinished(res) => {
-                match res {
-                    Ok(status) => {
-                        self.last_status_error = None;
-                        self.status = Some(status);
-                    }
-                    Err(err) => {
-                        self.last_status_error = Some(err);
-                    }
-                }
+                self.status.update(res);
                 let link = ctx.link().clone();
                 self._status_timeout = Some(gloo_timers::callback::Timeout::new(
                     ctx.props().status_interval,
@@ -188,7 +178,7 @@ impl yew::Component for RemoteOverviewPanelComp {
         let props = ctx.props();
 
         if props.remote != old_props.remote {
-            self.status = None;
+            self.status = LoadResult::new();
             self.last_error = None;
             self.time_data = Rc::new(Vec::new());
             self.cpu_data = Rc::new(Series::new("", Vec::new()));
@@ -205,9 +195,8 @@ impl yew::Component for RemoteOverviewPanelComp {
     }
 
     fn view(&self, ctx: &yew::Context<Self>) -> yew::Html {
-        let status_comp = node_info(self.status.as_ref().map(|s| s.into()));
+        let status_comp = node_info(self.status.data.as_ref().map(|s| s.into()));
 
-        let loading = self.status.is_none() && self.last_status_error.is_none();
         let title: Html = Row::new()
             .gap(2)
             .class(AlignItems::Baseline)
@@ -221,12 +210,13 @@ impl yew::Component for RemoteOverviewPanelComp {
             .with_child(
                 // FIXME: add some 'visible' or 'active' property to the progress
                 Progress::new()
-                    .value((!loading).then_some(0.0))
-                    .style("opacity", (!loading).then_some("0")),
+                    .value(self.status.has_data().then_some(0.0))
+                    .style("opacity", self.status.has_data().then_some("0")),
             )
             .with_child(status_comp)
             .with_optional_child(
-                self.last_status_error
+                self.status
+                    .error
                     .as_ref()
                     .map(|err| error_message(&err.to_string())),
             )
diff --git a/ui/src/pve/lxc.rs b/ui/src/pve/lxc.rs
index 08380b66..4028284f 100644
--- a/ui/src/pve/lxc.rs
+++ b/ui/src/pve/lxc.rs
@@ -24,6 +24,7 @@ use pdm_client::types::{IsRunning, LxcStatus};
 use crate::{
     pve::utils::render_lxc_name,
     renderer::{separator, status_row},
+    LoadResult,
 };
 
 #[derive(Clone, Debug, Properties)]
@@ -76,8 +77,7 @@ pub enum Msg {
 }
 
 pub struct LxcanelComp {
-    status: Option<LxcStatus>,
-    last_status_error: Option<proxmox_client::Error>,
+    status: LoadResult<LxcStatus, proxmox_client::Error>,
     last_rrd_error: Option<proxmox_client::Error>,
     _status_timeout: Option<Timeout>,
     _rrd_timeout: Option<Timeout>,
@@ -124,12 +124,11 @@ impl yew::Component for LxcanelComp {
         ctx.link()
             .send_message_batch(vec![Msg::ReloadStatus, Msg::ReloadRrd]);
         Self {
-            status: None,
+            status: LoadResult::new(),
             _status_timeout: None,
             _rrd_timeout: None,
             _async_pool: AsyncPool::new(),
             last_rrd_error: None,
-            last_status_error: None,
 
             rrd_time_frame: RRDTimeframe::load(),
 
@@ -164,15 +163,7 @@ impl yew::Component for LxcanelComp {
                 false
             }
             Msg::StatusResult(res) => {
-                match res {
-                    Ok(status) => {
-                        self.last_status_error = None;
-                        self.status = Some(status);
-                    }
-                    Err(err) => {
-                        self.last_status_error = Some(err);
-                    }
-                }
+                self.status.update(res);
 
                 self._status_timeout = Some(Timeout::new(props.status_interval, move || {
                     link.send_message(Msg::ReloadStatus)
@@ -231,8 +222,7 @@ impl yew::Component for LxcanelComp {
         let props = ctx.props();
 
         if props.remote != old_props.remote || props.info != old_props.info {
-            self.status = None;
-            self.last_status_error = None;
+            self.status = LoadResult::new();
             self.last_rrd_error = None;
 
             self.time = Rc::new(Vec::new());
@@ -262,7 +252,7 @@ impl yew::Component for LxcanelComp {
             .into();
 
         let mut status_comp = Column::new().gap(2).padding(4);
-        let status = match &self.status {
+        let status = match &self.status.data {
             Some(status) => status,
             None => &LxcStatus {
                 cpu: Some(props.info.cpu),
@@ -345,8 +335,6 @@ impl yew::Component for LxcanelComp {
             HumanByte::from(status.maxdisk.unwrap_or_default() as u64).to_string(),
         ));
 
-        let loading = self.status.is_none() && self.last_status_error.is_none();
-
         Panel::new()
             .class(FlexFit)
             .title(title)
@@ -354,8 +342,8 @@ impl yew::Component for LxcanelComp {
             .with_child(
                 // FIXME: add some 'visible' or 'active' property to the progress
                 Progress::new()
-                    .value((!loading).then_some(0.0))
-                    .style("opacity", (!loading).then_some("0")),
+                    .value(self.status.has_data().then_some(0.0))
+                    .style("opacity", self.status.has_data().then_some("0")),
             )
             .with_child(status_comp)
             .with_child(separator().padding_x(4))
diff --git a/ui/src/pve/node/overview.rs b/ui/src/pve/node/overview.rs
index 1a98c004..c2f2958f 100644
--- a/ui/src/pve/node/overview.rs
+++ b/ui/src/pve/node/overview.rs
@@ -17,7 +17,7 @@ use pwt::{
 use pdm_api_types::rrddata::NodeDataPoint;
 use pdm_client::types::NodeStatus;
 
-use crate::renderer::separator;
+use crate::{renderer::separator, LoadResult};
 
 #[derive(Clone, Debug, Eq, PartialEq, Properties)]
 pub struct NodeOverviewPanel {
@@ -62,12 +62,11 @@ pub struct NodeOverviewPanelComp {
     load_data: Rc<Series>,
     mem_data: Rc<Series>,
     mem_total_data: Rc<Series>,
-    status: Option<NodeStatus>,
+    status: LoadResult<NodeStatus, proxmox_client::Error>,
 
     rrd_time_frame: RRDTimeframe,
 
     last_error: Option<proxmox_client::Error>,
-    last_status_error: Option<proxmox_client::Error>,
 
     async_pool: AsyncPool,
     _timeout: Option<gloo_timers::callback::Timeout>,
@@ -103,9 +102,8 @@ impl yew::Component for NodeOverviewPanelComp {
             mem_data: Rc::new(Series::new("", Vec::new())),
             mem_total_data: Rc::new(Series::new("", Vec::new())),
             rrd_time_frame: RRDTimeframe::load(),
-            status: None,
+            status: LoadResult::new(),
             last_error: None,
-            last_status_error: None,
             async_pool: AsyncPool::new(),
             _timeout: None,
             _status_timeout: None,
@@ -165,13 +163,7 @@ impl yew::Component for NodeOverviewPanelComp {
                 Err(err) => self.last_error = Some(err),
             },
             Msg::StatusLoadFinished(res) => {
-                match res {
-                    Ok(status) => {
-                        self.last_status_error = None;
-                        self.status = Some(status);
-                    }
-                    Err(err) => self.last_status_error = Some(err),
-                }
+                self.status.update(res);
                 let link = ctx.link().clone();
                 self._status_timeout = Some(gloo_timers::callback::Timeout::new(
                     ctx.props().status_interval,
@@ -191,8 +183,7 @@ impl yew::Component for NodeOverviewPanelComp {
         let props = ctx.props();
 
         if props.remote != old_props.remote || props.node != old_props.node {
-            self.status = None;
-            self.last_status_error = None;
+            self.status = LoadResult::new();
             self.last_error = None;
             self.time_data = Rc::new(Vec::new());
             self.cpu_data = Rc::new(Series::new("", Vec::new()));
@@ -209,20 +200,20 @@ impl yew::Component for NodeOverviewPanelComp {
     }
 
     fn view(&self, ctx: &yew::Context<Self>) -> yew::Html {
-        let status_comp = node_info(self.status.as_ref().map(|s| s.into()));
-        let loading = self.status.is_none() && self.last_status_error.is_none();
+        let status_comp = node_info(self.status.data.as_ref().map(|s| s.into()));
         Container::new()
             .class(FlexFit)
             .class(ColorScheme::Neutral)
             .with_child(
                 // FIXME: add some 'visible' or 'active' property to the progress
                 Progress::new()
-                    .value((!loading).then_some(0.0))
-                    .style("opacity", (!loading).then_some("0")),
+                    .value(self.status.has_data().then_some(0.0))
+                    .style("opacity", self.status.has_data().then_some("0")),
             )
             .with_child(status_comp)
             .with_optional_child(
-                self.last_status_error
+                self.status
+                    .error
                     .as_ref()
                     .map(|err| error_message(&err.to_string())),
             )
diff --git a/ui/src/pve/qemu.rs b/ui/src/pve/qemu.rs
index 10266f39..3a18ca35 100644
--- a/ui/src/pve/qemu.rs
+++ b/ui/src/pve/qemu.rs
@@ -24,6 +24,7 @@ use pdm_client::types::{IsRunning, QemuStatus};
 use crate::{
     pve::utils::render_qemu_name,
     renderer::{separator, status_row},
+    LoadResult,
 };
 
 #[derive(Clone, Debug, Properties)]
@@ -76,8 +77,7 @@ pub enum Msg {
 }
 
 pub struct QemuPanelComp {
-    status: Option<QemuStatus>,
-    last_status_error: Option<proxmox_client::Error>,
+    status: LoadResult<QemuStatus, proxmox_client::Error>,
     last_rrd_error: Option<proxmox_client::Error>,
     _status_timeout: Option<Timeout>,
     _rrd_timeout: Option<Timeout>,
@@ -124,12 +124,11 @@ impl yew::Component for QemuPanelComp {
         ctx.link()
             .send_message_batch(vec![Msg::ReloadStatus, Msg::ReloadRrd]);
         Self {
-            status: None,
+            status: LoadResult::new(),
             _status_timeout: None,
             _rrd_timeout: None,
             _async_pool: AsyncPool::new(),
             last_rrd_error: None,
-            last_status_error: None,
 
             rrd_time_frame: RRDTimeframe::load(),
 
@@ -164,16 +163,7 @@ impl yew::Component for QemuPanelComp {
                 false
             }
             Msg::StatusResult(res) => {
-                match res {
-                    Ok(status) => {
-                        self.last_status_error = None;
-                        self.status = Some(status);
-                    }
-                    Err(err) => {
-                        self.last_status_error = Some(err);
-                    }
-                }
-
+                self.status.update(res);
                 self._status_timeout = Some(Timeout::new(props.status_interval, move || {
                     link.send_message(Msg::ReloadStatus)
                 }));
@@ -233,8 +223,7 @@ impl yew::Component for QemuPanelComp {
         let props = ctx.props();
 
         if props.remote != old_props.remote || props.info != old_props.info {
-            self.status = None;
-            self.last_status_error = None;
+            self.status = LoadResult::new();
             self.last_rrd_error = None;
 
             self.time = Rc::new(Vec::new());
@@ -265,7 +254,7 @@ impl yew::Component for QemuPanelComp {
 
         let mut status_comp = Column::new().gap(2).padding(4);
 
-        let status = match &self.status {
+        let status = match &self.status.data {
             Some(status) => status,
             None => &QemuStatus {
                 agent: None,
@@ -359,7 +348,6 @@ impl yew::Component for QemuPanelComp {
             HumanByte::from(status.maxdisk.unwrap_or_default() as u64).to_string(),
         ));
 
-        let loading = self.status.is_none() && self.last_status_error.is_none();
         Panel::new()
             .class(FlexFit)
             .title(title)
@@ -367,8 +355,8 @@ impl yew::Component for QemuPanelComp {
             .with_child(
                 // FIXME: add some 'visible' or 'active' property to the progress
                 Progress::new()
-                    .value((!loading).then_some(0.0))
-                    .style("opacity", (!loading).then_some("0")),
+                    .value(self.status.has_data().then_some(0.0))
+                    .style("opacity", self.status.has_data().then_some("0")),
             )
             .with_child(status_comp)
             .with_child(separator().padding_x(4))
diff --git a/ui/src/pve/storage.rs b/ui/src/pve/storage.rs
index 93a2fa29..9730d751 100644
--- a/ui/src/pve/storage.rs
+++ b/ui/src/pve/storage.rs
@@ -22,6 +22,7 @@ use pdm_client::types::PveStorageStatus;
 use crate::{
     pve::utils::{render_content_type, render_storage_type},
     renderer::{separator, status_row_right_icon},
+    LoadResult,
 };
 
 #[derive(Clone, Debug, Properties)]
@@ -74,8 +75,7 @@ pub enum Msg {
 }
 
 pub struct StoragePanelComp {
-    status: Option<PveStorageStatus>,
-    last_status_error: Option<proxmox_client::Error>,
+    status: LoadResult<PveStorageStatus, proxmox_client::Error>,
     last_rrd_error: Option<proxmox_client::Error>,
 
     /// internal guard for the periodic timeout callback to update status
@@ -131,12 +131,11 @@ impl yew::Component for StoragePanelComp {
         ctx.link()
             .send_message_batch(vec![Msg::ReloadStatus, Msg::ReloadRrd]);
         Self {
-            status: None,
+            status: LoadResult::new(),
             status_update_timeout_guard: None,
             rrd_update_timeout_guard: None,
             _async_pool: AsyncPool::new(),
             last_rrd_error: None,
-            last_status_error: None,
 
             rrd_time_frame: RRDTimeframe::load(),
 
@@ -167,16 +166,7 @@ impl yew::Component for StoragePanelComp {
                 false
             }
             Msg::StatusResult(res) => {
-                match res {
-                    Ok(status) => {
-                        self.last_status_error = None;
-                        self.status = Some(status);
-                    }
-                    Err(err) => {
-                        self.last_status_error = Some(err);
-                    }
-                }
-
+                self.status.update(res);
                 self.status_update_timeout_guard =
                     Some(Timeout::new(props.status_interval, move || {
                         link.send_message(Msg::ReloadStatus)
@@ -220,8 +210,7 @@ impl yew::Component for StoragePanelComp {
         let props = ctx.props();
 
         if props.remote != old_props.remote || props.info != old_props.info {
-            self.status = None;
-            self.last_status_error = None;
+            self.status = LoadResult::new();
             self.last_rrd_error = None;
 
             self.time = Rc::new(Vec::new());
@@ -246,7 +235,7 @@ impl yew::Component for StoragePanelComp {
             .into();
 
         let mut status_comp = Column::new().gap(2).padding(4);
-        let status = match &self.status {
+        let status = match &self.status.data {
             Some(status) => status,
             None => &PveStorageStatus {
                 active: None,
@@ -305,8 +294,6 @@ impl yew::Component for StoragePanelComp {
             .value(disk_usage as f32),
         );
 
-        let loading = self.status.is_none() && self.last_status_error.is_none();
-
         Panel::new()
             .class(FlexFit)
             .title(title)
@@ -314,8 +301,8 @@ impl yew::Component for StoragePanelComp {
             .with_child(
                 // FIXME: add some 'visible' or 'active' property to the progress
                 Progress::new()
-                    .value((!loading).then_some(0.0))
-                    .style("opacity", (!loading).then_some("0")),
+                    .value(self.status.has_data().then_some(0.0))
+                    .style("opacity", self.status.has_data().then_some("0")),
             )
             .with_child(status_comp)
             .with_child(separator().padding_x(4))
-- 
2.47.3



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  parent reply	other threads:[~2025-10-21 14:08 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-21 14:03 [pdm-devel] [PATCH datacenter-manager 00/15] prepare ui fore customizable views Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 01/15] ui: dashboard: refactor guest panel creation to its own module Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 02/15] ui: dashboard: refactor creating the node panel into " Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 03/15] ui: dashboard: refactor remote panel creation " Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 04/15] ui: dashboard: remote panel: make wizard menu optional Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 05/15] ui: dashboard: refactor sdn panel creation into its own module Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 06/15] ui: dashboard: refactor task summary panel creation to " Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 07/15] ui: dashboard: refactor subscription " Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 08/15] ui: dashboard: refactor top entities " Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 09/15] ui: dashboard: refactor DashboardConfig editing/constants to their module Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 10/15] ui: dashboard: factor out task parameter calculation Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 11/15] ui: dashboard: remove unused remote list Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 12/15] ui: dashboard: status row: make loading less jarring Dominik Csapak
2025-10-21 14:03 ` Dominik Csapak [this message]
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 14/15] ui: dashboard: implement 'View' Dominik Csapak
2025-10-21 14:03 ` [pdm-devel] [PATCH datacenter-manager 15/15] ui: dashboard: use 'View' instead of the Dashboard Dominik Csapak
2025-10-23  8:33 ` [pdm-devel] superseded: [PATCH datacenter-manager 00/15] prepare ui fore customizable views Dominik Csapak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251021140801.3611022-14-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal