Skip to content

Commit

Permalink
Use <Suspense> and IntoView
Browse files Browse the repository at this point in the history
- <Await> doesn't handle errors, so use <Suspense> in conjunction with
  <ErrorBoundary>
- implement IntoView for data types, so they can be directly rendered.

Introduce nix::info module, containing only nix information. We'll add
other nix::* modules latter.
  • Loading branch information
srid committed Aug 7, 2023
1 parent eea34f0 commit 50f3efb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
29 changes: 20 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::nix;
use crate::nix::info::get_nix_info;
use cfg_if::cfg_if;
#[cfg(feature = "ssr")]
use http::status::StatusCode;
Expand Down Expand Up @@ -33,6 +33,7 @@ pub fn App(cx: Scope) -> impl IntoView {
/// Home page
#[component]
fn Home(cx: Scope) -> impl IntoView {
let nix_info = create_resource(cx, || (), |_| async { get_nix_info().await });
view! { cx,
<div class="grid w-full min-h-screen bg-center bg-cover bg-base-200 place-items-center">
<div class="z-0 flex items-center justify-center col-start-1 row-start-1 text-center">
Expand All @@ -41,12 +42,15 @@ fn Home(cx: Scope) -> impl IntoView {
<p class="py-6">
<h2 class="text-3xl font-bold text-gray-500">"Nix Info"</h2>
<p class="my-1"><pre>
<Await
future=|_| nix::nix_info()
bind:data
>
{format!("{data:?}")}
</Await>
<Suspense fallback=move || view! {cx, <p>"Loading nix-info"</p> }>
<ErrorBoundary
fallback=|cx, errors| view! { cx,
<ErrorMessage message=format!("Error loading nix-info: {:?}", errors) />
}
>
<div>{nix_info.read(cx)}</div>
</ErrorBoundary>
</Suspense>
</pre></p>
</p>
<Link link="https://github.com/juspay/nix-browser" text="Source Code" rel="external" />
Expand All @@ -72,9 +76,16 @@ fn Link(
/// 404 page
#[component]
fn NotFound(cx: Scope) -> impl IntoView {
view! {cx,
<ErrorMessage message="404: Page not found" />
}
}

#[component]
fn ErrorMessage<S: Into<String>>(cx: Scope, message: S) -> impl IntoView {
view! { cx,
<div class="flex flex-row justify-center text-3xl text-error-500">
"404: Page not found"
<div class="flex flex-row justify-center text-3xl text-white bg-error-500">
{message.into()}
</div>
}
}
8 changes: 0 additions & 8 deletions src/nix.rs

This file was deleted.

31 changes: 31 additions & 0 deletions src/nix/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use leptos::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// Information about the user's Nix installation
pub struct NixInfo {
/// Nix version string
pub nix_version: String,
}

#[server(GetNixInfo, "/api")]
pub async fn get_nix_info() -> Result<NixInfo, ServerFnError> {
use tokio::process::Command;
let out = Command::new("nix").arg("--version").output().await?.stdout;
// TODO: Parse the version string
let nix_version = String::from_utf8(out)
.map_err(|e| <std::string::FromUtf8Error as Into<ServerFnError>>::into(e))?;
Ok(NixInfo { nix_version })
}

impl IntoView for NixInfo {
fn into_view(self, cx: Scope) -> View {
view! {cx,
<div class="p-2 bg-blue-100 border-2 border-black rounded shadow-md">
<b>Nix Version:</b>
<pre>{self.nix_version}</pre>
</div>
}
.into_view(cx)
}
}
1 change: 1 addition & 0 deletions src/nix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod info;

0 comments on commit 50f3efb

Please sign in to comment.