forge
Explore
Log in

Don't you dare go Hollow

iz

Clone
★ 0

0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits

mod.rs157 lines, 5.7 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, app_context};
8use topcoat::router::response::{IntoResponse, Response};
9use topcoat::view::{Child, View, ViewExt, component, view};
10
11pub mod admin;
12pub mod board;
13pub mod files;
14pub mod structure;
15pub mod task;
16
17use crate::assets::{MAIN_CSS_HREF, NAV_JS_HREF, PAGES_JS_HREF};
18use crate::family::Service;
19use crate::i18n::Lang;
20use crate::i18n::shell::{K, t};
21use crate::state::AppState;
22
23/// The family key of this app. The switcher marks the service with this key as the current one.
24const APP_KEY: &str = "iz";
25
26/// What the shell shows about the signed-in person: the name in the user menu, the administrator
27/// badge, and the CSRF token of the sign-out form in that menu.
28pub struct ViewerInfo {
29 /// The name shown in the user menu.
30 pub name: String,
31 /// Whether the topbar shows the administrator badge.
32 pub is_admin: bool,
33 /// The session's CSRF token. The sign-out form carries it.
34 pub csrf: String,
35}
36
37/// The HTML document around a page. `title` is the page's own title; the document title adds the
38/// app name. `child` is the page, placed in `<main>`. `page_script` is the one script of this page
39/// (for example `board.js`), or `None`; it is loaded with `defer` and only on the page that needs it.
40///
41/// The topbar has the wordmark on the left, the service switcher in the middle and the viewer's name
42/// on the right. The switcher lists the family services from the cache in `AppState`; it is left out
43/// when the list is empty. `nav.js` reads the restart text from `<body data-restart-text>`.
44#[component]
45pub async fn shell(
46 cx: &Cx,
47 title: &str,
48 lang: Lang,
49 viewer: Option<&ViewerInfo>,
50 page_script: Option<&str>,
51 #[default] child: Child<'_>,
52) -> Result<impl View> {
53 let document_title = format!("{title} \u{b7} iz");
54 let services = app_context::<AppState>(cx).family.snapshot();
55 Ok(view! {
56 <!DOCTYPE html>
57 <html lang=(lang.code()) data-theme="system">
58 <head>
59 <meta charset="utf-8">
60 <meta name="viewport" content="width=device-width, initial-scale=1">
61 <title>(document_title)</title>
62 <link rel="stylesheet" href=(MAIN_CSS_HREF)>
63 <script src=(NAV_JS_HREF) defer=""></script>
64 <script src=(PAGES_JS_HREF) defer=""></script>
65 if let Some(src) = page_script {
66 <script src=(src) defer=""></script>
67 }
68 </head>
69 <body data-restart-text=(t(lang, K::ServerRestarting))>
70 <a class="skip-link" href="#main">(t(lang, K::SkipToContent))</a>
71 <header class="topbar">
72 <a class="topbar-brand" href="/">"iz"</a>
73 if !services.is_empty() {
74 <nav class="switcher" aria-label=(t(lang, K::Services))>
75 for service in services.iter() {
76 <a href=(service.url.as_str()) rel="noopener" title=(service.name.as_str())
77 data-healthy=(if service.healthy { "true" } else { "false" })
78 aria-current=(service_is_current(service))>(service.name.as_str())</a>
79 }
80 </nav>
81 }
82 <div class="topbar-end">
83 if let Some(viewer) = viewer {
84 <details class="user-menu">
85 <summary>(viewer.name.as_str())</summary>
86 <form method="post" action="/auth/logout" class="user-menu-form">
87 <input type="hidden" name="csrf" value=(viewer.csrf.as_str())>
88 <button type="submit" class="button">(t(lang, K::SignOut))</button>
89 </form>
90 </details>
91 if viewer.is_admin {
92 <span class="badge accent">(t(lang, K::Admin))</span>
93 }
94 }
95 </div>
96 </header>
97 <main id="main">(child)</main>
98 </body>
99 </html>
100 })
101}
102
103/// Whether `service` is the app that renders the page, which the switcher marks with `aria-current`.
104fn service_is_current(service: &Service) -> Option<&'static str> {
105 (service.key == APP_KEY).then_some("true")
106}
107
108/// The body of a message page: a title, one sentence, an optional detail (escaped, in a code
109/// element) and a way back to the start.
110#[component]
111pub async fn message_page(
112 lang: Lang,
113 title: K,
114 body: K,
115 detail: Option<String>,
116) -> Result<impl View> {
117 Ok(view! {
118 <section class="card narrow">
119 <h1>(t(lang, title))</h1>
120 <p>(t(lang, body))</p>
121 if let Some(detail) = detail {
122 <p><code>(detail.as_str())</code></p>
123 }
124 <a class="button primary" href="/">(t(lang, K::BackHome))</a>
125 </section>
126 })
127}
128
129/// An error message for a failed action. Forms show it above their fields.
130#[component]
131pub async fn alert(message: &str) -> Result<impl View> {
132 Ok(view! { <div class="alert" role="alert">(message)</div> })
133}
134
135/// A plain message that is not an error, such as a confirmation or a hint.
136#[component]
137pub async fn notice(message: &str) -> Result<impl View> {
138 Ok(view! { <div class="notice" role="status">(message)</div> })
139}
140
141/// A section with nothing in it yet: a title, then what to do next.
142#[component]
143pub async fn empty_state(title: &str, #[default] child: Child<'_>) -> Result<impl View> {
144 Ok(view! {
145 <section class="empty-state">
146 <h2>(title)</h2>
147 (child)
148 </section>
149 })
150}
151
152/// Turns a rendered view into the response of a handler. For a handler that answers with a view on one
153/// path and with a redirect or JSON on another.
154pub async fn respond(cx: &Cx, view: impl View) -> Result<Response> {
155 let handle = view.first().await?;
156 handle.into_response(cx)
157}