//! Shared page chrome: the HTML shell and the small components every page is built from.
//!
//! `view!` escapes text and attribute values, so user data is safe in it by default. Nothing here
//! emits an inline script, style or event handler; see `security.rs`.
use topcoat::Result;
use topcoat::context::{Cx, app_context};
use topcoat::router::response::{IntoResponse, Response};
use topcoat::view::{Child, View, ViewExt, component, view};
pub mod admin;
pub mod board;
pub mod files;
pub mod structure;
pub mod task;
use crate::assets::{MAIN_CSS_HREF, NAV_JS_HREF, PAGES_JS_HREF};
use crate::family::Service;
use crate::i18n::Lang;
use crate::i18n::shell::{K, t};
use crate::state::AppState;
/// The family key of this app. The switcher marks the service with this key as the current one.
const APP_KEY: &str = "iz";
/// What the shell shows about the signed-in person: the name in the user menu, the administrator
/// badge, and the CSRF token of the sign-out form in that menu.
pub struct ViewerInfo {
/// The name shown in the user menu.
pub name: String,
/// Whether the topbar shows the administrator badge.
pub is_admin: bool,
/// The session's CSRF token. The sign-out form carries it.
pub csrf: String,
}
/// The HTML document around a page. `title` is the page's own title; the document title adds the
/// app name. `child` is the page, placed in ``. `page_script` is the one script of this page
/// (for example `board.js`), or `None`; it is loaded with `defer` and only on the page that needs it.
///
/// The topbar has the wordmark on the left, the service switcher in the middle and the viewer's name
/// on the right. The switcher lists the family services from the cache in `AppState`; it is left out
/// when the list is empty. `nav.js` reads the restart text from ``.
#[component]
pub async fn shell(
cx: &Cx,
title: &str,
lang: Lang,
viewer: Option<&ViewerInfo>,
page_script: Option<&str>,
#[default] child: Child<'_>,
) -> Result {
let document_title = format!("{title} \u{b7} iz");
let services = app_context::(cx).family.snapshot();
Ok(view! {
(document_title)
if let Some(src) = page_script {
}
(t(lang, K::SkipToContent))"iz"
if !services.is_empty() {
}
if let Some(viewer) = viewer {
(viewer.name.as_str())
if viewer.is_admin {
(t(lang, K::Admin))
}
}
(child)
})
}
/// Whether `service` is the app that renders the page, which the switcher marks with `aria-current`.
fn service_is_current(service: &Service) -> Option<&'static str> {
(service.key == APP_KEY).then_some("true")
}
/// The body of a message page: a title, one sentence, an optional detail (escaped, in a code
/// element) and a way back to the start.
#[component]
pub async fn message_page(
lang: Lang,
title: K,
body: K,
detail: Option,
) -> Result {
Ok(view! {
(t(lang, title))
(t(lang, body))
if let Some(detail) = detail {
(detail.as_str())
}
(t(lang, K::BackHome))
})
}
/// An error message for a failed action. Forms show it above their fields.
#[component]
pub async fn alert(message: &str) -> Result {
Ok(view! {
(message)
})
}
/// A plain message that is not an error, such as a confirmation or a hint.
#[component]
pub async fn notice(message: &str) -> Result {
Ok(view! {
(message)
})
}
/// A section with nothing in it yet: a title, then what to do next.
#[component]
pub async fn empty_state(title: &str, #[default] child: Child<'_>) -> Result {
Ok(view! {
(title)
(child)
})
}
/// Turns a rendered view into the response of a handler. For a handler that answers with a view on one
/// path and with a redirect or JSON on another.
pub async fn respond(cx: &Cx, view: impl View) -> Result {
let handle = view.first().await?;
handle.into_response(cx)
}