From e593f46a355361c1aea7da37c672c87a9c0b1ea9 Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Tue, 18 Apr 2023 19:33:44 -0500 Subject: [PATCH] Refactored poem selection so that a static hashmap and vector are ceated as a prop that is passed to each component in order to grab poem data more efficiently and cleanly. --- Cargo.toml | 3 +- public/styles/tailwind.min.css | 2 +- void-be/Cargo.toml | 1 + void-be/src/lib.rs | 45 +++- .../data/poems/2021-02-27_audio-reading.md | 2 +- void-fe/data/poems/2021-02-28_iced-coffee.md | 2 +- void-fe/src/components/void_footer.rs | 9 +- void-fe/src/components/void_page.rs | 7 +- void-fe/src/components/void_poem.rs | 48 ++-- void-fe/src/lib.rs | 39 +-- void-fe/src/utils/helpers.rs | 248 ++++++++++-------- void-fe/src/utils/prop_structs.rs | 23 +- 12 files changed, 257 insertions(+), 172 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f2a8e09..51dc85f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,8 @@ dioxus = "0.3.2" markdown = "1.0.0-alpha.7" dioxus-ssr = "0.3.0" rust-embed = { version = "6.6.1" } +once_cell = "1.17.1" [dependencies] void-be = { path = "./void-be" } -rocket = { workspace = true } \ No newline at end of file +rocket = { workspace = true } diff --git a/public/styles/tailwind.min.css b/public/styles/tailwind.min.css index a733a77..933c7a2 100644 --- a/public/styles/tailwind.min.css +++ b/public/styles/tailwind.min.css @@ -1 +1 @@ -/*! tailwindcss v3.2.7 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.container{width:100%}@media (min-width:640px){.container{max-width:640px}}@media (min-width:768px){.container{max-width:768px}}@media (min-width:1024px){.container{max-width:1024px}}@media (min-width:1280px){.container{max-width:1280px}}@media (min-width:1536px){.container{max-width:1536px}}.mx-auto{margin-left:auto;margin-right:auto}.ml-2{margin-left:.5rem}.mr-2{margin-right:.5rem}.flex{display:flex}.min-h-screen{min-height:100vh}.max-w-fit{max-width:-moz-fit-content;max-width:fit-content}.max-w-full{max-width:100%}.basis-full{flex-basis:100%}.flex-row{flex-direction:row}.flex-col{flex-direction:column}.justify-center{justify-content:center}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem*var(--tw-space-y-reverse))}.bg-alice-werefox-grey{--tw-bg-opacity:1;background-color:rgb(36 36 36/var(--tw-bg-opacity))}.bg-alice-werefox-grey-dark{--tw-bg-opacity:1;background-color:rgb(18 18 18/var(--tw-bg-opacity))}.bg-alice-werefox-grey-light{--tw-bg-opacity:1;background-color:rgb(204 204 204/var(--tw-bg-opacity))}.bg-alice-werefox-grey-lightest{--tw-bg-opacity:1;background-color:rgb(238 238 238/var(--tw-bg-opacity))}.p-2{padding:.5rem}.p-4{padding:1rem}.py-4{padding-top:1rem;padding-bottom:1rem}.text-center{text-align:center}.align-middle{vertical-align:middle}.font-nerd{font-family:DejaVuSansMono}.font-open{font-family:OpenDyslexic}.text-lg{font-size:1.125rem}.text-lg,.text-xl{line-height:1.75rem}.text-xl{font-size:1.25rem}.text-alice-werefox-grey-dark{--tw-text-opacity:1;color:rgb(18 18 18/var(--tw-text-opacity))}.text-alice-werefox-grey-light{--tw-text-opacity:1;color:rgb(204 204 204/var(--tw-text-opacity))}.ring-2{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color)}.ring-2,.ring-4{box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.ring-4{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(4px + var(--tw-ring-offset-width)) var(--tw-ring-color)}.ring-alice-werefox-red{--tw-ring-opacity:1;--tw-ring-color:rgb(201 52 57/var(--tw-ring-opacity))}.ring-alice-werefox-red-dark{--tw-ring-opacity:1;--tw-ring-color:rgb(128 0 8/var(--tw-ring-opacity))}.transition{transition-property:color,background-color,border-color,text-decoration-color,fill,stroke,opacity,box-shadow,transform,filter,-webkit-backdrop-filter;transition-property:color,background-color,border-color,text-decoration-color,fill,stroke,opacity,box-shadow,transform,filter,backdrop-filter;transition-property:color,background-color,border-color,text-decoration-color,fill,stroke,opacity,box-shadow,transform,filter,backdrop-filter,-webkit-backdrop-filter;transition-timing-function:cubic-bezier(.4,0,.2,1);transition-duration:.15s}@font-face{font-family:OpenDyslexic;src:url(/fonts/OpenDyslexic-Regular.otf)}@font-face{font-family:DejaVuSansMono;src:url(/fonts/DejaVuSansMono.ttf)}.before\:content-\[\'Open\'\]:before{--tw-content:"Open";content:var(--tw-content)}@keyframes yip{0%,to{transform:scale(1)}50%{transform:scale(1.01)}}.hover\:animate-yip:hover{animation:yip .1s ease-in-out}.hover\:text-alice-werefox-blue-dark:hover{--tw-text-opacity:1;color:rgb(51 0 255/var(--tw-text-opacity))}.hover\:text-alice-werefox-blue-light:hover{--tw-text-opacity:1;color:rgb(145 151 243/var(--tw-text-opacity))}.hover\:ring-alice-werefox-blue:hover{--tw-ring-opacity:1;--tw-ring-color:rgb(27 41 224/var(--tw-ring-opacity))}.group[open] .group-open\:before\:content-\[\'Close\'\]:before{--tw-content:"Close";content:var(--tw-content)}@media (prefers-color-scheme:dark){.dark\:bg-alice-werefox-grey{--tw-bg-opacity:1;background-color:rgb(36 36 36/var(--tw-bg-opacity))}.dark\:bg-alice-werefox-grey-dark{--tw-bg-opacity:1;background-color:rgb(18 18 18/var(--tw-bg-opacity))}.dark\:text-alice-werefox-grey-light{--tw-text-opacity:1;color:rgb(204 204 204/var(--tw-text-opacity))}.dark\:ring-alice-werefox-red{--tw-ring-opacity:1;--tw-ring-color:rgb(201 52 57/var(--tw-ring-opacity))}.dark\:hover\:text-alice-werefox-blue-light:hover{--tw-text-opacity:1;color:rgb(145 151 243/var(--tw-text-opacity))}.dark\:hover\:ring-alice-werefox-blue:hover{--tw-ring-opacity:1;--tw-ring-color:rgb(27 41 224/var(--tw-ring-opacity))}}@media (min-width:768px){.md\:flex-row{flex-direction:row}.md\:space-y-0>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(0px*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(0px*var(--tw-space-y-reverse))}.md\:pl-8{padding-left:2rem}.md\:pr-8{padding-right:2rem}} \ No newline at end of file +/*! tailwindcss v3.3.0 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.container{width:100%}@media (min-width:640px){.container{max-width:640px}}@media (min-width:768px){.container{max-width:768px}}@media (min-width:1024px){.container{max-width:1024px}}@media (min-width:1280px){.container{max-width:1280px}}@media (min-width:1536px){.container{max-width:1536px}}.mx-auto{margin-left:auto;margin-right:auto}.ml-2{margin-left:.5rem}.mr-2{margin-right:.5rem}.flex{display:flex}.contents{display:contents}.hidden{display:none}.min-h-screen{min-height:100vh}.max-w-fit{max-width:-moz-fit-content;max-width:fit-content}.max-w-full{max-width:100%}.basis-full{flex-basis:100%}.flex-row{flex-direction:row}.flex-col{flex-direction:column}.justify-center{justify-content:center}.space-y-4>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(1rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1rem*var(--tw-space-y-reverse))}.bg-alice-werefox-grey{--tw-bg-opacity:1;background-color:rgb(36 36 36/var(--tw-bg-opacity))}.bg-alice-werefox-grey-dark{--tw-bg-opacity:1;background-color:rgb(18 18 18/var(--tw-bg-opacity))}.bg-alice-werefox-grey-light{--tw-bg-opacity:1;background-color:rgb(204 204 204/var(--tw-bg-opacity))}.bg-alice-werefox-grey-lightest{--tw-bg-opacity:1;background-color:rgb(238 238 238/var(--tw-bg-opacity))}.p-2{padding:.5rem}.p-4{padding:1rem}.py-4{padding-top:1rem;padding-bottom:1rem}.text-center{text-align:center}.align-middle{vertical-align:middle}.font-nerd{font-family:DejaVuSansMono}.font-open{font-family:OpenDyslexic}.text-lg{font-size:1.125rem}.text-lg,.text-xl{line-height:1.75rem}.text-xl{font-size:1.25rem}.text-alice-werefox-grey-dark{--tw-text-opacity:1;color:rgb(18 18 18/var(--tw-text-opacity))}.text-alice-werefox-grey-light{--tw-text-opacity:1;color:rgb(204 204 204/var(--tw-text-opacity))}.ring-2{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color)}.ring-2,.ring-4{box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow,0 0 #0000)}.ring-4{--tw-ring-offset-shadow:var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow:var(--tw-ring-inset) 0 0 0 calc(4px + var(--tw-ring-offset-width)) var(--tw-ring-color)}.ring-alice-werefox-red{--tw-ring-opacity:1;--tw-ring-color:rgb(201 52 57/var(--tw-ring-opacity))}.ring-alice-werefox-red-dark{--tw-ring-opacity:1;--tw-ring-color:rgb(128 0 8/var(--tw-ring-opacity))}.transition{transition-property:color,background-color,border-color,text-decoration-color,fill,stroke,opacity,box-shadow,transform,filter,-webkit-backdrop-filter;transition-property:color,background-color,border-color,text-decoration-color,fill,stroke,opacity,box-shadow,transform,filter,backdrop-filter;transition-property:color,background-color,border-color,text-decoration-color,fill,stroke,opacity,box-shadow,transform,filter,backdrop-filter,-webkit-backdrop-filter;transition-timing-function:cubic-bezier(.4,0,.2,1);transition-duration:.15s}@font-face{font-family:OpenDyslexic;src:url(/fonts/OpenDyslexic-Regular.otf)}@font-face{font-family:DejaVuSansMono;src:url(/fonts/DejaVuSansMono.ttf)}.before\:content-\[\'Open\'\]:before{--tw-content:"Open";content:var(--tw-content)}@keyframes yip{0%,to{transform:scale(1)}50%{transform:scale(1.01)}}.hover\:animate-yip:hover{animation:yip .1s ease-in-out}.hover\:text-alice-werefox-blue-dark:hover{--tw-text-opacity:1;color:rgb(51 0 255/var(--tw-text-opacity))}.hover\:text-alice-werefox-blue-light:hover{--tw-text-opacity:1;color:rgb(145 151 243/var(--tw-text-opacity))}.hover\:ring-alice-werefox-blue:hover{--tw-ring-opacity:1;--tw-ring-color:rgb(27 41 224/var(--tw-ring-opacity))}.group[open] .group-open\:before\:content-\[\'Close\'\]:before{--tw-content:"Close";content:var(--tw-content)}@media (prefers-color-scheme:dark){.dark\:bg-alice-werefox-grey{--tw-bg-opacity:1;background-color:rgb(36 36 36/var(--tw-bg-opacity))}.dark\:bg-alice-werefox-grey-dark{--tw-bg-opacity:1;background-color:rgb(18 18 18/var(--tw-bg-opacity))}.dark\:text-alice-werefox-grey-light{--tw-text-opacity:1;color:rgb(204 204 204/var(--tw-text-opacity))}.dark\:ring-alice-werefox-red{--tw-ring-opacity:1;--tw-ring-color:rgb(201 52 57/var(--tw-ring-opacity))}.dark\:hover\:text-alice-werefox-blue-light:hover{--tw-text-opacity:1;color:rgb(145 151 243/var(--tw-text-opacity))}.dark\:hover\:ring-alice-werefox-blue:hover{--tw-ring-opacity:1;--tw-ring-color:rgb(27 41 224/var(--tw-ring-opacity))}}@media (min-width:768px){.md\:flex-row{flex-direction:row}.md\:space-y-0>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(0px*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(0px*var(--tw-space-y-reverse))}.md\:pl-8{padding-left:2rem}.md\:pr-8{padding-right:2rem}} \ No newline at end of file diff --git a/void-be/Cargo.toml b/void-be/Cargo.toml index 10cad9b..04a94c5 100644 --- a/void-be/Cargo.toml +++ b/void-be/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] void-fe = { path = "../void-fe" } dioxus-ssr = { workspace = true } +once_cell = { workspace = true } # If you're unsure about why we're depending on a release candidate, # check the Rocket documentation: diff --git a/void-be/src/lib.rs b/void-be/src/lib.rs index 0a243eb..badca77 100644 --- a/void-be/src/lib.rs +++ b/void-be/src/lib.rs @@ -8,15 +8,17 @@ extern crate rocket; /// A module that handles the backend for the site. pub mod web_app_backend { + use once_cell::sync::OnceCell; use rocket::fs::FileServer; use rocket::http::{Cookie, CookieJar}; use rocket::response::Redirect; use rocket::{Build, Rocket}; use rocket_dyn_templates::{context, Template}; - use void_fe::utils::prop_structs::PoemRequest; + use void_fe::utils::prop_structs::{PoemDatabase, VoidProps}; + use void_fe::utils::user_prefs::*; use void_fe::void_app::{self, VirtualDom}; - use void_fe::utils::user_prefs::*; + static POEM_DATABASE: OnceCell = OnceCell::new(); async fn get_user_prefs(cookies: &CookieJar<'_>) -> UserPrefs { let user_theme = match cookies.get("theme") { @@ -74,7 +76,15 @@ pub mod web_app_backend { #[get("/")] async fn index(cookies: &CookieJar<'_>) -> Template { let user_prefs = get_user_prefs(cookies).await; - let mut vdom = VirtualDom::new_with_props(void_app::HomePage, user_prefs); + let void_props = VoidProps { + slug: None, + poem_database: POEM_DATABASE + .get() + .expect("Poem database is not initialized") + .clone(), + user_prefs: user_prefs, + }; + let mut vdom = VirtualDom::new_with_props(void_app::HomePage, void_props); let _ = vdom.rebuild(); let output = dioxus_ssr::render(&vdom); Template::render( @@ -120,7 +130,15 @@ pub mod web_app_backend { #[get("/")] async fn poem_list(cookies: &CookieJar<'_>) -> Template { let user_prefs = get_user_prefs(cookies).await; - let mut vdom = VirtualDom::new_with_props(void_app::PoemListPage, user_prefs); + let void_props = VoidProps { + slug: None, + poem_database: POEM_DATABASE + .get() + .expect("Poem database is not initialized") + .clone(), + user_prefs: user_prefs, + }; + let mut vdom = VirtualDom::new_with_props(void_app::PoemListPage, void_props); let _ = vdom.rebuild(); let output = dioxus_ssr::render(&vdom); Template::render( @@ -137,13 +155,15 @@ pub mod web_app_backend { #[get("/")] async fn poem(cookies: &CookieJar<'_>, entry: &str) -> Template { let user_prefs = get_user_prefs(cookies).await; - let mut vdom = VirtualDom::new_with_props( - void_app::PoemPage, - PoemRequest { - slug: format!("{entry}"), - user_prefs, - }, - ); + let void_props = VoidProps { + slug: Some(entry.to_string()), + poem_database: POEM_DATABASE + .get() + .expect("Poem database is not initialized") + .clone(), + user_prefs: user_prefs, + }; + let mut vdom = VirtualDom::new_with_props(void_app::PoemPage, void_props); let _ = vdom.rebuild(); let output = dioxus_ssr::render(&vdom); Template::render( @@ -159,6 +179,9 @@ pub mod web_app_backend { /// This runs `rocket::build()` with the needed mounts and routes. pub async fn build_rocket() -> Rocket { + let mut poem_database = PoemDatabase::new(); + poem_database.build_poem_database().await; + POEM_DATABASE.set(poem_database).expect("Could not initialize poem database."); rocket::build() .mount("/images", FileServer::from("public/images")) .mount("/styles", FileServer::from("public/styles")) diff --git a/void-fe/data/poems/2021-02-27_audio-reading.md b/void-fe/data/poems/2021-02-27_audio-reading.md index a98c1c3..8503ba5 100644 --- a/void-fe/data/poems/2021-02-27_audio-reading.md +++ b/void-fe/data/poems/2021-02-27_audio-reading.md @@ -1,5 +1,5 @@ # im on repeat -[Audio reading](https://cloud.werefox.dev/s/dq5ccm5QqmMF4GB) +*Author's note: Unfortunately, I've lost the audio reading for this.* Death is like a memory. Ceaseless, and comforting\ Knowing things will end has always helped me feel like I'm free\ diff --git a/void-fe/data/poems/2021-02-28_iced-coffee.md b/void-fe/data/poems/2021-02-28_iced-coffee.md index 3f97b03..4856c84 100644 --- a/void-fe/data/poems/2021-02-28_iced-coffee.md +++ b/void-fe/data/poems/2021-02-28_iced-coffee.md @@ -1,5 +1,5 @@ # Iced Coffee -[Listen to the song on Soundcloud](https://soundcloud.com/alexis-werefox/iced-coffee/s-5SgBwPrLwyR) +Listen to the song on Soundcloud *Iced coffee on a cold Winter's day* \ *Iced coffee bittersweet at the taste* \ diff --git a/void-fe/src/components/void_footer.rs b/void-fe/src/components/void_footer.rs index f675e58..ff08690 100644 --- a/void-fe/src/components/void_footer.rs +++ b/void-fe/src/components/void_footer.rs @@ -1,5 +1,5 @@ use dioxus::prelude::*; -use crate::{components::void_buttons::NavigationButton, utils::user_prefs::{UserPrefs, ThemedComponent}}; +use crate::{components::void_buttons::{ButtonGroup, NavigationButton}, utils::user_prefs::{UserPrefs, ThemedComponent}}; pub fn Footer(cx: Scope) -> Element { cx.render(rsx! { @@ -13,7 +13,12 @@ fn MutantStandardFooter(cx: Scope) -> Element { let user_font = cx.props.get_font_class(); cx.render(rsx!{ div { class: "p-4 flex flex-col space-y-4 mx-auto max-w-full justify-center ring-4 {user_theme} {user_font}", - NavigationButton { title: "⚙️ Settings".to_string(), slug: "/settings".to_string(), user_prefs: user_prefs } + ButtonGroup{ + NavigationButton { title: "⚙️ Settings".to_string(), slug: "/settings".to_string(), user_prefs: user_prefs.clone() } + span { class: "font-nerd contents", + NavigationButton { title: " /src".to_string(), slug: "https://gitea.werefox.cafe/ada/void-werefox-cafe".to_string(), user_prefs: user_prefs } + } + } div { class: "flex mx-auto max-w-full justify-center text-md text-center", "This site uses Mutant Standard emoji, which are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License" } diff --git a/void-fe/src/components/void_page.rs b/void-fe/src/components/void_page.rs index dd68e39..624d3ad 100644 --- a/void-fe/src/components/void_page.rs +++ b/void-fe/src/components/void_page.rs @@ -2,11 +2,10 @@ use crate::utils::prop_structs::PageChildren; use dioxus::prelude::*; pub fn PageBase<'a>(cx: Scope<'a, PageChildren<'a>>) -> Element { - cx.render(rsx!{ + cx.render(rsx! { + a { rel: "me", href: "https://yiff.life/@werefox", hidden: true, "Mastodon" } div { class: "min-h-screen", - div { class: "container space-y-4 mx-auto p-4", - &cx.props.children - } + div { class: "container space-y-4 mx-auto p-4", &cx.props.children } } }) } diff --git a/void-fe/src/components/void_poem.rs b/void-fe/src/components/void_poem.rs index a9790dd..cecd1ec 100644 --- a/void-fe/src/components/void_poem.rs +++ b/void-fe/src/components/void_poem.rs @@ -1,17 +1,17 @@ use crate::components::void_buttons::*; use crate::components::void_title::*; -use crate::utils::{helpers, prop_structs::{PoemChildren, PoemData}, user_prefs::{UserPrefs, ThemedComponent} }; +use crate::utils::prop_structs::VoidProps; +use crate::utils::{prop_structs::PoemChildren, user_prefs::ThemedComponent}; use dioxus::prelude::*; -pub fn PoemList(cx: Scope) -> Element { - let poem_list = helpers::get_poem_list(); +pub fn PoemList(cx: Scope) -> Element { + let poem_list = cx.props.poem_database.get_poem_list(); cx.render(rsx! { ul { class: "flex flex-col space-y-4", poem_list.into_iter().map(|p| { - let user_prefs = cx.props.clone(); - let slug = format!("/poems/{}", p.1); + let slug = format!("/poems/{}", p.0); rsx!{ - NavigationButton { title: p.0, slug: slug, user_prefs: user_prefs.clone() } + NavigationButton { title: p.1, slug: slug, user_prefs: cx.props.user_prefs.clone() } } }) } @@ -26,25 +26,41 @@ pub fn MakePoem<'a>(cx: Scope<'a, PoemChildren<'a>>) -> Element { }) } -pub fn GetPoem(cx: Scope) -> Element { - let slug = String::from(cx.props.slug.clone().expect("No slug specified.")); - let (title, content, creation_date) = helpers::get_poem(slug.clone()); +pub fn GetPoem(cx: Scope) -> Element { + let poem_database = &cx.props.poem_database; + let slug = String::from(&cx.props.slug.clone().expect("No slug specified.")); + let poem_struct = poem_database.get_poem(slug.clone()); cx.render(rsx! { - Title { title: title, is_html: true, user_prefs: cx.props.user_prefs.clone() } + Title { title: poem_struct.title.clone(), is_html: true, user_prefs: cx.props.user_prefs.clone() } MakePoem{ - PoemContent { content: content, creation_date: creation_date, user_prefs: cx.props.user_prefs.clone() } + PoemContent { slug: slug, poem_database: poem_database.clone(), user_prefs: cx.props.user_prefs.clone() } } }) } -pub fn PoemContent(cx: Scope) -> Element { +pub fn PoemContent(cx: Scope) -> Element { let (user_theme, user_font) = cx.props.user_prefs.get_pref_classes(ThemedComponent::Card); - let content = cx.props.content.clone().expect("No content specified."); - let creation_date = cx + let slug = cx .props + .slug + .as_ref() + .expect("Received slug for poem selection."); + let content = &cx + .props + .poem_database + .poem_hashmap + .get(slug) + .expect("Grabbed poem stuct from databse.") + .content + .clone(); + let creation_date = &cx + .props + .poem_database + .poem_hashmap + .get(slug) + .expect("Grabbed poem struct from database.") .creation_date - .clone() - .expect("No creation date specified."); + .clone(); #[cfg(any(target_family = "unix", target_family = "windows"))] return cx.render(rsx! { div { class: "flex p-2 mx-auto max-w-full justify-center", diff --git a/void-fe/src/lib.rs b/void-fe/src/lib.rs index 6e2e81f..ab52234 100644 --- a/void-fe/src/lib.rs +++ b/void-fe/src/lib.rs @@ -62,19 +62,20 @@ pub mod void_app { } /// Renders the app and returns the rendered Element. - pub fn HomePage(cx: Scope) -> Element { - let user_prefs = cx.props.clone(); + pub fn HomePage(cx: Scope) -> 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 title = "A Letter to the Void".to_string(); + let page_title = "A Letter to the Void".to_string(); cx.render(rsx!{ div { class: "{user_theme} {user_font}", PageBase { - Title { title: title, is_html: false, user_prefs: user_prefs.clone() } + 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: helpers::get_latest_entry("".to_string()), user_prefs: user_prefs.clone() } - NavigationButton { title: "See Oldest Entry".to_string(), slug: helpers::get_oldest_entry("".to_string()), user_prefs: user_prefs.clone() } - NavigationButton { title: "See A Random Entry".to_string(), slug: helpers::get_random_entry(), user_prefs: user_prefs.clone() } + 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() } @@ -84,15 +85,16 @@ pub mod void_app { } /// Renders the app and returns the rendered Element. - pub fn PoemListPage(cx: Scope) -> Element { - let user_prefs = cx.props.clone(); + pub fn PoemListPage(cx: Scope) -> 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 { 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() } } @@ -100,11 +102,12 @@ pub mod void_app { }) } - pub fn PoemPage(cx: Scope) -> Element { + pub fn PoemPage(cx: Scope) -> 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.clone(); + 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) @@ -115,13 +118,13 @@ pub mod void_app { div { class: "{user_theme} {user_font}", PageBase { BackToHomePage { theme: user_prefs.clone().get_theme(), font: user_prefs.clone().get_font() } - GetPoem { slug: slug.clone(), user_prefs: user_prefs.clone() } + GetPoem { slug: slug.clone(), poem_database: poem_database.clone(), user_prefs: user_prefs.clone() } ButtonGroup { - NavigationButton { title: "Oldest".to_string(), slug: helpers::get_oldest_entry(slug.clone()), user_prefs: user_prefs.clone() } - NavigationButton { title: "Previous".to_string(), slug: helpers::get_previous_entry(slug.clone()), user_prefs: user_prefs.clone() } - NavigationButton { title: "Random".to_string(), slug: helpers::get_random_entry(), user_prefs: user_prefs.clone() } - NavigationButton { title: "Next".to_string(), slug: helpers::get_next_entry(slug.clone()), user_prefs: user_prefs.clone() } - NavigationButton { title: "Latest".to_string(), slug: helpers::get_latest_entry(slug.clone()), user_prefs: user_prefs.clone() } + 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() } diff --git a/void-fe/src/utils/helpers.rs b/void-fe/src/utils/helpers.rs index fd5c6c9..3e2d9a9 100644 --- a/void-fe/src/utils/helpers.rs +++ b/void-fe/src/utils/helpers.rs @@ -1,15 +1,150 @@ use markdown::Options; use rand::seq::SliceRandom; use rust_embed::RustEmbed; -use std::collections::VecDeque; +use std::collections::{HashMap, VecDeque}; +use super::prop_structs::{PoemDatabase, PoemStruct}; #[derive(RustEmbed)] #[folder = "data/other"] struct OtherData; #[derive(RustEmbed)] #[folder = "data/poems"] -struct Poems; +pub struct Poems; + +impl PoemDatabase { + pub fn new() -> PoemDatabase { + PoemDatabase { + poem_list: Vec::<(String, String)>::new(), + poem_hashmap: HashMap::::new(), + } + } + + // There's no need to actually make a database yet, but maybe in the future... + pub async fn build_poem_database(&mut self) { + for p in Poems::iter() { + let filename = p.to_string(); + let poem_content = Poems::get(&filename).expect("Found poem {filename:?}"); + let mut poem_to_str = std::str::from_utf8(poem_content.data.as_ref()) + .expect("Poem is valid UT8.") + .lines(); + let poem_title = poem_to_str.next().expect("No title specified."); + let poem_content = poem_to_str.into_iter().collect::>().join("\n"); + let poem_title_to_html_string = + markdown::to_html_with_options(poem_title, &Options::gfm()).unwrap(); + let poem_content_to_html_string = + markdown::to_html_with_options(poem_content.as_str(), &Options::gfm()).unwrap(); + let mut split_filename = filename.trim_end_matches(".md").split("_"); + let creation_date = split_filename.next().expect("Obtained creation date"); + let slug = split_filename.next().expect("Obtained slug"); + self.poem_list + .push((creation_date.to_string(), slug.to_string())); + let poem_struct = PoemStruct { + title: poem_title_to_html_string.to_string(), + content: poem_content_to_html_string, + creation_date: creation_date.to_string(), + }; + self.poem_hashmap.insert(slug.to_string(), poem_struct); + } + self.poem_list.sort_by_key(|k| k.0.clone()) + } + pub fn get_poem(&self, slug: String) -> PoemStruct { + self.poem_hashmap + .get(slug.as_str()) + .expect("Grabbed poem from database") + .clone() + } + + pub fn get_poem_list(&self) -> Vec<(String, String)> { + self.poem_list + .iter() + .map(|s| { + ( + s.1.clone(), + self.poem_hashmap + .get(&s.1) + .expect("Got poemstruct from database") + .title + .clone(), + ) + }) + .collect::>() + .clone() + } + + pub fn get_oldest_entry(&self, current: String) -> String { + let mut poem_list = VecDeque::from_iter(self.poem_list.iter()); + let oldest = poem_list + .pop_front() + .expect("There is an entry in this list of poems.") + .1 + .clone(); + if current == oldest { + return format!("/poems/{current}#"); + } + format!("/poems/{oldest}") + } + + pub fn get_latest_entry(&self, current: String) -> String { + let mut poem_list = self.poem_list.clone(); + let latest = poem_list + .pop() + .expect("There is an entry in this list of poems.") + .1 + .clone(); + if current == latest { + return format!("/poems/{current}#"); + } + format!("/poems/{latest}") + } + + pub fn get_previous_entry(&self, current: String) -> String { + let poem_list = self.poem_list.clone(); + match poem_list.iter().enumerate().find_map(|(index, p)| { + if p.1 == current { + if index != 0 { + Some(poem_list[index - 1].1.clone()) + } else { + None + } + } else { + None + } + }) { + Some(entry) => format!("/poems/{entry}"), + None => format!("/poems/{current}"), + } + } + + pub fn get_next_entry(&self, current: String) -> String { + let poem_list = self.poem_list.clone(); + match poem_list.iter().enumerate().find_map(|(index, p)| { + if p.1 == current { + if index != poem_list.len() - 1 { + Some(poem_list[index + 1].1.clone()) + } else { + None + } + } else { + None + } + }) { + Some(entry) => format!("/poems/{entry}"), + None => format!("/poems/{current}"), + } + } + + pub fn get_random_entry(&self) -> String { + let poem_list = self.poem_list.clone(); + let mut rng = rand::thread_rng(); + let random_entry = poem_list + .choose(&mut rng) + .expect("Got a valid entry") + .1 + .clone(); + format!("/poems/{random_entry}") + } +} pub fn get_homepage_paragraph() -> String { let homepage_paragraph_content = @@ -21,112 +156,3 @@ pub fn get_homepage_paragraph() -> String { markdown::to_html_with_options(homepage_paragraph_to_string, &Options::gfm()).unwrap(); test } - -pub fn get_poem(slug: String) -> (String, String, String) { - let filename = String::from(String::from(slug.clone()) + ".md"); - let creation_date = - String::from(String::from("
Written on: ") + filename.split("_").next().unwrap()); - let poem_content = Poems::get(&filename).expect("Found poem {filename:?}"); - let mut poem_to_str = std::str::from_utf8(poem_content.data.as_ref()) - .expect("Title is valid UT8.") - .lines(); - let poem_title = poem_to_str.next().unwrap(); - let poem_content = poem_to_str.into_iter().collect::>().join("\n"); - let poem_title_to_html_string = - markdown::to_html_with_options(poem_title, &Options::gfm()).unwrap(); - let poem_content_to_html_string = - markdown::to_html_with_options(poem_content.as_str(), &Options::gfm()).unwrap(); - ( - poem_title_to_html_string, - poem_content_to_html_string, - creation_date, - ) -} - -pub fn get_poem_list() -> Vec<(String, String)> { - let mut poem_list = Vec::new(); - for p in Poems::iter() { - let filename = p.to_string(); - let poem_content = Poems::get(&filename).expect("Found poem {filename:?}"); - let mut poem_to_str = std::str::from_utf8(poem_content.data.as_ref()) - .expect("Title is valid UT8.") - .lines(); - let title_markdown = poem_to_str.next().expect("No title specified."); - let title = markdown::to_html_with_options(title_markdown, &Options::gfm()).unwrap(); - let slug = String::from(filename.trim_end_matches(".md")); - poem_list.push((title.clone(), slug.clone())); - } - log::trace!("{poem_list:?}"); - poem_list -} - -pub fn get_oldest_entry(current: String) -> String { - let mut poem_list = VecDeque::from(get_poem_list()); - let oldest = poem_list - .pop_front() - .expect("There is an entry in this list of poems.") - .1; - if current == oldest { - return format!("/poems/{current}#"); - } - format!("/poems/{oldest}") -} - -pub fn get_latest_entry(current: String) -> String { - let mut poem_list = get_poem_list(); - let latest = poem_list - .pop() - .expect("There is an entry in this list of poems.") - .1; - if current == latest { - return format!("/poems/{current}#"); - } - format!("/poems/{latest}") -} - -pub fn get_previous_entry(current: String) -> String { - let poem_list = get_poem_list(); - match poem_list.iter().enumerate().find_map(|(index, p)| { - if p.1 == current { - if index != 0 { - Some(poem_list[index - 1].1.clone()) - } else { - None - } - } else { - None - } - }) { - Some(entry) => format!("/poems/{entry}"), - None => format!("/poems/{current}"), - } -} - -pub fn get_next_entry(current: String) -> String { - let poem_list = get_poem_list(); - match poem_list.iter().enumerate().find_map(|(index, p)| { - if p.1 == current { - if index != poem_list.len() - 1 { - Some(poem_list[index + 1].1.clone()) - } else { - None - } - } else { - None - } - }) { - Some(entry) => format!("/poems/{entry}"), - None => format!("/poems/{current}"), - } -} - -pub fn get_random_entry() -> String { - let poem_list = get_poem_list(); - let mut rng = rand::thread_rng(); - let random_entry = poem_list - .choose(&mut rng) - .expect("Got a valid entry") - .1 - .clone(); - format!("/poems/{random_entry}") -} diff --git a/void-fe/src/utils/prop_structs.rs b/void-fe/src/utils/prop_structs.rs index de61828..d31844a 100644 --- a/void-fe/src/utils/prop_structs.rs +++ b/void-fe/src/utils/prop_structs.rs @@ -1,5 +1,5 @@ +use std::collections::HashMap; use crate::void_app::{Element, Props}; - use super::user_prefs::UserPrefs; #[derive(PartialEq, Props)] @@ -29,14 +29,25 @@ pub struct ButtonProps { } #[derive(PartialEq, Props)] -pub struct PoemData { - pub title: Option, - pub content: Option, - pub creation_date: Option, +pub struct VoidProps { pub slug: Option, + pub poem_database: PoemDatabase, pub user_prefs: UserPrefs, } +#[derive(PartialEq, Props, Clone, Debug)] +pub struct PoemDatabase { + pub poem_list: Vec<(String, String)>, + pub poem_hashmap: HashMap, +} + +#[derive(PartialEq, Clone, Debug)] +pub struct PoemStruct { + pub title: String, + pub content: String, + pub creation_date: String, +} + // These next three should all just be one prop. #[derive(Props)] pub struct PoemChildren<'a> { @@ -51,4 +62,4 @@ pub struct PageChildren<'a> { #[derive(Props)] pub struct ContentChildren<'a> { pub children: Element<'a>, -} \ No newline at end of file +}