use leptos::prelude::*;
use leptos_meta::{provide_meta_context, MetaTags, Stylesheet, Title};
use leptos_router::{
components::{Route, Router, Routes},
StaticSegment,
};
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/>
<MetaTags/>
</head>
<body>
<App/>
</body>
</html>
}
}
#[server]
pub async fn get_space_information() -> Result<String, ServerFnError> {
let space = monocoque_core::get_space("/srv/").map_err(ServerFnError::new)?;
let to_tib = |b: u128| b as f64 / (1024_u128.pow(4)) as f64;
Ok(format!(
"Total: {:.2} TiB, Used: {:.2} TiB, Free: {:.2} TiB",
to_tib(space.total_bytes),
to_tib(space.used_bytes),
to_tib(space.free_bytes),
))
}
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
view! {
<Stylesheet id="leptos" href="/pkg/monocoque-server.css"/>
<Title text="Welcome to Leptos"/>
<Router>
<main>
<Routes fallback=|| "Page not found.".into_view()>
<Route path=StaticSegment("") view=HomePage/>
</Routes>
</main>
</Router>
}
}
#[component]
fn HomePage() -> impl IntoView {
let count = RwSignal::new(0);
let on_click = move |_| *count.write() += 1;
let space = Resource::new(|| (), |_| async move { get_space_information().await });
view! {
<h1>"Welcome to Leptos!!!!"</h1>
<Suspense>
<h1 class="text-red-500">
{move || {
match space.get() {
None => "Loading space...".to_string(),
Some(Ok(space)) => space,
Some(Err(err)) => format!("Error: {err}"),
}
}}
</h1>
</Suspense>
<button on:click=on_click class="text-red-900">"Click Me: " {count}</button>
}
}