steid

@jamesgill /

Rust14.3 KBCode·Blame·History·Raw
1//! The profile page — `/{handle}`.
2//!
3//! Public. This is the first page that renders for anonymous visitors, so everything it
4//! shows comes from [`PublicProfile`](crate::application::PublicProfile), which has no
5//! private fields to leak.
6
7use topcoat::{
8 Result,
9 context::Cx,
10 router::{
11 error::{RouterErrorExt, not_found},
12 page, path_param,
13 },
14 view::{component, view},
15};
16
17use crate::{
18 application::{PublicProfile, RepoSummary, list_repos, view_profile},
19 components::{
20 badge::{BadgeVariant, badge},
21 button::{ButtonSize, ButtonVariant, button_variants},
22 },
23 domain::{ImportState, OrgName},
24};
25
26use super::{
27 browse::ago,
28 context::{current_actor, memberships, orgs, repos, server_error},
29 layout::narrow,
30};
31
32/// `{handle}` from the path. The struct name snake-cased is the parameter name.
33///
34/// Declared as `str` so the raw segment arrives unparsed — validation is `OrgName`'s
35/// job, and a handle that fails it is a page that does not exist rather than a bad
36/// request.
37#[path_param]
38struct Handle(str);
39
40/// Resolves `{handle}` from the path, or 404.
41///
42/// A malformed handle 404s rather than erroring: `/Not A Handle` is a page that does
43/// not exist, not a bad request.
44///
45/// Shared with `/api`, which resolves the same segment without wanting a profile —
46/// listing repositories under a handle needs the handle and nothing else.
47pub(super) fn handle_param(cx: &Cx) -> Result<OrgName> {
48 Ok(OrgName::new(path_param::<Handle>(cx)).map_err(|_| not_found())?)
49}
50
51/// Resolves `{handle}` from the path into a profile, or 404.
52pub(super) async fn profile_for(cx: &Cx) -> Result<PublicProfile> {
53 let handle = handle_param(cx)?;
54 let actor = current_actor(cx).await?;
55
56 Ok(view_profile(&handle, &actor, &orgs(cx), &memberships(cx))
57 .await
58 .map_err(server_error)?
59 .ok_or_not_found()?)
60}
61
62/// How many repositories the overview previews before deferring to the index.
63///
64/// Small on purpose. The overview is a shopfront, not an inventory — the tab is where
65/// completeness lives.
66const PREVIEW: usize = 5;
67
68/// Which tab is lit.
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub(super) enum Tab {
71 Overview,
72 Repositories,
73}
74
75/// The identity block and the tab bar, shared by every page that is *about a handle*
76/// rather than about a thing inside one.
77///
78/// Flat navigation rather than stacked sections: an empty section stops being a visible
79/// failure on the front page and becomes a destination that happens to be empty. See
80/// `plans/ui.md`.
81/// The parameter is `identity`, not `profile`: the `#[page] async fn profile` below puts
82/// a unit struct of that name in module scope, which shadows a parameter sharing it.
83#[component]
84pub(super) async fn profile_chrome(
85 identity: &PublicProfile,
86 active: Tab,
87 repo_count: usize,
88) -> Result {
89 let handle = identity.handle.as_str();
90 let overview = format!("/{handle}");
91 let repositories = format!("/{handle}/repos");
92 let settings = format!("/{handle}/settings");
93 let count = repo_count.to_string();
94
95 view! {
96 <header>
97 <h1 class="text-xl font-semibold tracking-tight">(&identity.label)</h1>
98 <p class="mt-0.5 font-mono text-[13px] text-muted-foreground">"@" (handle)</p>
99 ({
100 match &identity.bio {
101 Some(bio) => view! {
102 <p class="mt-3.5 max-w-lg text-sm leading-relaxed text-muted-foreground">(bio)</p>
103 },
104 None => view! {},
105 }
106 }?)
107 </header>
108
109 <nav class="mt-8 flex items-center gap-6 border-b border-border text-[13px]">
110 tab_link(
111 href: overview.as_str(),
112 label: "Overview",
113 count: "",
114 active: active == Tab::Overview,
115 )
116 tab_link(
117 href: repositories.as_str(),
118 label: "Repositories",
119 count: count.as_str(),
120 active: active == Tab::Repositories,
121 )
122
123 if identity.viewer_is_owner {
124 <a
125 href=(settings.as_str())
126 class="ml-auto -mb-px border-b-2 border-transparent pb-2.5 text-muted-foreground hover:text-foreground"
127 >"Settings"</a>
128 }
129 </nav>
130 }
131}
132
133/// One tab. A plain link, so every tab is a real URL and none of this needs JavaScript.
134#[component]
135async fn tab_link(href: &str, label: &str, count: &str, active: bool) -> Result {
136 view! {
137 <a
138 href=(href)
139 class=(if active {
140 "-mb-px border-b-2 border-foreground pb-2.5 font-medium text-foreground"
141 } else {
142 "-mb-px border-b-2 border-transparent pb-2.5 text-muted-foreground hover:text-foreground"
143 })
144 >
145 (label)
146 if !count.is_empty() {
147 <span class="ml-1.5 text-xs text-muted-foreground/60">(count)</span>
148 }
149 </a>
150 }
151}
152
153/// The pinned repository, given weight by everything except size.
154///
155/// Eyebrow, a heavier weight, a roomier description and a rule beneath it —
156/// `plans/ui.md` records why scale is deliberately not one of the devices.
157#[component]
158async fn lead(handle: &str, repo: &RepoSummary) -> Result {
159 view! {
160 <section class="mt-8 border-b border-border/60 pb-8">
161 <p class="text-[11px] font-medium uppercase tracking-[0.1em] text-muted-foreground/70">
162 "Currently building"
163 </p>
164 <div class="mt-2 flex items-baseline gap-2">
165 <a
166 href=(format!("/{handle}/repos/{}", repo.name))
167 class="text-sm font-semibold hover:underline"
168 >(repo.name.as_str())</a>
169 if !repo.visibility.is_public() {
170 badge(variant: BadgeVariant::Outline, "Private")
171 }
172 if repo.archived {
173 badge(variant: BadgeVariant::Outline, "Archived")
174 }
175 match repo.import_state {
176 Some(state) => badge(
177 variant: BadgeVariant::Outline,
178 (import_label(state))
179 ),
180 None => "",
181 }
182 </div>
183 ({
184 match &repo.description {
185 Some(description) => view! {
186 <p class="mt-2 max-w-lg text-sm leading-relaxed text-muted-foreground">
187 (description)
188 </p>
189 },
190 None => view! {},
191 }
192 }?)
193 <p class="mt-2.5 font-mono text-[11px] text-muted-foreground/80">
194 "updated " (ago(repo.updated_at))
195 </p>
196 </section>
197 }
198}
199
200/// What a listing row says about an import.
201///
202/// "Importing" on a row that failed an hour ago would be a lie, and the two are equally
203/// worth a badge: one says wait, the other says do something.
204fn import_label(state: ImportState) -> &'static str {
205 match state {
206 ImportState::Importing => "Importing",
207 ImportState::Failed => "Import failed",
208 }
209}
210
211/// A flat list of repositories. Rows and hairlines, never boxes.
212#[component]
213pub(super) async fn repo_rows(handle: &str, repos: &[RepoSummary]) -> Result {
214 view! {
215 <div class="mt-1">
216 for repo in repos {
217 <a
218 href=(format!("/{handle}/repos/{}", repo.name))
219 class="group -mx-2.5 flex items-baseline justify-between gap-4 rounded px-2.5 py-2.5 hover:bg-surface"
220 >
221 <div>
222 <div class="flex items-baseline gap-2">
223 <span class="text-sm font-medium group-hover:underline">
224 (repo.name.as_str())
225 </span>
226 if !repo.visibility.is_public() {
227 badge(variant: BadgeVariant::Outline, "Private")
228 }
229 if repo.archived {
230 badge(variant: BadgeVariant::Outline, "Archived")
231 }
232 // A row that leads nowhere useful yet says so here rather
233 // than only on the page behind it: a listing is where an
234 // owner notices an import that has not landed.
235 match repo.import_state {
236 Some(state) => badge(
237 variant: BadgeVariant::Outline,
238 (import_label(state))
239 ),
240 None => "",
241 }
242 </div>
243 ({
244 match &repo.description {
245 Some(description) => view! {
246 <p class="mt-0.5 text-[13px] leading-snug text-muted-foreground">
247 (description)
248 </p>
249 },
250 None => view! {},
251 }
252 }?)
253 </div>
254 <span class="shrink-0 font-mono text-[11px] text-muted-foreground">
255 (ago(repo.updated_at))
256 </span>
257 </a>
258 }
259 </div>
260 }
261}
262
263#[page("/{handle}")]
264async fn profile(cx: &Cx) -> Result {
265 let profile = profile_for(cx).await?;
266
267 // The handle resolved above, so `None` here is impossible; an empty list is the
268 // right answer either way rather than a 500.
269 let listed = list_repos(
270 &profile.handle,
271 &current_actor(cx).await?,
272 &orgs(cx),
273 &memberships(cx),
274 &repos(cx),
275 )
276 .await
277 .map_err(server_error)?
278 .unwrap_or_default();
279
280 // The lead comes out of the listing rather than a second query, so the two can
281 // never disagree about which repository is pinned.
282 let lead_repo = listed.iter().find(|repo| repo.pinned).cloned();
283 let rest: Vec<RepoSummary> = listed
284 .iter()
285 .filter(|repo| !repo.pinned)
286 .take(PREVIEW)
287 .cloned()
288 .collect();
289 let more = listed
290 .len()
291 .saturating_sub(rest.len() + usize::from(lead_repo.is_some()));
292 let handle = profile.handle.to_string();
293
294 view! {
295 narrow(
296 profile_chrome(identity: &profile, active: Tab::Overview, repo_count: listed.len())
297
298 ({
299 match &lead_repo {
300 Some(repo) => view! { lead(handle: handle.as_str(), repo: repo) },
301 None => view! {},
302 }
303 }?)
304
305 if !rest.is_empty() {
306 <section class="mt-8">
307 <div class="flex items-baseline justify-between">
308 <h2 class="text-[11px] font-medium uppercase tracking-[0.1em] text-muted-foreground/70">
309 "Repositories"
310 </h2>
311 if more > 0 {
312 <a
313 href=(format!("/{handle}/repos"))
314 class="text-xs text-muted-foreground hover:text-foreground"
315 >"All " (listed.len().to_string()) " \u{2192}"</a>
316 }
317 </div>
318 repo_rows(handle: handle.as_str(), repos: &rest)
319 </section>
320 }
321
322 // Nothing to show. A visitor gets silence rather than an empty box advertising
323 // incompleteness; the owner gets the way to fix it.
324 if listed.is_empty() {
325 if profile.viewer_is_owner {
326 <div class="mt-8">
327 <p class="text-sm text-muted-foreground">"No repositories yet."</p>
328 <p class="mt-3">
329 <a
330 href=(format!("/{handle}/repos/new"))
331 class=(button_variants(ButtonVariant::Outline, ButtonSize::Sm))
332 >"New repository"</a>
333 </p>
334 </div>
335 }
336 }
337 )
338 }
339}
340
341/// Every repository under a handle that the viewer may see.
342///
343/// The tab's destination, and where completeness lives — the overview deliberately
344/// previews only [`PREVIEW`] of them.
345#[page("/{handle}/repos")]
346async fn repos_index(cx: &Cx) -> Result {
347 // Not `profile`: the `#[page] async fn profile` above is a unit struct in this
348 // module, so `let profile = …` parses as a unit-struct pattern rather than a new
349 // binding. The error names neither the page nor the shadowing.
350 let identity = profile_for(cx).await?;
351
352 let listed = list_repos(
353 &identity.handle,
354 &current_actor(cx).await?,
355 &orgs(cx),
356 &memberships(cx),
357 &repos(cx),
358 )
359 .await
360 .map_err(server_error)?
361 .unwrap_or_default();
362
363 let handle = identity.handle.to_string();
364 let new_repo = format!("/{handle}/repos/new");
365
366 view! {
367 narrow(
368 profile_chrome(identity: &identity, active: Tab::Repositories, repo_count: listed.len())
369
370 if listed.is_empty() {
371 <p class="mt-8 text-sm text-muted-foreground">"No repositories yet."</p>
372 if identity.viewer_is_owner {
373 <p class="mt-3">
374 <a
375 href=(new_repo.as_str())
376 class=(button_variants(ButtonVariant::Outline, ButtonSize::Sm))
377 >"New repository"</a>
378 </p>
379 }
380 } else {
381 <div class="mt-6">
382 if identity.viewer_is_owner {
383 <p class="mb-2 flex justify-end">
384 <a
385 href=(new_repo.as_str())
386 class=(button_variants(ButtonVariant::Outline, ButtonSize::Sm))
387 >"New repository"</a>
388 </p>
389 }
390 repo_rows(handle: handle.as_str(), repos: &listed)
391 </div>
392 }
393 )
394 }
395}