forge
Explore
Log in

Don't you dare go Hollow

im

Clone
★ 0

0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits

mod.rs173 lines, 6.3 KiBHistoryRawDownload
1//! Shared page chrome: the HTML shell and the small components every page is built from.
2//!
3//! `view!` escapes text and attribute values, so user data is safe in it by default. Nothing here
4//! emits an inline script, style or event handler; see `security.rs`.
5
6use topcoat::Result;
7use topcoat::context::Cx;
8use topcoat::view::{Child, View, component, view};
9
10use crate::assets::{MAIN_CSS_HREF, NAV_JS_HREF, PAGES_JS_HREF};
11use crate::auth::Viewer;
12use crate::i18n::account::{Txt, text};
13use crate::i18n::shell::{K, t};
14use crate::i18n::{Lang, lang as request_lang};
15
16/// What the shell shows about the signed-in person, and the token of their sign-out form.
17pub struct ViewerInfo {
18 /// The name shown in the account menu.
19 pub name: String,
20 /// Whether the menu offers the administration section.
21 pub is_admin: bool,
22 /// The session's CSRF token, for the sign-out form in the menu.
23 pub csrf: String,
24 /// `system`, `light` or `dark`. Becomes a class on `<html>`.
25 pub theme: String,
26}
27
28impl ViewerInfo {
29 /// What the shell shows for `viewer`, with the token of its sign-out form.
30 pub fn of(viewer: &Viewer) -> Self {
31 Self {
32 name: viewer.user.name.clone(),
33 is_admin: viewer.user.is_admin,
34 csrf: viewer.session.csrf_token.clone(),
35 theme: viewer.user.theme.clone(),
36 }
37 }
38
39 /// The class of `<html>` for this theme. Anything unknown falls back to the device's choice.
40 pub fn theme_class(&self) -> &'static str {
41 match self.theme.as_str() {
42 "light" => "theme-light",
43 "dark" => "theme-dark",
44 _ => "theme-system",
45 }
46 }
47}
48
49/// The language of a page: the signed-in person's stored choice when they have one and have not
50/// chosen "automatic", else the request's `Accept-Language`.
51pub fn page_lang(cx: &Cx, viewer: Option<&Viewer>) -> Lang {
52 let stored = viewer
53 .filter(|viewer| !viewer.user.locale_auto)
54 .map(|viewer| viewer.user.locale.as_str())
55 .filter(|locale| !locale.is_empty());
56 request_lang(cx, stored)
57}
58
59/// The HTML document around a page. `title` is the page's own title; the document title adds the
60/// app name. `child` is the page, placed in `<main>`.
61///
62/// The topbar has the wordmark on the left, the service switcher in the middle (empty for now) and
63/// the viewer's name on the right. `nav.js` reads the restart text from `<body data-restart-text>`.
64#[component]
65pub async fn shell(
66 title: &str,
67 lang: Lang,
68 viewer: Option<ViewerInfo>,
69 #[default] child: Child<'_>,
70) -> Result<impl View> {
71 let document_title = format!("{title} \u{b7} im");
72 let theme_class = viewer
73 .as_ref()
74 .map_or("theme-system", ViewerInfo::theme_class);
75 Ok(view! {
76 <!DOCTYPE html>
77 <html lang=(lang.code()) class=(theme_class)>
78 <head>
79 <meta charset="utf-8">
80 <meta name="viewport" content="width=device-width, initial-scale=1">
81 <title>(document_title)</title>
82 <link rel="stylesheet" href=(MAIN_CSS_HREF)>
83 <script src=(NAV_JS_HREF) defer=""></script>
84 <script src=(PAGES_JS_HREF) defer=""></script>
85 </head>
86 <body data-restart-text=(t(lang, K::ServerRestarting))>
87 <a class="skip-link" href="#main">(t(lang, K::SkipToContent))</a>
88 <header class="topbar">
89 <a class="topbar-brand" href="/">"im"</a>
90 <nav class="switcher" aria-label=(t(lang, K::Services))></nav>
91 <div class="topbar-end">
92 if let Some(viewer) = viewer {
93 user_menu(lang: lang, viewer: viewer)
94 }
95 </div>
96 </header>
97 <main id="main">(child)</main>
98 </body>
99 </html>
100 })
101}
102
103/// The account menu of the topbar: the name, the account links and the sign-out button. It is a
104/// disclosure element, so it opens without any script. The sign-out form is a full page load, so
105/// the browser drops its live stream at once.
106#[component]
107pub async fn user_menu(lang: Lang, viewer: ViewerInfo) -> Result<impl View> {
108 Ok(view! {
109 <details class="user-menu">
110 <summary>
111 <span class="user-menu-name">(viewer.name.clone())</span>
112 if viewer.is_admin {
113 <span class="badge accent">(t(lang, K::Admin))</span>
114 }
115 </summary>
116 <div class="user-menu-panel">
117 <nav class="user-menu-links" aria-label=(text(lang, Txt::AccountMenu))>
118 <a href="/account/profile">(text(lang, Txt::ProfileTitle))</a>
119 <a href="/account/preferences">(text(lang, Txt::PreferencesTitle))</a>
120 <a href="/account/security">(text(lang, Txt::SecurityTitle))</a>
121 <a href="/account/sessions">(text(lang, Txt::SessionsTitle))</a>
122 if viewer.is_admin {
123 <a href="/admin">(text(lang, Txt::AdminArea))</a>
124 }
125 </nav>
126 <form method="post" action="/logout" data-hard="true" class="user-menu-logout">
127 <input type="hidden" name="csrf" value=(viewer.csrf.clone())>
128 <button type="submit" class="button ghost">(text(lang, Txt::SignOut))</button>
129 </form>
130 </div>
131 </details>
132 })
133}
134
135/// An error message for a failed action. Forms show it above their fields.
136#[component]
137pub async fn alert(message: String) -> Result<impl View> {
138 Ok(view! { <div class="alert" role="alert">(message)</div> })
139}
140
141/// A plain message that is not an error, such as a confirmation or a hint.
142#[component]
143pub async fn notice(message: String) -> Result<impl View> {
144 Ok(view! { <div class="notice" role="status">(message)</div> })
145}
146
147/// A section with nothing in it yet: a title, then what to do next.
148#[component]
149pub async fn empty_state(title: &str, #[default] child: Child<'_>) -> Result<impl View> {
150 Ok(view! {
151 <section class="empty-state">
152 <h2>(title)</h2>
153 (child)
154 </section>
155 })
156}
157
158/// A labelled text input. `value` refills the field after an error; password fields pass "".
159#[component]
160pub async fn field(
161 label: &str,
162 name: &'static str,
163 kind: &'static str,
164 autocomplete: &'static str,
165 value: String,
166) -> Result<impl View> {
167 Ok(view! {
168 <label class="field">
169 <span class="field-label">(label)</span>
170 <input name=(name) type=(kind) autocomplete=(autocomplete) value=(value) required="" spellcheck="false">
171 </label>
172 })
173}