use gpui::{App, Global, SharedString};
use hashbrown::HashMap;
use matchit::Params;
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
pub struct Location {
pub pathname: SharedString,
pub state: Params<'static, 'static>,
}
impl Default for Location {
fn default() -> Self {
Self {
pathname: "/".into(),
state: Params::default(),
}
}
}
#[derive(PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
pub struct PathMatch {
pub pathname: SharedString,
pub pathname_base: SharedString,
pub pattern: SharedString,
pub params: Params<'static, 'static>,
}
#[derive(PartialEq, Clone)]
pub struct RouterState {
pub location: Location,
pub path_match: Option<PathMatch>,
pub params: HashMap<SharedString, SharedString>,
}
impl Global for RouterState {}
impl RouterState {
pub fn init(cx: &mut App) {
let state = Self {
location: Location::default(),
path_match: None,
params: HashMap::new(),
};
cx.set_global::<RouterState>(state);
}
pub fn with_path(&mut self, pathname: SharedString) -> &mut Self {
self.location.pathname = pathname;
self
}
pub fn global(cx: &App) -> &Self {
cx.global::<Self>()
}
pub fn global_mut(cx: &mut App) -> &mut Self {
cx.global_mut::<Self>()
}
}