use crate::register;
use leptos::{
attr::global::GlobalAttributes, component, prelude::LeptosOptions,
tachys::html::element::link, IntoView,
};
#[component]
pub fn Stylesheet(
#[prop(into)]
href: String,
#[prop(optional, into)]
id: Option<String>,
) -> impl IntoView {
register(link().id(id).rel("stylesheet").href(href))
}
#[component]
pub fn HashedStylesheet(
options: LeptosOptions,
#[prop(optional, into)]
id: Option<String>,
#[prop(optional, into)]
root: Option<String>,
) -> impl IntoView {
let mut css_file_name = options.output_name.to_string();
if options.hash_files {
let hash_path = std::env::current_exe()
.map(|path| {
path.parent().map(|p| p.to_path_buf()).unwrap_or_default()
})
.unwrap_or_default()
.join(options.hash_file.as_ref());
if hash_path.exists() {
let hashes = std::fs::read_to_string(&hash_path)
.expect("failed to read hash file");
for line in hashes.lines() {
let line = line.trim();
if !line.is_empty() {
if let Some((file, hash)) = line.split_once(':') {
if file == "css" {
css_file_name
.push_str(&format!(".{}", hash.trim()));
}
}
}
}
}
}
css_file_name.push_str(".css");
let pkg_path = &options.site_pkg_dir;
let root = root.unwrap_or_default();
link()
.id(id)
.rel("stylesheet")
.href(format!("{root}/{pkg_path}/{css_file_name}"))
}