//! 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; use topcoat::view::{Child, View, component, view}; use crate::assets::{MAIN_CSS_HREF, NAV_JS_HREF, PAGES_JS_HREF}; use crate::auth::Viewer; use crate::i18n::account::{Txt, text}; use crate::i18n::shell::{K, t}; use crate::i18n::{Lang, lang as request_lang}; /// What the shell shows about the signed-in person, and the token of their sign-out form. pub struct ViewerInfo { /// The name shown in the account menu. pub name: String, /// Whether the menu offers the administration section. pub is_admin: bool, /// The session's CSRF token, for the sign-out form in the menu. pub csrf: String, /// `system`, `light` or `dark`. Becomes a class on ``. pub theme: String, } impl ViewerInfo { /// What the shell shows for `viewer`, with the token of its sign-out form. pub fn of(viewer: &Viewer) -> Self { Self { name: viewer.user.name.clone(), is_admin: viewer.user.is_admin, csrf: viewer.session.csrf_token.clone(), theme: viewer.user.theme.clone(), } } /// The class of `` for this theme. Anything unknown falls back to the device's choice. pub fn theme_class(&self) -> &'static str { match self.theme.as_str() { "light" => "theme-light", "dark" => "theme-dark", _ => "theme-system", } } } /// The language of a page: the signed-in person's stored choice when they have one and have not /// chosen "automatic", else the request's `Accept-Language`. pub fn page_lang(cx: &Cx, viewer: Option<&Viewer>) -> Lang { let stored = viewer .filter(|viewer| !viewer.user.locale_auto) .map(|viewer| viewer.user.locale.as_str()) .filter(|locale| !locale.is_empty()); request_lang(cx, stored) } /// 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 `
`. /// /// The topbar has the wordmark on the left, the service switcher in the middle (empty for now) and /// the viewer's name on the right. `nav.js` reads the restart text from ``. #[component] pub async fn shell( title: &str, lang: Lang, viewer: Option, #[default] child: Child<'_>, ) -> Result { let document_title = format!("{title} \u{b7} im"); let theme_class = viewer .as_ref() .map_or("theme-system", ViewerInfo::theme_class); Ok(view! { (document_title)
"im"
if let Some(viewer) = viewer { user_menu(lang: lang, viewer: viewer) }
(child)
}) } /// The account menu of the topbar: the name, the account links and the sign-out button. It is a /// disclosure element, so it opens without any script. The sign-out form is a full page load, so /// the browser drops its live stream at once. #[component] pub async fn user_menu(lang: Lang, viewer: ViewerInfo) -> Result { Ok(view! {
(viewer.name.clone()) if viewer.is_admin { (t(lang, K::Admin)) }
}) } /// An error message for a failed action. Forms show it above their fields. #[component] pub async fn alert(message: String) -> Result { Ok(view! { }) } /// A plain message that is not an error, such as a confirmation or a hint. #[component] pub async fn notice(message: String) -> 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)
}) } /// A labelled text input. `value` refills the field after an error; password fields pass "". #[component] pub async fn field( label: &str, name: &'static str, kind: &'static str, autocomplete: &'static str, value: String, ) -> Result { Ok(view! { }) }