use leptos::prelude::*;
use leptos_router::hooks::use_location;
use leptos_router::NavigateOptions;
pub fn use_route_active(path: &str, exact: bool) -> Memo<bool> {
let location = use_location();
let path = path.to_string();
Memo::new(move |_| {
let raw = location.pathname.get();
let normalized = if raw.len() > 1 {
raw.trim_end_matches('/').to_string()
} else {
raw
};
if exact {
normalized == path
} else {
normalized.starts_with(&path)
}
})
}
pub fn navigate_back_or(fallback: &str, navigate: &impl Fn(&str, NavigateOptions)) {
#[cfg(target_arch = "wasm32")]
{
if let Some(window) = web_sys::window() {
if let Ok(history) = window.history() {
if history.length().unwrap_or(0) > 1 {
let _ = history.back();
return;
}
}
let referrer = window.document().map(|d| d.referrer()).unwrap_or_default();
if !referrer.is_empty() {
if let Ok(history) = window.history() {
let _ = history.back();
return;
}
}
}
}
navigate(fallback, NavigateOptions::default());
}