render refactor

This commit is contained in:
Zynh Ludwig 2024-08-28 18:45:12 -07:00
parent 3bb1d11f8c
commit 391f9cfa01
2 changed files with 43 additions and 45 deletions

View file

@ -30,6 +30,7 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod cache;
mod nyazoom_headers;
pub mod ssr;
mod state;
mod util;
mod views;
@ -135,12 +136,11 @@ async fn remaining(
async fn welcome() -> impl IntoResponse {
let cat_fact = views::get_cat_fact().await;
Html(
leptos::ssr::render_to_string(move || {
leptos::view! { <Welcome fact=cat_fact /> }
})
.to_string(),
)
Html(ssr::render(move || {
leptos::view! {
<Welcome fact=cat_fact />
}
}))
}
async fn records(State(state): State<AppState>) -> impl IntoResponse {
@ -151,8 +151,7 @@ async fn records(State(state): State<AppState>) -> impl IntoResponse {
// this behind some kind of authentication
async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone();
Html(
leptos::ssr::render_to_string(move || {
Html(ssr::render(move || {
leptos::view! {
<HtmxPage>
<div class="form-wrapper">
@ -173,9 +172,7 @@ async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
</div>
</HtmxPage>
}
})
.to_string(),
)
}))
}
async fn link(
@ -190,15 +187,12 @@ async fn link(
.filter(|record| record.can_be_downloaded())
{
if record.can_be_downloaded() {
return Ok(Html(
leptos::ssr::render_to_string({
return Ok(Html(ssr::render({
let record = record.clone();
|| {
leptos::view! { <DownloadLinkPage id=id record=record /> }
}
})
.into(),
));
})));
}
}
}
@ -294,12 +288,9 @@ async fn upload_to_zip(
.status(200)
.header("Content-Type", "text/html")
.header("HX-Push-Url", format!("/link/{}", &id))
.body(
leptos::ssr::render_to_string(|| {
.body(ssr::render(|| {
leptos::view! { <LinkView id record /> }
})
.into(),
)
}))
.unwrap();
Ok(response)

7
src/ssr.rs Normal file
View file

@ -0,0 +1,7 @@
pub fn render<F, N>(f: F) -> String
where
F: FnOnce() -> N + 'static,
N: leptos::IntoView,
{
leptos::ssr::render_to_string(f).to_string()
}