Skip to content

Commit

Permalink
chore: drop dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Aug 29, 2023
1 parent 773ee18 commit 38ac316
Showing 1 changed file with 0 additions and 88 deletions.
88 changes: 0 additions & 88 deletions spog/ui/src/hooks/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,11 @@ use crate::{
components::search::{DynamicSearchParameters, SearchMode},
utils::search::*,
};
use gloo_utils::format::JsValueSerdeExt;
use patternfly_yew::prelude::*;
use spog_model::config::Filters;
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use wasm_bindgen::JsValue;
use yew::prelude::*;

pub const DEFAULT_PAGE_SIZE: usize = 10;

#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct SearchViewState<T> {
pub search: T,
pub pagination: PaginationControl,
}

impl<T> Deref for SearchViewState<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.search
}
}

impl<T> DerefMut for SearchViewState<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.search
}
}

#[hook]
pub fn use_search_view_state<T, F>(
props_query: Option<String>,
total: Option<usize>,
f: F,
) -> (UseStateHandle<T>, UsePagination)
where
T: for<'de> serde::Deserialize<'de> + serde::Serialize + Clone + Default + Debug + PartialEq + 'static,
F: FnOnce(String) -> T,
{
// the active query
let state = use_memo(
|()| {
// initialize with the state from history, properties, or with a reasonable default
gloo_utils::history()
.state()
.ok()
.and_then(|state| {
let deser = state.into_serde::<SearchViewState<T>>();
log::debug!("Deserialized: {deser:?}");
deser.ok()
})
.or_else(|| {
props_query.and_then(|s| {
log::debug!("Initial: {s}");
match s.is_empty() {
true => None,
false => Some(SearchViewState {
search: f(s),
pagination: PaginationControl {
per_page: DEFAULT_PAGE_SIZE,
..Default::default()
},
}),
}
})
})
.unwrap_or_default()
},
(),
);

let search_params = use_state_eq(|| state.search.clone());
let pagination = use_pagination(total, || state.pagination);

// when the search params or pagination state changes, store in history API
use_effect_with_deps(
|(search_params, pagination)| {
// store changes to the state in the current history
if let Ok(data) = JsValue::from_serde(&SearchViewState {
search: search_params,
pagination: *pagination,
}) {
let _ = gloo_utils::history().replace_state(&data, "");
}
},
((*search_params).clone(), pagination.state.control),
);

(search_params, pagination)
}

#[derive(Clone)]
pub struct UseStandardSearch {
pub search_params: UseStateHandle<SearchMode<DynamicSearchParameters>>,
Expand Down

0 comments on commit 38ac316

Please sign in to comment.