use yew::{html::ChildrenProps, AttrValue, Children, Properties};
pub const HASHIRA_TITLE_MARKER: &str = "<!--hashira_title-->";
pub const HASHIRA_META_MARKER: &str = "<!--hashira_meta-->";
pub const HASHIRA_CONTENT_MARKER: &str = "<!--hashira_content-->";
pub const HASHIRA_LINKS_MARKER: &str = "<!--hashira_links-->";
pub const HASHIRA_SCRIPTS_MARKER: &str = "<!--hashira_scripts-->";
pub const HASHIRA_ROOT: &str = "__hashira__root__";
pub const HASHIRA_PAGE_DATA: &str = "__hashira__page_data__";
pub const HASHIRA_WASM_LOADER: &str = "__hashira_wasm_loader";
#[derive(Debug, Clone, PartialEq, Properties)]
pub struct MainProps {
#[prop_or_default]
pub class: Option<String>,
#[prop_or_default]
pub style: Option<String>,
#[prop_or_default]
pub children: Children,
}
#[yew::function_component]
pub fn Title() -> yew::Html {
yew::Html::from_html_unchecked(AttrValue::from(HASHIRA_TITLE_MARKER))
}
#[yew::function_component]
pub fn Meta() -> yew::Html {
yew::Html::from_html_unchecked(AttrValue::from(HASHIRA_META_MARKER))
}
#[yew::function_component]
pub fn Content() -> yew::Html {
yew::Html::from_html_unchecked(AttrValue::from(HASHIRA_CONTENT_MARKER))
}
#[yew::function_component]
pub fn Links() -> yew::Html {
yew::Html::from_html_unchecked(AttrValue::from(HASHIRA_LINKS_MARKER))
}
#[yew::function_component]
pub fn Scripts() -> yew::Html {
yew::Html::from_html_unchecked(AttrValue::from(HASHIRA_SCRIPTS_MARKER))
}
#[yew::function_component]
pub fn Main(props: &MainProps) -> yew::Html {
yew::html! {
<main id={HASHIRA_ROOT} class={props.class.clone()} style={props.style.clone()}>
{for props.children.iter()}
</main>
}
}
#[cfg(debug_assertions)]
#[yew::function_component]
pub fn LiveReload() -> yew::Html {
const LIVE_RELOAD_SCRIPT: &str = include_str!("./livereload.js");
use crate::env::{HASHIRA_LIVE_RELOAD_HOST, HASHIRA_LIVE_RELOAD_PORT};
if !crate::env::is_live_reload() {
return yew::Html::default();
}
let host = std::env::var(HASHIRA_LIVE_RELOAD_HOST)
.map(|env| format!("'{}'", env))
.unwrap_or_else(|_| String::from("undefined"));
let port = std::env::var(HASHIRA_LIVE_RELOAD_PORT).unwrap_or_else(|_| String::from("0"));
yew::Html::from_html_unchecked(AttrValue::from(format!(
r#"
<script>
window.{HASHIRA_LIVE_RELOAD_HOST} = {host};
window.{HASHIRA_LIVE_RELOAD_PORT} = {port};
{LIVE_RELOAD_SCRIPT}
</script>"#
)))
}
#[cfg(not(debug_assertions))]
#[yew::function_component]
pub fn LiveReload() -> yew::Html {
yew::Html::default()
}
#[yew::function_component]
pub fn WasmLoadingBase(props: &ChildrenProps) -> yew::Html {
if crate::consts::IS_SERVER && crate::env::get_client_name().is_none(){
return yew::Html::default();
}
yew::html! {
<div id={HASHIRA_WASM_LOADER} data-wasm-loaded="false">
{for props.children.iter()}
</div>
}
}
#[yew::function_component]
pub fn WasmLoading() -> yew::Html {
yew::html! {
<WasmLoadingBase>
<style>
{yew::Html::from_html_unchecked(AttrValue::from(format!(r#"
#{HASHIRA_WASM_LOADER}[data-wasm-loaded="false"] {{
position: fixed;
z-index: 9999;
width: 100%;
height: 100%;
background-color: white;
opacity: 0;
cursor: wait;
}}
"#)))}
</style>
</WasmLoadingBase>
}
}