Adding components and assets.
Commiting to try the library with the target dioxus project.
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use tavern::Lore;
|
||||
use tavern::{Adventurer, Legend, Lore, Tale};
|
||||
|
||||
use crate::components::social::*;
|
||||
use crate::page::Page;
|
||||
use crate::server::*;
|
||||
|
||||
|
||||
|
||||
@ -15,7 +14,6 @@ const POST_CSS: Asset = asset!("/assets/css/blog_post.css");
|
||||
const TAG_CSS: Asset = asset!("/assets/css/blog_tag.css");
|
||||
|
||||
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum TagStyle
|
||||
{
|
||||
@ -85,6 +83,7 @@ pub fn TagList(children: Element) -> Element
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
#[component]
|
||||
pub fn BlogAuthor() -> Element
|
||||
{
|
||||
@ -210,20 +209,32 @@ pub fn BlogNav() -> Element
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
#[component]
|
||||
pub fn BlogPost(children: Element) -> Element
|
||||
pub fn PostHeaderAuthor(name: String, handle: String, profile_link: String) -> Element
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
document::Link { rel: "stylesheet", href: POST_CSS }
|
||||
h4 { "Author: ", a { href: "{profile_link}", "{name} @{handle}" } }
|
||||
}
|
||||
}
|
||||
|
||||
article
|
||||
#[component]
|
||||
pub fn PostHeader(title: String, author: String, tags: Vec<String>) -> Element
|
||||
{
|
||||
let Ok(author) = use_server_future(move || get_author(author.clone())) else
|
||||
{
|
||||
return rsx! { p { "Failed to load author." } }
|
||||
};
|
||||
|
||||
rsx!
|
||||
{
|
||||
h1 { "{title}" }
|
||||
TagList
|
||||
{
|
||||
class: "blog_post_style",
|
||||
h1 { "Leet Code: Let's Get Started" }
|
||||
TagList
|
||||
for category in tags
|
||||
{
|
||||
TagItem
|
||||
{
|
||||
@ -231,7 +242,96 @@ pub fn BlogPost(children: Element) -> Element
|
||||
style: TagStyle::Regular
|
||||
}
|
||||
}
|
||||
h4 { "Author: ", a { href: "https://cybermages.tech", "Jason Smith" } }
|
||||
}
|
||||
|
||||
match &*author.read()
|
||||
{
|
||||
Some(Ok(author)) =>
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
PostHeaderAuthor
|
||||
{
|
||||
name: author.name.clone(),
|
||||
handle: author.handle.clone(),
|
||||
profile_link: author.legend.profile.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(Err(e)) =>
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
p { "Unable to show post header." }
|
||||
p { "{e}" }
|
||||
}
|
||||
}
|
||||
|
||||
None =>
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
p { "Loading..." }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn BlogPost(slug: String, children: Element) -> Element
|
||||
{
|
||||
// 1. Fetch the blog post using the slug.
|
||||
let Ok(post) = use_server_future(move || get_blog_post(slug.clone())) else
|
||||
{
|
||||
return rsx! { p { "Failed to load post." } }
|
||||
};
|
||||
|
||||
// Then build the component.
|
||||
rsx!
|
||||
{
|
||||
document::Link { rel: "stylesheet", href: POST_CSS }
|
||||
|
||||
article
|
||||
{
|
||||
class: "blog_post_style",
|
||||
|
||||
match &*post.read()
|
||||
{
|
||||
Some(Ok(post)) =>
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
PostHeader
|
||||
{
|
||||
title: post.lore.title.clone(),
|
||||
author: post.lore.author.clone(),
|
||||
tags: post.lore.tags.clone()
|
||||
}
|
||||
|
||||
div { dangerous_inner_html: "{post.story}" }
|
||||
}
|
||||
}
|
||||
|
||||
Some(Err(e)) =>
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
p { "Unable to show desired post." }
|
||||
p { "{e}" }
|
||||
}
|
||||
}
|
||||
|
||||
None =>
|
||||
{
|
||||
rsx!
|
||||
{
|
||||
p { "Loading..." }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{children}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,20 +2,17 @@
|
||||
|
||||
mod info;
|
||||
|
||||
#[cfg(not(feature = "server"))]
|
||||
mod components;
|
||||
|
||||
mod server;
|
||||
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use tavern::Database;
|
||||
|
||||
pub use crate::info::{get_name, get_version};
|
||||
|
||||
|
||||
#[cfg(not(feature = "server"))]
|
||||
pub use crate::components::*;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn init_database<P>(path: P)
|
||||
-> Result<std::sync::Arc<Database>, Box<dyn std::error::Error>>
|
||||
where P: AsRef<std::path::Path>
|
||||
{
|
||||
let db = Database::open(path).await?;
|
||||
Ok(std::sync::Arc::new(db))
|
||||
}
|
||||
pub use crate::server::*;
|
||||
|
||||
@ -1,27 +1,70 @@
|
||||
use dioxus::prelude::*;
|
||||
// Remember that Server functions parameters must not be references.
|
||||
// They are coming from potentially other computers.
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use std::sync::Arc;
|
||||
use crate::{Database, Lore, Tale};
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
use tavern::{Adventurer, Lore, Tale};
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
use tavern::Database;
|
||||
|
||||
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
static BLOG_DATABASE: OnceCell<Arc<Database>> = OnceCell::const_new();
|
||||
|
||||
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
async fn get_database_instance() -> &'static Arc<Database>
|
||||
{
|
||||
BLOG_DATABASE.get_or_init(|| async {
|
||||
let db = Database::open("tavern.db")
|
||||
.await
|
||||
.expect("Failed to open database");
|
||||
Arc::new(db)
|
||||
}).await
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[server]
|
||||
pub async fn get_blog_list() -> Result<Vec<Lore>, ServerFnError> {
|
||||
let db = server_context()
|
||||
.get::<Arc<Database>>()
|
||||
.ok_or_else(|| ServerFnError::ServerError("Database context not available".to_string()))?;
|
||||
pub async fn get_blog_list(categories: Vec<String>) -> Result<Vec<Lore>, ServerFnError>
|
||||
{
|
||||
let db = get_database_instance().await;
|
||||
|
||||
let summaries = db.get_tales_summary(&[]).await
|
||||
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
||||
let summaries = db.get_tales_summary(&categories).await
|
||||
.map_err(|e| ServerFnError::new(e))?;
|
||||
|
||||
Ok(summaries)
|
||||
}
|
||||
|
||||
|
||||
#[server]
|
||||
pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError> {
|
||||
let db = server_context()
|
||||
.get::<Arc<Database>>()
|
||||
.ok_or_else(|| ServerFnError::ServerError("Database context not available".to_string()))?;
|
||||
pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError>
|
||||
{
|
||||
let db = get_database_instance().await;
|
||||
|
||||
let tale = db.get_tale_by_slug(&slug).await
|
||||
.map_err(|e| ServerFnError::ServerError(e.to_string()))?;
|
||||
.map_err(|e| ServerFnError::new(e))?;
|
||||
|
||||
tale.ok_or(ServerFnError::ServerError("Post not found".into()))
|
||||
tale.ok_or(ServerFnError::new(format!("Post {} not found", slug)))
|
||||
}
|
||||
|
||||
|
||||
#[server]
|
||||
pub async fn get_author(handle: String) -> Result<Adventurer, ServerFnError>
|
||||
{
|
||||
let db = get_database_instance().await;
|
||||
|
||||
let author = db.get_adventurer(&handle).await
|
||||
.map_err(|e| ServerFnError::new(e))?;
|
||||
|
||||
author.ok_or(ServerFnError::new(format!("Author {} not found.", handle)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user