Skip to main content

monocoque_server/
app.rs

1use leptos::prelude::*;
2use leptos_meta::{provide_meta_context, MetaTags, Stylesheet, Title};
3use leptos_router::{
4    components::{Route, Router, Routes},
5    StaticSegment,
6};
7
8pub fn shell(options: LeptosOptions) -> impl IntoView {
9    view! {
10        <!DOCTYPE html>
11        <html lang="en">
12            <head>
13                <meta charset="utf-8"/>
14                <meta name="viewport" content="width=device-width, initial-scale=1"/>
15                <AutoReload options=options.clone() />
16                <HydrationScripts options/>
17                <MetaTags/>
18            </head>
19            <body>
20                <App/>
21            </body>
22        </html>
23    }
24}
25
26#[server]
27pub async fn get_space_information() -> Result<String, ServerFnError> {
28    let space = monocoque_core::get_space("/srv/").map_err(ServerFnError::new)?;
29
30    let to_tib = |b: u128| b as f64 / (1024_u128.pow(4)) as f64;
31
32    Ok(format!(
33        "Total: {:.2} TiB, Used: {:.2} TiB, Free: {:.2} TiB",
34        to_tib(space.total_bytes),
35        to_tib(space.used_bytes),
36        to_tib(space.free_bytes),
37    ))
38}
39
40#[component]
41pub fn App() -> impl IntoView {
42    // Provides context that manages stylesheets, titles, meta tags, etc.
43    provide_meta_context();
44
45    view! {
46        // injects a stylesheet into the document <head>
47        // id=leptos means cargo-leptos will hot-reload this stylesheet
48        <Stylesheet id="leptos" href="/pkg/monocoque-server.css"/>
49
50        // sets the document title
51        <Title text="Welcome to Leptos"/>
52
53        // content for this welcome page
54        <Router>
55            <main>
56                <Routes fallback=|| "Page not found.".into_view()>
57                    <Route path=StaticSegment("") view=HomePage/>
58                </Routes>
59            </main>
60        </Router>
61    }
62}
63
64#[component]
65fn HomePage() -> impl IntoView {
66    let count = RwSignal::new(0);
67    let on_click = move |_| *count.write() += 1;
68
69    let space = Resource::new(|| (), |_| async move { get_space_information().await });
70
71    view! {
72        <h1>"Welcome to Leptos!!!!"</h1>
73
74        <Suspense>
75            <h1 class="text-red-500">
76                {move || {
77                    match space.get() {
78                        None => "Loading space...".to_string(),
79                        Some(Ok(space)) => space,
80                        Some(Err(err)) => format!("Error: {err}"),
81                    }
82                }}
83            </h1>
84        </Suspense>
85
86        <button on:click=on_click class="text-red-900">"Click Me: " {count}</button>
87    }
88}