steid

@jamesgill /

1use topcoat::{
2 Result,
3 context::Cx,
4 router::{StatusCode, layout, uri},
5 tailwind,
6 view::{View, component, view},
7};
8
9use crate::application::Identity;
10
11use super::{
12 atom,
13 context::identity,
14 csrf::{CsrfRefused, REFUSED, csrf_field},
15};
16
17/// The HTML shell every page renders inside.
18///
19/// A layout rather than a helper function: `view!` needs the request context in scope,
20/// so a plain `fn shell(..)` cannot build one. Layouts wrap by path prefix, and this
21/// one is at `/`, so it wraps everything.
22///
23/// `class="dark"` opts the whole document into the dark palette. Steid is dark-first —
24/// developers live there — and light mode is a later toggle rather than the default.
25///
26/// The shell imposes no width: prose-shaped pages wrap themselves in [`narrow`] and
27/// code-shaped ones in [`wide`], because a repository tree and a settings form do not
28/// want the same column. A page that forgets a container renders full-bleed, which is
29/// immediately visible — the same reasoning as request helpers being functions.
30#[layout("/")]
31async fn root_layout(cx: &Cx, slot: Result) -> Result {
32 // A refused form submission is the one failure answered with a page rather than a
33 // status alone, and it is caught here because a `#[page]` handler cannot return
34 // both a status and a view any other way — the router renders only its own error
35 // types, so `CsrfRefused` reaching it would be a 500. See `web::csrf`.
36 let content = match slot {
37 Ok(content) => content,
38 Err(error) if error.downcast_ref::<CsrfRefused>().is_some() => {
39 let viewer = identity(cx).await?;
40 let no_feed = None;
41
42 return view! {
43 (StatusCode::FORBIDDEN)
44 shell(
45 viewer: &viewer,
46 feed: &no_feed,
47 child: view! { narrow(<h1>"Refused"</h1><p>(REFUSED)</p>) }?,
48 )
49 };
50 }
51 Err(error) => return Err(error),
52 };
53
54 let viewer = identity(cx).await?;
55 // Feed autodiscovery is derived from the request's path rather than contributed by
56 // the page. Only a *layer* may put a value on the request context, so a page has
57 // nowhere to leave one for the layout to pick up — see `atom::discovered`, which
58 // holds the knowledge of which URLs have feeds, with tests.
59 let feed = atom::discovered(uri(cx).path());
60
61 view! { shell(viewer: &viewer, feed: &feed, child: content) }
62}
63
64/// The document itself.
65///
66/// Split out of [`root_layout`] so the CSRF refusal above can render inside the same
67/// chrome as every other page rather than as a bare line of text.
68#[component]
69async fn shell(
70 viewer: &Option<Identity>,
71 feed: &Option<(String, String)>,
72 #[default] child: View,
73) -> Result {
74 view! {
75 <!DOCTYPE html>
76 <html lang="en" class="dark">
77 <head>
78 <meta charset="utf-8" />
79 <meta name="viewport" content="width=device-width, initial-scale=1" />
80 <title>"steid"</title>
81 <meta name="description" content="A version control server that is entirely yours." />
82 <link rel="stylesheet" href=(tailwind::stylesheet!()) />
83 match feed {
84 Some((href, title)) => <link
85 rel="alternate"
86 type="application/atom+xml"
87 title=(title.as_str())
88 href=(href.as_str())
89 />,
90 None => "",
91 }
92 topcoat::dev::script()
93 </head>
94 <body>
95 top_bar(viewer: viewer)
96 <main class="px-6 py-10">(child)</main>
97 </body>
98 </html>
99 }
100}
101
102/// The one-line global bar: where you are signed in, or the way in.
103///
104/// Deliberately not an app shell. Steid's visitors are anonymous readers of someone's
105/// site, so the chrome stays a slim bar and real navigation lives with the thing being
106/// navigated — the profile's tab bar, the repository's — rather than in a global rail.
107///
108/// Everything on the signed-in side is owner furniture; a visitor sees only "Sign in",
109/// and the wordmark, which goes to `/` — the owner's profile, by the front-door rule.
110#[component]
111async fn top_bar(viewer: &Option<Identity>) -> Result {
112 view! {
113 <header class="border-b border-border">
114 <div class="flex h-11 items-center px-6 text-[13px]">
115 <a href="/" class="font-mono font-semibold tracking-tight">"steid"</a>
116 <div class="ml-auto flex items-center gap-5">
117 match viewer {
118 Some(me) => {
119 <a
120 href=(format!("/{}", me.handle))
121 class="font-mono text-muted-foreground hover:text-foreground"
122 >"@" (me.handle.as_str())</a>
123 <a
124 href=(format!("/{}/settings", me.handle))
125 class="text-muted-foreground hover:text-foreground"
126 >"Settings"</a>
127 <form method="post" action="/auth/logout">
128 csrf_field()
129 <button
130 type="submit"
131 class="cursor-pointer text-muted-foreground hover:text-foreground"
132 >"Sign out"</button>
133 </form>
134 }
135 None => {
136 <a
137 href="/auth/login"
138 class="text-muted-foreground hover:text-foreground"
139 >"Sign in"</a>
140 }
141 }
142 </div>
143 </div>
144 </header>
145 }
146}
147
148/// The centered column for prose-shaped pages: profiles, forms, settings, writing.
149#[component]
150pub(super) async fn narrow(#[default] child: View) -> Result {
151 view! { <div class="mx-auto w-full max-w-3xl">(child)</div> }
152}
153
154/// The full working width for code-shaped pages: trees, blobs, logs.
155///
156/// Info density is the point — a file view in a 48rem column wastes the screen the
157/// reader sized their window to.
158#[component]
159pub(super) async fn wide(#[default] child: View) -> Result {
160 view! { <div class="mx-auto w-full max-w-screen-xl">(child)</div> }
161}