forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

mod.rs177 lines, 6.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::view::{Child, View, component, view};
8
9use crate::assets::{DRIVE_JS_HREF, MAIN_CSS_HREF, NAV_JS_HREF, PAGES_JS_HREF, VIEWER_JS_HREF};
10use crate::auth::Viewer;
11use crate::family::FamilyLink;
12use crate::i18n::Lang;
13use crate::i18n::admin::{AK, a};
14use crate::i18n::drive::{DK, d};
15use crate::i18n::settings::{SK as SettingsK, s as settings_s};
16use crate::i18n::share::{SK, s as share_s};
17use crate::i18n::shell::{K, t};
18use crate::views::drive::quota_bar;
19
20pub mod drive;
21
22/// What the shell shows about the signed-in person: the name, the administrator badge, and the CSRF
23/// token of the session for the sign-out form.
24pub struct ViewerInfo {
25 /// The name shown in the topbar.
26 pub name: String,
27 /// Whether the topbar shows the administrator badge.
28 pub is_admin: bool,
29 /// The session's CSRF token, which the sign-out form sends.
30 pub csrf: String,
31 /// Bytes the viewer's files use, trashed ones included.
32 pub used: i64,
33 /// The viewer's quota in bytes.
34 pub quota: i64,
35 /// The viewer's theme: `system`, `light` or `dark`. It goes on `<html data-theme>`.
36 pub theme: String,
37 /// The family switcher list, as the family worker last fetched it.
38 pub family: Vec<FamilyLink>,
39}
40
41impl ViewerInfo {
42 /// What the shell needs to know about a signed-in viewer. `family` is the switcher list.
43 pub fn of(viewer: &Viewer, family: Vec<FamilyLink>) -> Self {
44 Self {
45 name: viewer.member.name.clone(),
46 is_admin: viewer.member.is_admin,
47 csrf: viewer.session.csrf_token.clone(),
48 used: viewer.member.used_bytes,
49 quota: viewer.member.quota_bytes,
50 theme: viewer.member.theme.clone(),
51 family,
52 }
53 }
54}
55
56/// The HTML document around a page. `title` is the page's own title; the document title adds the
57/// app name. `child` is the page, placed in `<main>`. `drive_js` adds the upload script, which only the
58/// drive pages need.
59///
60/// The topbar has the wordmark on the left, the service switcher in the middle (this service's pages, then
61/// the other family services from the family worker) and the viewer's user menu on the right. The menu's
62/// sign-out form opts out of soft navigation, because it ends in a redirect to `im`. `nav.js` reads the
63/// 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] drive_js: bool,
70 #[default] child: Child<'_>,
71) -> Result<impl View> {
72 let document_title = format!("{title} \u{b7} in");
73 let theme = viewer.map_or("system", |v| v.theme.as_str());
74 Ok(view! {
75 <!DOCTYPE html>
76 <html lang=(lang.code()) data-theme=(theme)>
77 <head>
78 <meta charset="utf-8">
79 <meta name="viewport" content="width=device-width, initial-scale=1">
80 <title>(document_title)</title>
81 <link rel="stylesheet" href=(MAIN_CSS_HREF)>
82 <script src=(NAV_JS_HREF) defer=""></script>
83 <script src=(PAGES_JS_HREF) defer=""></script>
84 <script src=(VIEWER_JS_HREF) defer=""></script>
85 if drive_js {
86 <script src=(DRIVE_JS_HREF) defer=""></script>
87 }
88 </head>
89 <body data-restart-text=(t(lang, K::ServerRestarting))>
90 <a class="skip-link" href="#main">(t(lang, K::SkipToContent))</a>
91 <header class="topbar">
92 <a class="topbar-brand" href="/">"in"</a>
93 <nav class="switcher" aria-label=(t(lang, K::Services))>
94 if viewer.is_some() {
95 <a href="/drive">(d(lang, DK::Drive))</a>
96 <a href="/shared">(share_s(lang, SK::NavShared))</a>
97 <a href="/links">(share_s(lang, SK::NavLinks))</a>
98 <a href="/trash">(d(lang, DK::Trash))</a>
99 if let Some(viewer) = viewer {
100 for link in &viewer.family {
101 switch_link(name: link.name.as_str(), url: link.url.as_str())
102 }
103 }
104 }
105 </nav>
106 <div class="topbar-end">
107 if let Some(viewer) = viewer {
108 quota_bar(lang: lang, used: viewer.used, quota: viewer.quota)
109 <details class="user-menu">
110 <summary>(viewer.name.as_str())</summary>
111 <a href="/settings">(s_title(lang))</a>
112 if viewer.is_admin {
113 <a href="/admin">(a_title(lang))</a>
114 }
115 <form method="post" action="/auth/logout" data-hard="">
116 <input type="hidden" name="csrf" value=(viewer.csrf.as_str())>
117 <button class="button" type="submit">(t(lang, K::SignOut))</button>
118 </form>
119 </details>
120 if viewer.is_admin {
121 <span class="badge accent">(t(lang, K::Admin))</span>
122 }
123 }
124 </div>
125 </header>
126 <main id="main">(child)</main>
127 </body>
128 </html>
129 })
130}
131
132/// The menu link to the settings page, in the viewer's language.
133fn s_title(lang: Lang) -> &'static str {
134 settings_s(lang, SettingsK::Title)
135}
136
137/// The menu link to the administration page, in the viewer's language.
138fn a_title(lang: Lang) -> &'static str {
139 a(lang, AK::Title)
140}
141
142/// One other family service in the switcher. The name and the URL are escaped by `view!`, and the URL was
143/// checked by `family::usable` before it was stored.
144#[component]
145pub async fn switch_link(name: &str, url: &str) -> Result<impl View> {
146 Ok(view! { <a class="switch-link" href=(url) rel="noopener">(name)</a> })
147}
148
149/// An error message for a failed action. Forms show it above their fields.
150#[allow(dead_code)] // The first form that can fail a check uses it.
151#[component]
152pub async fn alert(message: &str) -> Result<impl View> {
153 Ok(view! { <div class="alert" role="alert">(message)</div> })
154}
155
156/// A plain message that is not an error, such as a confirmation or a hint.
157#[component]
158pub async fn notice(message: &str) -> Result<impl View> {
159 Ok(view! { <div class="notice" role="status">(message)</div> })
160}
161
162/// Something to look at that is not a failure yet, such as a state that has gone stale.
163#[component]
164pub async fn warning(message: &str) -> Result<impl View> {
165 Ok(view! { <div class="warning" role="status">(message)</div> })
166}
167
168/// A section with nothing in it yet: a title, then what to do next.
169#[component]
170pub async fn empty_state(title: &str, #[default] child: Child<'_>) -> Result<impl View> {
171 Ok(view! {
172 <section class="empty-state">
173 <h2>(title)</h2>
174 (child)
175 </section>
176 })
177}