1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::{
Location, NavigateOptions, NavigationError, Params, ParamsError, ParamsMap,
RouteContext, RouterContext,
};
use leptos::{create_memo, signal_prelude::*, use_context, Memo, Scope};
use std::rc::Rc;
pub fn use_router(cx: Scope) -> RouterContext {
if let Some(router) = use_context::<RouterContext>(cx) {
router
} else {
leptos::leptos_dom::debug_warn!(
"You must call use_router() within a <Router/> component"
);
panic!("You must call use_router() within a <Router/> component");
}
}
pub fn use_route(cx: Scope) -> RouteContext {
use_context::<RouteContext>(cx).unwrap_or_else(|| use_router(cx).base())
}
pub fn use_location(cx: Scope) -> Location {
use_router(cx).inner.location.clone()
}
pub fn use_params_map(cx: Scope) -> Memo<ParamsMap> {
let route = use_route(cx);
route.params()
}
pub fn use_params<T: Params>(cx: Scope) -> Memo<Result<T, ParamsError>>
where
T: PartialEq + std::fmt::Debug,
{
let route = use_route(cx);
create_memo(cx, move |_| route.params().with(T::from_map))
}
pub fn use_query_map(cx: Scope) -> Memo<ParamsMap> {
use_router(cx).inner.location.query
}
pub fn use_query<T: Params>(cx: Scope) -> Memo<Result<T, ParamsError>>
where
T: PartialEq + std::fmt::Debug,
{
let router = use_router(cx);
create_memo(cx, move |_| {
router.inner.location.query.with(|m| T::from_map(m))
})
}
pub fn use_resolved_path(
cx: Scope,
path: impl Fn() -> String + 'static,
) -> Memo<Option<String>> {
let route = use_route(cx);
create_memo(cx, move |_| {
let path = path();
if path.starts_with('/') {
Some(path)
} else {
route.resolve_path(&path).map(String::from)
}
})
}
pub fn use_navigate(
cx: Scope,
) -> impl Fn(&str, NavigateOptions) -> Result<(), NavigationError> {
let router = use_router(cx);
move |to, options| {
Rc::clone(&router.inner).navigate_from_route(to, &options)
}
}