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
use dioxus_core::Element;

use dioxus_core as dioxus;
use dioxus_core::prelude::*;
use dioxus_core_macro::Props;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;

use crate::{RouteContext, RouterService};

#[derive(Props)]
pub struct RouteProps<'a> {
    to: &'a str,

    children: Element<'a>,

    #[props(default)]
    fallback: bool,
}

pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
    // now we want to submit
    let router_root = cx
        .use_hook(|_| cx.consume_context::<RouterService>())
        .as_ref()?;

    cx.use_hook(|_| {
        // create a bigger, better, longer route if one above us exists
        let total_route = match cx.consume_context::<RouteContext>() {
            Some(ctx) => format!("{}", ctx.total_route.clone()),
            None => format!("{}", cx.props.to.clone()),
        };

        // provide our route context
        let route_context = cx.provide_context(RouteContext {
            declared_route: cx.props.to.to_string(),
            total_route,
        });

        // submit our rout
        router_root.register_total_route(
            route_context.total_route.clone(),
            cx.scope_id(),
            cx.props.fallback,
        );

        Some(RouteInner {})
    });

    log::trace!("Checking route {}", cx.props.to);

    if router_root.should_render(cx.scope_id()) {
        cx.render(rsx!(&cx.props.children))
    } else {
        None
    }
}

struct RouteInner {}

impl Drop for RouteInner {
    fn drop(&mut self) {
        // todo!()
    }
}