all lists on 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 v3 4/9] ui: add possibility to insert into search box
Date: Tue, 26 Aug 2025 14:31:20 +0200	[thread overview]
Message-ID: <20250826123336.3970108-5-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250826123336.3970108-1-d.csapak@proxmox.com>

by implementing a 'SearchProvider' context. This enables us to
insert a search term from everywhere. This can be helpful e.g. if we
want to prefill the search box with a specific pattern

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 ui/Cargo.toml               |  1 +
 ui/src/lib.rs               |  3 +++
 ui/src/main.rs              | 17 ++++++++++++-----
 ui/src/search_provider.rs   | 35 +++++++++++++++++++++++++++++++++++
 ui/src/widget/search_box.rs | 26 +++++++++++++++++++++-----
 5 files changed, 72 insertions(+), 10 deletions(-)
 create mode 100644 ui/src/search_provider.rs

diff --git a/ui/Cargo.toml b/ui/Cargo.toml
index ef66020..4c48502 100644
--- a/ui/Cargo.toml
+++ b/ui/Cargo.toml
@@ -43,6 +43,7 @@ pbs-api-types = "1.0.3"
 pdm-api-types = { version = "0.2", path = "../lib/pdm-api-types" }
 pdm-ui-shared = { version = "0.2", path = "../lib/pdm-ui-shared" }
 pdm-client = { version = "0.2", path = "../lib/pdm-client" }
+pdm-search = { version = "0.2", path = "../lib/pdm-search" }
 
 [patch.crates-io]
 # proxmox-client = { path = "../../proxmox/proxmox-client" }
diff --git a/ui/src/lib.rs b/ui/src/lib.rs
index e3755ec..edb50f9 100644
--- a/ui/src/lib.rs
+++ b/ui/src/lib.rs
@@ -21,6 +21,9 @@ pub use remotes::RemoteConfigPanel;
 mod top_nav_bar;
 pub use top_nav_bar::TopNavBar;
 
+mod search_provider;
+pub use search_provider::SearchProvider;
+
 mod dashboard;
 pub use dashboard::Dashboard;
 use yew_router::prelude::RouterScopeExt;
diff --git a/ui/src/main.rs b/ui/src/main.rs
index 6e2c9b2..be0c10c 100644
--- a/ui/src/main.rs
+++ b/ui/src/main.rs
@@ -22,7 +22,7 @@ use proxmox_yew_comp::{
 
 //use pbs::MainMenu;
 use pdm_api_types::subscription::{RemoteSubscriptionState, RemoteSubscriptions};
-use pdm_ui::{register_pve_tasks, MainMenu, RemoteList, TopNavBar};
+use pdm_ui::{register_pve_tasks, MainMenu, RemoteList, SearchProvider, TopNavBar};
 
 type MsgRemoteList = Result<RemoteList, Error>;
 
@@ -46,6 +46,7 @@ struct DatacenterManagerApp {
     remote_list: RemoteList,
     remote_list_error: Option<String>,
     remote_list_timeout: Option<Timeout>,
+    search_provider: SearchProvider,
 }
 
 async fn check_subscription() -> Msg {
@@ -166,6 +167,7 @@ impl Component for DatacenterManagerApp {
             remote_list: Vec::new().into(),
             remote_list_error: None,
             remote_list_timeout: None,
+            search_provider: SearchProvider::new(),
         };
 
         this.on_login(ctx, false);
@@ -258,10 +260,15 @@ impl Component for DatacenterManagerApp {
             .with_optional_child(subscription_alert);
 
         let context = self.remote_list.clone();
-
-        DesktopApp::new(
-            html! {<ContextProvider<RemoteList> {context}>{body}</ContextProvider<RemoteList>>},
-        )
+        let search_context = self.search_provider.clone();
+
+        DesktopApp::new(html! {
+            <ContextProvider<SearchProvider> context={search_context}>
+                <ContextProvider<RemoteList> {context}>
+                    {body}
+                </ContextProvider<RemoteList>>
+            </ContextProvider<SearchProvider>>
+        })
         .into()
     }
 }
diff --git a/ui/src/search_provider.rs b/ui/src/search_provider.rs
new file mode 100644
index 0000000..441cc2b
--- /dev/null
+++ b/ui/src/search_provider.rs
@@ -0,0 +1,35 @@
+use yew::Callback;
+
+use pwt::state::{SharedState, SharedStateObserver};
+
+use pdm_search::Search;
+
+#[derive(Clone, PartialEq)]
+pub struct SearchProvider {
+    state: SharedState<String>,
+}
+
+impl SearchProvider {
+    pub fn new() -> Self {
+        Self {
+            state: SharedState::new("".into()),
+        }
+    }
+
+    pub fn add_listener(
+        &self,
+        cb: impl Into<Callback<SharedState<String>>>,
+    ) -> SharedStateObserver<String> {
+        self.state.add_listener(cb)
+    }
+
+    pub fn search(&self, search_term: Search) {
+        **self.state.write() = search_term.to_string();
+    }
+}
+
+pub fn get_search_provider<T: yew::Component>(ctx: &yew::Context<T>) -> Option<SearchProvider> {
+    let (provider, _context_listener) = ctx.link().context(Callback::from(|_| {}))?;
+
+    Some(provider)
+}
diff --git a/ui/src/widget/search_box.rs b/ui/src/widget/search_box.rs
index 0aeedb7..6b2478f 100644
--- a/ui/src/widget/search_box.rs
+++ b/ui/src/widget/search_box.rs
@@ -9,13 +9,15 @@ use yew::{
 };
 
 use pwt::{
-    dom::focus::FocusTracker,
-    dom::IntoHtmlElement,
+    dom::{focus::FocusTracker, IntoHtmlElement},
     prelude::*,
     props::CssLength,
+    state::{SharedState, SharedStateObserver},
     widget::{form::Field, Container},
 };
 
+use crate::search_provider::get_search_provider;
+
 use super::ResourceTree;
 
 #[derive(Properties, PartialEq)]
@@ -35,7 +37,7 @@ impl From<SearchBox> for VNode {
 }
 
 pub enum Msg {
-    ChangeTerm(String),
+    ChangeTerm(String, bool), // force value
     FocusChange(bool),
     ToggleFocus,
 }
@@ -48,6 +50,8 @@ pub struct PdmSearchBox {
     focus: bool,
     global_shortcut_listener: Closure<dyn Fn(KeyboardEvent)>,
     toggle_focus: bool,
+    _observer: Option<SharedStateObserver<String>>,
+    force_value: bool,
 }
 
 impl Component for PdmSearchBox {
@@ -57,6 +61,14 @@ impl Component for PdmSearchBox {
 
     fn create(ctx: &yew::Context<Self>) -> Self {
         let link = ctx.link().clone();
+        let _observer = get_search_provider(ctx).map(|search| {
+            search.add_listener(ctx.link().batch_callback(|value: SharedState<String>| {
+                vec![
+                    Msg::ToggleFocus,
+                    Msg::ChangeTerm(value.read().clone(), true),
+                ]
+            }))
+        });
         Self {
             search_field_ref: Default::default(),
             search_box_ref: Default::default(),
@@ -72,13 +84,16 @@ impl Component for PdmSearchBox {
                     _ => {}
                 }
             })),
+            _observer,
+            force_value: false,
         }
     }
 
     fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
         match msg {
-            Msg::ChangeTerm(term) => {
+            Msg::ChangeTerm(term, force_value) => {
                 self.search_term = term;
+                self.force_value = force_value;
                 true
             }
             Msg::FocusChange(focus) => {
@@ -122,7 +137,8 @@ impl Component for PdmSearchBox {
                 Field::new()
                     .placeholder(tr!("Search (Ctrl+Space / Ctrl+Shift+F)"))
                     .node_ref(self.search_field_ref.clone())
-                    .on_input(ctx.link().callback(Msg::ChangeTerm)),
+                    .value(self.force_value.then_some(self.search_term.clone()))
+                    .on_input(ctx.link().callback(|term| Msg::ChangeTerm(term, false))),
             )
             .with_child(search_result)
             .into()
-- 
2.47.2



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


  parent reply	other threads:[~2025-08-26 12:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-26 12:31 [pdm-devel] [PATCH datacenter-manager v3 0/9] implement more complex search syntax Dominik Csapak
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 1/9] pdm-api-types: resources: add helper methods for fields Dominik Csapak
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 2/9] lib: add pdm-search crate Dominik Csapak
2025-08-27  9:12   ` Lukas Wagner
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 3/9] server: api: resources: add more complex filter syntax Dominik Csapak
2025-08-27  9:15   ` Lukas Wagner
2025-08-27  9:33     ` Stefan Hanreich
2025-08-27 20:15       ` Thomas Lamprecht
2025-08-26 12:31 ` Dominik Csapak [this message]
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 5/9] ui: dashboard: remotes panel: open search on click Dominik Csapak
2025-08-27  9:37   ` Lukas Wagner
2025-08-28  8:54     ` Dominik Csapak
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 6/9] ui: dashboard: guest panel: search for guest states when clicking on them Dominik Csapak
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 7/9] ui: dashboard: search for nodes when clicking on the nodes panel Dominik Csapak
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 8/9] ui: search box: add clear trigger Dominik Csapak
2025-08-26 12:31 ` [pdm-devel] [PATCH datacenter-manager v3 9/9] ui: dashboard: guest panel: improve column widths Dominik Csapak
2025-08-26 14:22 ` [pdm-devel] [PATCH datacenter-manager v3 0/9] implement more complex search syntax Stefan Hanreich
2025-08-28 13:21 ` [pdm-devel] superseded: " 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=20250826123336.3970108-5-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal