| 1 | use topcoat::{ |
| 2 | Result, |
| 3 | context::Cx, |
| 4 | router::{StatusCode, layout, uri}, |
| 5 | tailwind, |
| 6 | view::{View, component, view}, |
| 7 | }; |
| 8 | |
| 9 | use crate::application::Identity; |
| 10 | |
| 11 | use super::{ |
| 12 | atom, |
| 13 | context::identity, |
| 14 | csrf::{CsrfRefused, REFUSED, csrf_field}, |
| 15 | }; |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | #[layout("/")] |
| 31 | async fn root_layout(cx: &Cx, slot: Result) -> Result { |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 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 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | let feed = atom::discovered(uri(cx).path()); |
| 60 | |
| 61 | view! { shell(viewer: &viewer, feed: &feed, child: content) } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | #[component] |
| 69 | async 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 | <link rel="stylesheet" href=(tailwind::stylesheet!()) /> |
| 82 | match feed { |
| 83 | Some((href, title)) => <link |
| 84 | rel="alternate" |
| 85 | type="application/atom+xml" |
| 86 | title=(title.as_str()) |
| 87 | href=(href.as_str()) |
| 88 | />, |
| 89 | None => "", |
| 90 | } |
| 91 | topcoat::dev::script() |
| 92 | </head> |
| 93 | <body> |
| 94 | top_bar(viewer: viewer) |
| 95 | <main class="px-6 py-10">(child)</main> |
| 96 | </body> |
| 97 | </html> |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// The one-line global bar: where you are signed in, or the way in. |
| 102 | /// |
| 103 | /// Deliberately not an app shell. Steid's visitors are anonymous readers of someone's |
| 104 | /// site, so the chrome stays a slim bar and real navigation lives with the thing being |
| 105 | /// navigated — the profile's tab bar, the repository's — rather than in a global rail. |
| 106 | /// |
| 107 | /// Everything on the signed-in side is owner furniture; a visitor sees only "Sign in", |
| 108 | /// and the wordmark, which goes to `/` — the owner's profile, by the front-door rule. |
| 109 | #[component] |
| 110 | async fn top_bar(viewer: &Option<Identity>) -> Result { |
| 111 | view! { |
| 112 | <header class="border-b border-border"> |
| 113 | <div class="flex h-11 items-center px-6 text-[13px]"> |
| 114 | <a href="/" class="font-mono font-semibold tracking-tight">"steid"</a> |
| 115 | <div class="ml-auto flex items-center gap-5"> |
| 116 | match viewer { |
| 117 | Some(me) => { |
| 118 | <a |
| 119 | href=(format!("/{}", me.handle)) |
| 120 | class="font-mono text-muted-foreground hover:text-foreground" |
| 121 | >"@" (me.handle.as_str())</a> |
| 122 | <a |
| 123 | href=(format!("/{}/settings", me.handle)) |
| 124 | class="text-muted-foreground hover:text-foreground" |
| 125 | >"Settings"</a> |
| 126 | <form method="post" action="/auth/logout"> |
| 127 | csrf_field() |
| 128 | <button |
| 129 | type="submit" |
| 130 | class="cursor-pointer text-muted-foreground hover:text-foreground" |
| 131 | >"Sign out"</button> |
| 132 | </form> |
| 133 | } |
| 134 | None => { |
| 135 | <a |
| 136 | href="/auth/login" |
| 137 | class="text-muted-foreground hover:text-foreground" |
| 138 | >"Sign in"</a> |
| 139 | } |
| 140 | } |
| 141 | </div> |
| 142 | </div> |
| 143 | </header> |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /// The centered column for prose-shaped pages: profiles, forms, settings, writing. |
| 148 | #[component] |
| 149 | pub(super) async fn narrow(#[default] child: View) -> Result { |
| 150 | view! { <div class="mx-auto w-full max-w-3xl">(child)</div> } |
| 151 | } |
| 152 | |
| 153 | /// The full working width for code-shaped pages: trees, blobs, logs. |
| 154 | /// |
| 155 | /// Info density is the point — a file view in a 48rem column wastes the screen the |
| 156 | /// reader sized their window to. |
| 157 | #[component] |
| 158 | pub(super) async fn wide(#[default] child: View) -> Result { |
| 159 | view! { <div class="mx-auto w-full max-w-screen-xl">(child)</div> } |
| 160 | } |