Landing: categories beside the latest topics, in Classic and Focus; the categories page moves to /c/

644543b · gmorell · 2026-12-03 02:02

21 files +285 -14
Message
{commit_body(@commit)}

Files changed

modified src/handlers/focus.rs
+3 −2
@@ -18,7 +18,8 @@
18 18 //! Escape closes (keys in `assets/f/js/src.js`). Modelled on cartosnap
19 19 //! `share_live.ex` (`?photo=`, filmstrip, `phx-window-keydown` stepping) and
20 20 //! pin_flip's lightbox (counter, prev/next, Escape, click outside).
21 −//! * `/?view=focus` — the home page as a grid of top-level category tiles.
21 +//! * `/c/?view=focus` — the categories page as a grid of top-level category tiles.
22 +//! * `/?view=focus` — the landing: categories beside the latest topics as tiles (`landing.rs`).
22 23 //!
23 24 //! Everything is in the URL. Navigation is ordinary hx-boost (only `#content`
24 25 //! swaps); the reader's links swap just `#reader` (the handler answers a
@@ -547,7 +548,7 @@ pub(crate) fn category_rail(
547 548 .collect();
548 549 let up = match category.parent_id.and_then(|p| tree.get(p)) {
549 550 Some(p) => UpLink { name: p.name.clone(), href: focus_url(&p.slug, sort) },
550 − None => UpLink { name: "All categories".into(), href: "/?view=focus".into() },
551 + None => UpLink { name: "All categories".into(), href: "/c/?view=focus".into() },
551 552 };
552 553 (up, rail)
553 554 }
modified src/handlers/forum.rs
+4 −2
@@ -438,8 +438,10 @@ async fn username_of(state: &AppState, id: Option<Uuid>) -> Option<String> {
438 438 }
439 439
440 440
441 −/// Forum home — list categories with their topic counts.
442 −pub async fn home(
441 +/// The categories page (`/c/`): every visible category with its
442 +/// subcategories and topic counts, or category tiles in Focus. This was the
443 +/// landing page; `/` is now [`super::landing::home`].
444 +pub async fn categories_index(
443 445 State(state): State<AppState>,
444 446 Query(q): Query<HomeQuery>,
445 447 headers: HeaderMap,
added src/handlers/landing.rs
+158 −0
@@ -0,0 +1,158 @@
1 +//! The landing page (`/`): two columns in both layouts. Categories on the
2 +//! left (the visible top level, each with its visible children), the latest
3 +//! topics across everything the viewer can see on the right. Classic shows
4 +//! the latest as the usual topic table; Focus as tiles, with the reader
5 +//! (`&topic=<id>`) stepping through that same list.
6 +//!
7 +//! The full categories page that used to be the landing lives at `/c/`
8 +//! ([`super::forum::categories_index`]).
9 +
10 +use std::collections::HashSet;
11 +
12 +use axum::extract::{Query, State};
13 +use axum::http::{HeaderMap, StatusCode};
14 +use axum::response::Response;
15 +use minijinja::context;
16 +use sea_orm::*;
17 +use serde::{Deserialize, Serialize};
18 +use uuid::Uuid;
19 +
20 +use super::focus::{self, subtree_count, topic_counts, ViewMode};
21 +use super::forum::{build_topic_rows, ise, viewer_of, TopicSort};
22 +use crate::category_tree::{color_widget_css, CategoryTree};
23 +use crate::entities::{topics, users};
24 +use crate::middleware::UserContext;
25 +use crate::AppState;
26 +
27 +/// How many topics the latest column shows.
28 +pub(crate) const LATEST_LIMIT: u64 = 30;
29 +
30 +#[derive(Debug, Deserialize)]
31 +pub struct LandingQuery {
32 + /// `classic` or `focus` (or the old `list`/`visual`); see
33 + /// [`focus::resolve_view`].
34 + pub view: Option<String>,
35 + /// Focus: open the reader at this topic.
36 + pub topic: Option<Uuid>,
37 +}
38 +
39 +#[derive(Serialize)]
40 +struct SideCategory {
41 + name: String,
42 + slug: String,
43 + href: String,
44 + widget_bg: String,
45 + count: u64,
46 + children: Vec<SideCategory>,
47 +}
48 +
49 +/// The newest-activity live topics in categories the viewer can see.
50 +pub(crate) async fn latest_topics(
51 + state: &AppState,
52 + vis: &HashSet<Uuid>,
53 + limit: u64,
54 +) -> Result<Vec<(topics::Model, Option<users::Model>)>, StatusCode> {
55 + if vis.is_empty() {
56 + return Ok(Vec::new());
57 + }
58 + topics::Entity::find()
59 + .filter(topics::Column::CategoryId.is_in(vis.iter().copied()))
60 + .filter(topics::Column::DeletedAt.is_null())
61 + .order_by_desc(topics::Column::BumpedAt)
62 + .limit(limit)
63 + .find_also_related(users::Entity)
64 + .all(state.db.as_ref())
65 + .await
66 + .map_err(ise("load latest topics"))
67 +}
68 +
69 +pub async fn home(
70 + State(state): State<AppState>,
71 + Query(q): Query<LandingQuery>,
72 + headers: HeaderMap,
73 + user_context: Option<UserContext>,
74 +) -> Result<Response, StatusCode> {
75 + let uc = user_context.as_ref();
76 + let view = focus::view_for(&state, q.view.as_deref(), uc, &headers);
77 + let is_focus = view.mode == ViewMode::Focus;
78 +
79 + let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load category tree"))?;
80 + let vis = tree.visible_category_ids(viewer_of(&user_context));
81 + let loaded = latest_topics(&state, &vis, LATEST_LIMIT).await?;
82 +
83 + // Focus: the reader's own htmx requests need only #reader.
84 + let base = "/?view=focus".to_string();
85 + if is_focus && focus::wants_reader(&headers) {
86 + let (_, reader) = focus::tiles_and_reader(&state, &tree, loaded, uc, &headers, q.topic).await?;
87 + return Ok(view.respond(focus::reader_fragment(&state, reader, &base, uc)?));
88 + }
89 +
90 + let counts = topic_counts(&state).await?;
91 + let sort = uc
92 + .and_then(|c| TopicSort::parse(&c.user.preferred_sort))
93 + .unwrap_or(TopicSort::Latest);
94 + let href = |slug: &str| {
95 + if is_focus { focus::focus_url(slug, sort) } else { format!("/c/{slug}") }
96 + };
97 + let side = |id: Uuid| -> Option<SideCategory> {
98 + let c = tree.get(id)?;
99 + Some(SideCategory {
100 + name: c.name.clone(),
101 + slug: c.slug.clone(),
102 + href: href(&c.slug),
103 + widget_bg: color_widget_css(&tree.widget_colors(id)),
104 + count: subtree_count(&tree, &vis, &counts, id),
105 + children: Vec::new(),
106 + })
107 + };
108 + let categories: Vec<SideCategory> = tree
109 + .roots()
110 + .iter()
111 + .filter(|id| vis.contains(id))
112 + .filter_map(|&root| {
113 + let mut row = side(root)?;
114 + row.children = tree
115 + .children(root)
116 + .iter()
117 + .filter(|id| vis.contains(id))
118 + .filter_map(|&id| side(id))
119 + .collect();
120 + Some(row)
121 + })
122 + .collect();
123 +
124 + let user = uc.map(|c| c.user.clone());
125 + let can_write = uc.is_some_and(|c| super::may_write(&state, &c.user));
126 + let page = if is_focus {
127 + let (tiles, reader) = focus::tiles_and_reader(&state, &tree, loaded, uc, &headers, q.topic).await?;
128 + super::render(
129 + &state,
130 + "forum/landing_focus.html",
131 + context! {
132 + title => "Home",
133 + app_name => state.config.app_name,
134 + user => user,
135 + can_write => can_write,
136 + categories => categories,
137 + topics => tiles,
138 + reader => reader,
139 + base => base,
140 + },
141 + )?
142 + } else {
143 + let rows = build_topic_rows(&state, &tree, loaded).await?;
144 + super::render(
145 + &state,
146 + "forum/landing.html",
147 + context! {
148 + title => "Home",
149 + app_name => state.config.app_name,
150 + user => user,
151 + can_write => can_write,
152 + categories => categories,
153 + topics => rows,
154 + },
155 + )?
156 + };
157 + Ok(view.respond(page))
158 +}
modified src/handlers/mod.rs
+3 −1
@@ -5,6 +5,7 @@ pub mod categories;
5 5 pub mod devices;
6 6 pub mod focus;
7 7 pub mod forum;
8 +pub mod landing;
8 9 pub mod notifications;
9 10 pub mod posts;
10 11 pub mod reactions;
@@ -29,9 +30,10 @@ pub use categories::{
29 30 update_category,
30 31 };
31 32 pub use forum::{
32 − category_show, create_post, create_topic, delete_topic, home, new_topic_form, restore_topic,
33 + categories_index, category_show, create_post, create_topic, delete_topic, new_topic_form, restore_topic,
33 34 topic_show, update_topic_tags,
34 35 };
36 +pub use landing::home;
35 37 pub use posts::{
36 38 delete_post, edit_post_form, post_history, post_permalink, restore_post, update_post,
37 39 };
modified src/main.rs
+2 −0
@@ -85,6 +85,8 @@ async fn main() -> Result<(), anyhow::Error> {
85 85 let public_routes = Router::new()
86 86 .route("/", get(handlers::home))
87 87 .route("/healthz", get(healthz))
88 + .route("/c", get(handlers::categories_index))
89 + .route("/c/", get(handlers::categories_index))
88 90 .route("/c/{slug}", get(handlers::category_show))
89 91 .route("/t/{id}", get(handlers::topic_show))
90 92 .route("/p/{id}", get(handlers::post_permalink))
added templates/forum/_landing_categories.html
+40 −0

Click to load diff…

modified templates/forum/category.html
+1 −1

Click to load diff…

modified templates/forum/focus.html
+1 −1

Click to load diff…

modified templates/forum/home.html
+1 −1

Click to load diff…

modified templates/forum/home_visual.html
+1 −1

Click to load diff…

added templates/forum/landing.html
+24 −0

Click to load diff…

added templates/forum/landing_focus.html
+30 −0

Click to load diff…

modified templates/forum/new_topic.html
+1 −1

Click to load diff…

modified templates/forum/topic.html
+1 −1

Click to load diff…

modified templates/forum/topic_deleted.html
+1 −1

Click to load diff…

modified templates/forum/topic_focus.html
+1 −1

Click to load diff…

modified templates/root.html
+13 −1

Click to load diff…

Parents: e7d5438