Focus feeds get the rail: .. home, Latest, Popular, top-level categories

4074972 · gmorell · 2026-12-03 03:53

7 files +115 -37
Message
{commit_body(@commit)}

Files changed

modified src/handlers/focus.rs
+24 −1
@@ -206,6 +206,8 @@ pub(crate) struct RailItem {
206 206 slug: String,
207 207 href: String,
208 208 widget_bg: String,
209 + /// A Font Awesome icon shown instead of the colour widget (feeds).
210 + icon: &'static str,
209 211 count: u64,
210 212 current: bool,
211 213 }
@@ -542,6 +544,7 @@ pub(crate) fn category_rail(
542 544 .filter(|i| vis.contains(i))
543 545 .filter_map(|id| {
544 546 tree.get(id).map(|c| RailItem {
547 + icon: "",
545 548 name: c.name.clone(),
546 549 slug: c.slug.clone(),
547 550 href: focus_url(&c.slug, sort),
@@ -666,7 +669,26 @@ impl TopicTile {
666 669 }
667 670
668 671 pub(crate) fn rail_item(name: &str, slug: &str, href: String, count: u64, current: bool) -> RailItem {
669 − RailItem { name: name.into(), slug: slug.into(), href, widget_bg: String::new(), count, current }
672 + RailItem { name: name.into(), slug: slug.into(), href, widget_bg: String::new(), icon: "", count, current }
673 +}
674 +
675 +/// A rail item for a feed (`icon` instead of a colour widget).
676 +pub(crate) fn rail_feed(name: &str, slug: &str, href: String, icon: &'static str, count: u64, current: bool) -> RailItem {
677 + RailItem { name: name.into(), slug: slug.into(), href, widget_bg: String::new(), icon, count, current }
678 +}
679 +
680 +/// A rail item for a category, with its colour widget.
681 +pub(crate) fn rail_category(tree: &CategoryTree, id: Uuid, href: String, count: u64) -> Option<RailItem> {
682 + let c = tree.get(id)?;
683 + Some(RailItem {
684 + name: c.name.clone(),
685 + slug: c.slug.clone(),
686 + href,
687 + widget_bg: color_widget_css(&tree.widget_colors(id)),
688 + icon: "",
689 + count,
690 + current: false,
691 + })
670 692 }
671 693
672 694 pub(crate) fn up_link(name: &str, href: &str) -> UpLink {
@@ -867,6 +889,7 @@ pub async fn home_focus(
867 889 .filter(|i| vis.contains(i))
868 890 .filter_map(|&id| {
869 891 tree.get(id).map(|s| RailItem {
892 + icon: "",
870 893 name: s.name.clone(),
871 894 slug: s.slug.clone(),
872 895 href: focus_url(&s.slug, sort),
modified src/handlers/landing.rs
+89 −34
@@ -87,6 +87,72 @@ struct SideCategory {
87 87 children: Vec<SideCategory>,
88 88 }
89 89
90 +fn user_sort(uc: Option<&UserContext>) -> TopicSort {
91 + uc.and_then(|c| TopicSort::parse(&c.user.preferred_sort)).unwrap_or(TopicSort::Latest)
92 +}
93 +
94 +/// The landing's categories column: visible top-level
95 +/// categories, each with its visible children, linked in the current layout.
96 +fn side_categories(
97 + tree: &CategoryTree,
98 + vis: &HashSet<Uuid>,
99 + counts: &HashMap<Uuid, u64>,
100 + is_focus: bool,
101 + sort: TopicSort,
102 +) -> Vec<SideCategory> {
103 + let side = |id: Uuid| -> Option<SideCategory> {
104 + let c = tree.get(id)?;
105 + Some(SideCategory {
106 + name: c.name.clone(),
107 + slug: c.slug.clone(),
108 + href: if is_focus { focus::focus_url(&c.slug, sort) } else { format!("/c/{}", c.slug) },
109 + widget_bg: color_widget_css(&tree.widget_colors(id)),
110 + count: subtree_count(tree, vis, counts, id),
111 + children: Vec::new(),
112 + })
113 + };
114 + tree.roots()
115 + .iter()
116 + .filter(|id| vis.contains(id))
117 + .filter_map(|&root| {
118 + let mut row = side(root)?;
119 + row.children = tree
120 + .children(root)
121 + .iter()
122 + .filter(|id| vis.contains(id))
123 + .filter_map(|&id| side(id))
124 + .collect();
125 + Some(row)
126 + })
127 + .collect()
128 +}
129 +
130 +/// The Focus rail beside a feed: `..` home, both feeds (this one in focus),
131 +/// then the visible top-level categories.
132 +fn feed_rail(
133 + tree: &CategoryTree,
134 + vis: &HashSet<Uuid>,
135 + counts: &HashMap<Uuid, u64>,
136 + current: Feed,
137 + sort: TopicSort,
138 + lens: (usize, usize),
139 +) -> (focus::UpLink, Vec<focus::RailItem>) {
140 + let mut rail = vec![
141 + focus::rail_feed("Latest", "feed-latest", "/p/latest?view=focus".into(), Feed::Latest.icon(), lens.0 as u64, current == Feed::Latest),
142 + focus::rail_feed("Popular", "feed-popular", "/p/popular?view=focus".into(), Feed::Popular.icon(), lens.1 as u64, current == Feed::Popular),
143 + ];
144 + rail.extend(
145 + tree.roots()
146 + .iter()
147 + .filter(|id| vis.contains(id))
148 + .filter_map(|&id| {
149 + let slug = &tree.get(id)?.slug;
150 + focus::rail_category(tree, id, focus::focus_url(slug, sort), subtree_count(tree, vis, counts, id))
151 + }),
152 + );
153 + (focus::up_link("Home", "/?view=focus"), rail)
154 +}
155 +
90 156 /// The newest-activity live topics in categories the viewer can see.
91 157 pub(crate) async fn latest_topics(
92 158 state: &AppState,
@@ -205,13 +271,33 @@ async fn feed(
205 271 let vis = tree.visible_category_ids(viewer_of(&user_context));
206 272 let loaded = feed_topics(&state, &vis, feed, FEED_LIMIT as usize).await?;
207 273 let path = format!("/p/{}", feed.slug());
208 −
209 − if view.is_focus() {
274 + let is_focus = view.is_focus();
275 + if is_focus && focus::wants_reader(&headers) {
276 + let (_, reader) = focus::tiles_and_reader(&state, &tree, loaded, uc, &headers, q.topic).await?;
277 + let base = format!("{path}?view=focus");
278 + return Ok(view.respond(focus::reader_fragment(&state, reader, &base, uc)?));
279 + }
280 + if is_focus {
281 + let counts = topic_counts(&state).await?;
282 + let sort = user_sort(uc);
283 + // The other feed's size, for its square on the rail.
284 + let other = match feed {
285 + Feed::Latest => Feed::Popular,
286 + Feed::Popular => Feed::Latest,
287 + };
288 + let other_len = feed_topics(&state, &vis, other, FEED_LIMIT as usize).await?.len();
289 + let lens = match feed {
290 + Feed::Latest => (loaded.len(), other_len),
291 + Feed::Popular => (other_len, loaded.len()),
292 + };
293 + let rail = feed_rail(&tree, &vis, &counts, feed, sort, lens);
210 294 let (tiles, reader) = focus::tiles_and_reader(&state, &tree, loaded, uc, &headers, q.topic).await?;
211 295 let mut page = ListPage::new(feed.slug(), feed.heading(), path);
212 296 page.heading_icon = feed.icon();
213 297 page.subheading = Some(feed.blurb().into());
214 298 page.empty = "Nothing here yet.".into();
299 + page.rail = Some(rail);
300 + page.rail_label = "Feeds and categories";
215 301 let html = page.render(&state, tiles, reader, uc, &headers)?;
216 302 return Ok(view.respond(html));
217 303 }
@@ -280,38 +366,7 @@ pub async fn home(
280 366 }
281 367
282 368 let counts = topic_counts(&state).await?;
283 − let sort = uc
284 − .and_then(|c| TopicSort::parse(&c.user.preferred_sort))
285 − .unwrap_or(TopicSort::Latest);
286 − let href = |slug: &str| {
287 − if is_focus { focus::focus_url(slug, sort) } else { format!("/c/{slug}") }
288 − };
289 − let side = |id: Uuid| -> Option<SideCategory> {
290 − let c = tree.get(id)?;
291 − Some(SideCategory {
292 − name: c.name.clone(),
293 − slug: c.slug.clone(),
294 − href: href(&c.slug),
295 − widget_bg: color_widget_css(&tree.widget_colors(id)),
296 − count: subtree_count(&tree, &vis, &counts, id),
297 − children: Vec::new(),
298 − })
299 − };
300 − let categories: Vec<SideCategory> = tree
301 − .roots()
302 − .iter()
303 − .filter(|id| vis.contains(id))
304 − .filter_map(|&root| {
305 − let mut row = side(root)?;
306 − row.children = tree
307 − .children(root)
308 − .iter()
309 − .filter(|id| vis.contains(id))
310 − .filter_map(|&id| side(id))
311 − .collect();
312 − Some(row)
313 − })
314 − .collect();
369 + let categories = side_categories(&tree, &vis, &counts, is_focus, user_sort(uc));
315 370
316 371 let user = uc.map(|c| c.user.clone());
317 372 let can_write = uc.is_some_and(|c| super::may_write(&state, &c.user));
modified templates/forum/_rail.html
+2 −2
@@ -12,14 +12,14 @@
12 12 {% if r.current %}
13 13 <div id="focus-rail-{{ r.slug }}" aria-current="true" title="{{ r.name }} (in focus)"
14 14 class="flex aspect-square w-16 shrink-0 flex-col justify-between rounded-box border-2 border-accent bg-accent/10 p-1.5 shadow-sm md:w-full">
15 − {% if r.widget_bg %}<span class="size-4 rounded-full border border-base-content/20" style="background: {{ r.widget_bg }}" aria-hidden="true"></span>{% else %}<span class="font-mono text-xs opacity-60" aria-hidden="true">#</span>{% endif %}
15 + {% if r.icon %}<i class="fas {{ r.icon }} text-sm opacity-70" aria-hidden="true"></i>{% elif r.widget_bg %}<span class="size-4 rounded-full border border-base-content/20" style="background: {{ r.widget_bg }}" aria-hidden="true"></span>{% else %}<span class="font-mono text-xs opacity-60" aria-hidden="true">#</span>{% endif %}
16 16 <span class="line-clamp-2 text-[0.65rem] font-semibold leading-tight">{{ r.name }}</span>
17 17 <span class="font-mono text-[0.6rem] opacity-60">{{ r.count }}</span>
18 18 </div>
19 19 {% else %}
20 20 <a id="focus-rail-{{ r.slug }}" href="{{ r.href }}" title="Focus on {{ r.name }}" aria-label="Focus on {{ r.name }}"
21 21 class="flex aspect-square w-16 shrink-0 flex-col justify-between rounded-box border border-base-300 bg-base-200 p-1.5 transition-all hover:-translate-y-0.5 hover:border-primary/60 focus-visible:outline-2 focus-visible:outline-primary md:w-full">
22 − {% if r.widget_bg %}<span class="size-4 rounded-full border border-base-content/20" style="background: {{ r.widget_bg }}" aria-hidden="true"></span>{% else %}<span class="font-mono text-xs opacity-60" aria-hidden="true">#</span>{% endif %}
22 + {% if r.icon %}<i class="fas {{ r.icon }} text-sm opacity-70" aria-hidden="true"></i>{% elif r.widget_bg %}<span class="size-4 rounded-full border border-base-content/20" style="background: {{ r.widget_bg }}" aria-hidden="true"></span>{% else %}<span class="font-mono text-xs opacity-60" aria-hidden="true">#</span>{% endif %}
23 23 <span class="line-clamp-2 text-[0.65rem] font-medium leading-tight">{{ r.name }}</span>
24 24 <span class="font-mono text-[0.6rem] opacity-50">{{ r.count }}</span>
25 25 </a>

Parents: 1974764