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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use dioxus::prelude::*;
use std::{cell::RefCell, str::FromStr};

use crate::{prelude::Outlet, routable::Routable, router_cfg::RouterConfig};

/// The config for [`Router`].
pub struct RouterConfigFactory<R: Routable> {
    #[allow(clippy::type_complexity)]
    config: RefCell<Option<Box<dyn FnOnce() -> RouterConfig<R>>>>,
}

#[cfg(feature = "serde")]
impl<R: Routable> Default for RouterConfigFactory<R>
where
    <R as FromStr>::Err: std::fmt::Display,
    R: serde::Serialize + serde::de::DeserializeOwned,
{
    fn default() -> Self {
        Self::from(RouterConfig::default)
    }
}

#[cfg(not(feature = "serde"))]
impl<R: Routable> Default for RouterConfigFactory<R>
where
    <R as FromStr>::Err: std::fmt::Display,
{
    fn default() -> Self {
        Self::from(RouterConfig::default)
    }
}

impl<R: Routable, F: FnOnce() -> RouterConfig<R> + 'static> From<F> for RouterConfigFactory<R> {
    fn from(value: F) -> Self {
        Self {
            config: RefCell::new(Some(Box::new(value))),
        }
    }
}

#[cfg(feature = "serde")]
/// The props for [`Router`].
#[derive(Props)]
pub struct RouterProps<R: Routable>
where
    <R as FromStr>::Err: std::fmt::Display,
    R: serde::Serialize + serde::de::DeserializeOwned,
{
    #[props(default, into)]
    config: RouterConfigFactory<R>,
}

#[cfg(not(feature = "serde"))]
/// The props for [`Router`].
#[derive(Props)]
pub struct RouterProps<R: Routable>
where
    <R as FromStr>::Err: std::fmt::Display,
{
    #[props(default, into)]
    config: RouterConfigFactory<R>,
}

#[cfg(not(feature = "serde"))]
impl<R: Routable> Default for RouterProps<R>
where
    <R as FromStr>::Err: std::fmt::Display,
{
    fn default() -> Self {
        Self {
            config: RouterConfigFactory::default(),
        }
    }
}

#[cfg(feature = "serde")]
impl<R: Routable> Default for RouterProps<R>
where
    <R as FromStr>::Err: std::fmt::Display,
    R: serde::Serialize + serde::de::DeserializeOwned,
{
    fn default() -> Self {
        Self {
            config: RouterConfigFactory::default(),
        }
    }
}

#[cfg(not(feature = "serde"))]
impl<R: Routable> PartialEq for RouterProps<R>
where
    <R as FromStr>::Err: std::fmt::Display,
{
    fn eq(&self, _: &Self) -> bool {
        // prevent the router from re-rendering when the initial url or config changes
        true
    }
}

#[cfg(feature = "serde")]
impl<R: Routable> PartialEq for RouterProps<R>
where
    <R as FromStr>::Err: std::fmt::Display,
    R: serde::Serialize + serde::de::DeserializeOwned,
{
    fn eq(&self, _: &Self) -> bool {
        // prevent the router from re-rendering when the initial url or config changes
        true
    }
}

#[cfg(not(feature = "serde"))]
/// A component that renders the current route.
pub fn Router<R: Routable + Clone>(cx: Scope<RouterProps<R>>) -> Element
where
    <R as FromStr>::Err: std::fmt::Display,
{
    use crate::prelude::{outlet::OutletContext, RouterContext};

    use_context_provider(cx, || {
        RouterContext::new(
            (cx.props
                .config
                .config
                .take()
                .expect("use_context_provider ran twice"))(),
            cx.schedule_update_any(),
        )
    });
    use_context_provider(cx, || OutletContext::<R> {
        current_level: 0,
        _marker: std::marker::PhantomData,
    });

    render! {
        Outlet::<R> {}
    }
}

#[cfg(feature = "serde")]
/// A component that renders the current route.
pub fn Router<R: Routable + Clone>(cx: Scope<RouterProps<R>>) -> Element
where
    <R as FromStr>::Err: std::fmt::Display,
    R: serde::Serialize + serde::de::DeserializeOwned,
{
    use_context_provider(cx, || {
        RouterContext::new(
            (cx.props
                .config
                .config
                .take()
                .expect("use_context_provider ran twice"))(),
            cx.schedule_update_any(),
        )
    });
    use_context_provider(cx, || OutletContext::<R> {
        current_level: 0,
        _marker: std::marker::PhantomData,
    });

    render! {
        Outlet::<R> {}
    }
}