summary refs log tree commit diff
path: root/src/components/app.rs
blob: dd2d018423f0d8fba0f06f152f38aae3d4f57a53 (plain)
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
use std::collections::HashMap;

use leptos::prelude::*;
use leptos::Params;
use leptos_meta::{provide_meta_context, MetaTags, Stylesheet, Title};
use leptos_router::hooks::use_params;
use leptos_router::params::Params;
use leptos_router::{
    components::{ParentRoute, Route, Router, Routes},
    path,
};

use super::editor::WikiEditor;
use super::renderer::WikiPage;
use crate::components::layout::Layout;
use crate::data::Namespace;
use crate::data::Page;
use crate::data::PageUuid;

pub fn shell(options: LeptosOptions) -> impl IntoView {
    view! {
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta charset="utf-8"/>
                <meta name="viewport" content="width=device-width, initial-scale=1"/>
                <AutoReload options=options.clone() />
                <HydrationScripts options islands=true/>
                <MetaTags/>
            </head>
            <body>
                <App/>
            </body>
        </html>
    }
}

#[component]
pub fn App() -> impl IntoView {
    // Provides context that manages stylesheets, titles, meta tags, etc.
    provide_meta_context();

    view! {
        // injects a stylesheet into the document <head>
        // id=leptos means cargo-leptos will hot-reload this stylesheet
        <Stylesheet id="leptos" href="/_/stormscribe.css"/>

        // sets the document title
        <Title text="Welcome to Leptos"/>

        // content for this welcome page
        <Router>
            <Routes fallback=|| "Page not found.".into_view()>
                <ParentRoute path=path!("/") view=Layout>
                    <Route path=path!("/~/*path") view=PageRender/>
                    <Route path=path!("/edit/*path") view=PageEdit/>
                </ParentRoute>
            </Routes>
        </Router>
    }
}

#[derive(Params, PartialEq)]
struct PageParams {
    path: Option<String>,
}

#[server]
async fn get_namespace() -> Result<Namespace, ServerFnError> {
    use crate::data::StormscribeData;

    Ok(StormscribeData::get_namespace())
}

#[server]
async fn get_pages() -> Result<HashMap<PageUuid, Page>, ServerFnError> {
    use crate::data::StormscribeData;

    Ok(StormscribeData::get_pages())
}

// Renders a page
#[component]
fn PageRender() -> impl IntoView {
    let params = use_params::<PageParams>();

    let page_path = params
        .read()
        .as_ref()
        .ok()
        .map(|params| params.path.clone().unwrap_or("Unknown path".to_string()))
        .unwrap_or("Could not read params".to_string());

    let namespace = Resource::new(move || {}, |_| get_namespace());
    let page_resource = Resource::new(move || {}, |_| get_pages());

    view! {
        <Suspense
            fallback=move || view! { <p>"Loading..."</p> }
        >
            {move || Suspend::new(async move {
                let name_data = namespace.await;
                let page_data = page_resource.await;
                match (name_data, page_data) {
                    (Ok(names), Ok(pages)) => view! {
                        <pre>{format!("{names:#?}")}</pre>
                        <pre>{format!("{pages:#?}")}</pre>
                    }.into_any(),
                    _ => view! {<p>Error</p>}.into_any(),
                }
            })}
        </Suspense>
    }
    .into_any()
}

// Renders a page
#[component]
fn PageEdit() -> impl IntoView {
    let params = use_params::<PageParams>();

    let page_path = params
        .read()
        .as_ref()
        .ok()
        .map(|params| params.path.clone().unwrap_or("Unknown path".to_string()))
        .unwrap_or("Could not read params".to_string());

    view! {
        <WikiEditor url_path=page_path />
    }
}