175 lines
9.4 KiB
Rust
175 lines
9.4 KiB
Rust
//! # Rust Letter Frontend
|
|
//!
|
|
//! Rendering functions for the site using [Dioxus](https://dioxuslabs.com/).
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
mod components;
|
|
pub mod utils;
|
|
|
|
/// A module that handles the functions needed
|
|
/// to render the site.
|
|
pub mod void_app {
|
|
|
|
// import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
|
|
pub use dioxus::prelude::*;
|
|
|
|
use crate::components::void_buttons::*;
|
|
use crate::components::void_content::*;
|
|
use crate::components::void_footer::*;
|
|
use crate::components::void_page::PageBase;
|
|
use crate::components::void_poem::*;
|
|
use crate::components::void_title::*;
|
|
use crate::utils::helpers;
|
|
use crate::utils::prop_structs::*;
|
|
use crate::utils::user_prefs::*;
|
|
|
|
#[cfg(any(target_family = "wasm"))]
|
|
use dioxus_helmet::Helmet;
|
|
#[cfg(any(target_family = "wasm"))]
|
|
use dioxus_router::{Link, Redirect, Route, Router};
|
|
#[cfg(any(target_family = "wasm"))]
|
|
use dioxus_use_storage::use_local_storage;
|
|
|
|
#[cfg(target_family = "wasm")]
|
|
pub fn DioxusApp(cx: Scope) -> Element {
|
|
let mut poem_database = PoemDatabase::new();
|
|
poem_database.build_poem_database();
|
|
|
|
let user_prefs = UserPrefs::new(ThemePref::Auto, FontPref::OpenDyslexic);
|
|
|
|
cx.render(rsx! {
|
|
Router {
|
|
Route { to: "/", HomePage { slug: "/".to_string(), poem_database: poem_database.clone(), user_prefs: user_prefs.clone(), } }
|
|
Route { to: "/poems", PoemListPage { slug: "/poems".to_string(), poem_database: poem_database.clone(), user_prefs: user_prefs.clone(), } }
|
|
Route { to: "/poems/:slug", PoemPage { slug: "".to_string(), poem_database: poem_database.clone(), user_prefs: user_prefs.clone(), } }
|
|
Route { to: "/settings", SettingsPage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() } }
|
|
Route { to: "/settings/dark", SettingsPage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() } }
|
|
Route { to: "/settings/font", SettingsPage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() } }
|
|
Route { to: "", PageNotFound {} }
|
|
}
|
|
})
|
|
}
|
|
|
|
#[cfg(target_family = "wasm")]
|
|
fn PageNotFound(cx: Scope) -> Element {
|
|
cx.render(rsx! {
|
|
p { "That page doesn't exist, sorry!" }
|
|
Redirect { to: "/" }
|
|
})
|
|
}
|
|
|
|
/// Renders the app and returns the rendered Element.
|
|
pub fn HomePage(cx: Scope<VoidProps>) -> Element {
|
|
let poem_database = &cx.props.poem_database;
|
|
let user_prefs = cx.props.user_prefs.clone();
|
|
let (user_theme, user_font) = user_prefs.get_pref_classes(ThemedComponent::Page);
|
|
let page_title = "A Letter to the Void".to_string();
|
|
cx.render(rsx!{
|
|
div { class: "{user_theme} {user_font}",
|
|
PageBase {
|
|
Title { title: page_title, is_html: false, user_prefs: user_prefs.clone() }
|
|
RenderContent { content: helpers::get_homepage_paragraph(), user_prefs: user_prefs.clone() }
|
|
ButtonGroup {
|
|
NavigationButton { title: "See Latest Entry".to_string(), slug: poem_database.get_latest_entry("".to_string()), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "See Oldest Entry".to_string(), slug: poem_database.get_oldest_entry("".to_string()), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "See A Random Entry".to_string(), slug: poem_database.get_random_entry(), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "See All Entries".to_string(), slug: "/poems".to_string(), user_prefs: user_prefs.clone() }
|
|
}
|
|
Footer { theme: user_prefs.clone().get_theme(), font: user_prefs.get_font() }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/// Renders the app and returns the rendered Element.
|
|
pub fn PoemListPage(cx: Scope<VoidProps>) -> Element {
|
|
let poem_database = &cx.props.poem_database;
|
|
let user_prefs = cx.props.user_prefs.clone();
|
|
let (user_theme, user_font) = user_prefs.get_pref_classes(ThemedComponent::Page);
|
|
cx.render(rsx! {
|
|
div { class: "{user_theme} {user_font}",
|
|
PageBase {
|
|
Title { title: "A Letter to the Void".to_string(), is_html: false, user_prefs: user_prefs.clone() }
|
|
BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() }
|
|
PoemList { poem_database: poem_database.clone(), user_prefs: user_prefs.clone() }
|
|
BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() }
|
|
Footer { theme: user_prefs.clone().get_theme(), font: user_prefs.get_font() }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
pub fn PoemPage(cx: Scope<VoidProps>) -> Element {
|
|
let poem_database = &cx.props.poem_database;
|
|
let user_prefs = cx.props.user_prefs.clone();
|
|
let (user_theme, user_font) = user_prefs.get_pref_classes(ThemedComponent::Page);
|
|
#[cfg(any(target_family = "unix", target_family = "windows"))]
|
|
let slug = &cx
|
|
.props
|
|
.slug
|
|
.as_ref()
|
|
.expect("A slug was given in the pops.")
|
|
.clone();
|
|
#[cfg(target_family = "wasm")]
|
|
let slug = String::from(
|
|
dioxus_router::use_route(cx)
|
|
.segment("slug")
|
|
.expect("No slug specified."),
|
|
);
|
|
cx.render(rsx!{
|
|
div { class: "{user_theme} {user_font}",
|
|
PageBase {
|
|
BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() }
|
|
GetPoem { slug: slug.clone(), poem_database: poem_database.clone(), user_prefs: user_prefs.clone() }
|
|
ButtonGroup {
|
|
NavigationButton { title: "Oldest".to_string(), slug: poem_database.get_oldest_entry(slug.clone()), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "Previous".to_string(), slug: poem_database.get_previous_entry(slug.clone()), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "Random".to_string(), slug: poem_database.get_random_entry(), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "Next".to_string(), slug: poem_database.get_next_entry(slug.clone()), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "Latest".to_string(), slug: poem_database.get_latest_entry(slug.clone()), user_prefs: user_prefs.clone() }
|
|
}
|
|
BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() }
|
|
Footer { theme: user_prefs.clone().get_theme(), font: user_prefs.get_font() }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
pub fn SettingsPage(cx: Scope<UserPrefs>) -> Element {
|
|
let user_prefs = cx.props.clone();
|
|
let (user_theme, user_font) = user_prefs.get_pref_classes(ThemedComponent::Page);
|
|
// Get rid of this and create a general card component with children.
|
|
let (user_theme_card, user_font_card) = user_prefs.get_pref_classes(ThemedComponent::Card);
|
|
cx.render(rsx! {
|
|
div { class: "{user_theme} {user_font}",
|
|
PageBase {
|
|
Title { title: "Settings".to_string(), is_html: false, user_prefs: user_prefs.clone() }
|
|
BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() }
|
|
div { class: "p-4 flex flex-col space-y-4 mx-auto max-w-full justify-center",
|
|
div { class: "p-4 flex flex-col space-y-4 text-xl text-center ring-4 {user_theme_card} {user_font_card}",
|
|
p {
|
|
"Theme"
|
|
}
|
|
ButtonGroup {
|
|
NavigationButton { title: "Light".to_string(), slug: "/settings/?theme=light".to_string(), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "Dark".to_string(), slug: "/settings/?theme=dark".to_string(), user_prefs: user_prefs.clone() }
|
|
NavigationButton { title: "Auto".to_string(), slug: "/settings/?theme=auto".to_string(), user_prefs: user_prefs.clone() }
|
|
}
|
|
}
|
|
div { class: "p-4 flex flex-col space-y-4 text-xl text-center ring-4 {user_theme_card} {user_font_card}",
|
|
p {
|
|
"Font"
|
|
}
|
|
ButtonGroup {
|
|
NavigationButton { title: "Nerd Font".to_string(), slug: "/settings/?font=nerd".to_string(), user_prefs: user_prefs.clone() override_font: "font-nerd".to_string() }
|
|
NavigationButton { title: "Open Dyslexic".to_string(), slug: "/settings/?font=open".to_string(), user_prefs: user_prefs.clone() override_font: "font-open".to_string() }
|
|
}
|
|
}
|
|
}
|
|
BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.get_font() }
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|