//! 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::view::{Child, View, component, view}; use crate::assets::{DRIVE_JS_HREF, MAIN_CSS_HREF, NAV_JS_HREF, PAGES_JS_HREF, VIEWER_JS_HREF}; use crate::auth::Viewer; use crate::family::FamilyLink; use crate::i18n::Lang; use crate::i18n::admin::{AK, a}; use crate::i18n::drive::{DK, d}; use crate::i18n::settings::{SK as SettingsK, s as settings_s}; use crate::i18n::share::{SK, s as share_s}; use crate::i18n::shell::{K, t}; use crate::views::drive::quota_bar; pub mod drive; /// What the shell shows about the signed-in person: the name, the administrator badge, and the CSRF /// token of the session for the sign-out form. pub struct ViewerInfo { /// The name shown in the topbar. pub name: String, /// Whether the topbar shows the administrator badge. pub is_admin: bool, /// The session's CSRF token, which the sign-out form sends. pub csrf: String, /// Bytes the viewer's files use, trashed ones included. pub used: i64, /// The viewer's quota in bytes. pub quota: i64, /// The viewer's theme: `system`, `light` or `dark`. It goes on ``. pub theme: String, /// The family switcher list, as the family worker last fetched it. pub family: Vec, } impl ViewerInfo { /// What the shell needs to know about a signed-in viewer. `family` is the switcher list. pub fn of(viewer: &Viewer, family: Vec) -> Self { Self { name: viewer.member.name.clone(), is_admin: viewer.member.is_admin, csrf: viewer.session.csrf_token.clone(), used: viewer.member.used_bytes, quota: viewer.member.quota_bytes, theme: viewer.member.theme.clone(), family, } } } /// 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 `
`. `drive_js` adds the upload script, which only the /// drive pages need. /// /// The topbar has the wordmark on the left, the service switcher in the middle (this service's pages, then /// the other family services from the family worker) and the viewer's user menu on the right. The menu's /// sign-out form opts out of soft navigation, because it ends in a redirect to `im`. `nav.js` reads the /// restart text from ``. #[component] pub async fn shell( title: &str, lang: Lang, viewer: Option<&ViewerInfo>, #[default] drive_js: bool, #[default] child: Child<'_>, ) -> Result { let document_title = format!("{title} \u{b7} in"); let theme = viewer.map_or("system", |v| v.theme.as_str()); Ok(view! { (document_title) if drive_js { }
"in"
if let Some(viewer) = viewer { quota_bar(lang: lang, used: viewer.used, quota: viewer.quota)
(viewer.name.as_str()) (s_title(lang)) if viewer.is_admin { (a_title(lang)) }
if viewer.is_admin { (t(lang, K::Admin)) } }
(child)
}) } /// The menu link to the settings page, in the viewer's language. fn s_title(lang: Lang) -> &'static str { settings_s(lang, SettingsK::Title) } /// The menu link to the administration page, in the viewer's language. fn a_title(lang: Lang) -> &'static str { a(lang, AK::Title) } /// One other family service in the switcher. The name and the URL are escaped by `view!`, and the URL was /// checked by `family::usable` before it was stored. #[component] pub async fn switch_link(name: &str, url: &str) -> Result { Ok(view! { (name) }) } /// An error message for a failed action. Forms show it above their fields. #[allow(dead_code)] // The first form that can fail a check uses it. #[component] pub async fn alert(message: &str) -> Result { Ok(view! { }) } /// 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)
}) } /// Something to look at that is not a failure yet, such as a state that has gone stale. #[component] pub async fn warning(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)
}) }