neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
Manage categories in Focus: nested cards, and + -> form -> card inline adds
6b030de · gmorell · 2026-12-03 02:18
Message
{commit_body(@commit)}
Files changed
modified
src/handlers/categories.rs
+247
−24
@@ -1,6 +1,6 @@
| 1 | 1 | use axum::{ |
| 2 | − extract::{Form, Path, State}, | |
| 3 | − http::StatusCode, | |
| 2 | + extract::{Form, Path, Query, State}, | |
| 3 | + http::{HeaderMap, StatusCode}, | |
| 4 | 4 | response::{Html, IntoResponse, Redirect, Response}, |
| 5 | 5 | }; |
| 6 | 6 | use chrono::Utc; |
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
| 10 | 10 | use tracing::error; |
| 11 | 11 | use uuid::Uuid; |
| 12 | 12 | |
| 13 | −use crate::category_tree::CategoryTree; | |
| 13 | +use crate::category_tree::{color_widget_css, CategoryTree, MAX_DEPTH}; | |
| 14 | 14 | use crate::entities::categories; |
| 15 | 15 | use crate::middleware::UserContext; |
| 16 | 16 | use crate::AppState; |
@@ -72,13 +72,31 @@ struct ParentOption {
| 72 | 72 | depth: i32, |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | −/// List categories for management (staff only), as an indented tree. | |
| 75 | +#[derive(Debug, Deserialize)] | |
| 76 | +pub struct ManageQuery { | |
| 77 | + /// `classic` or `focus`; see [`super::focus::resolve_view`]. | |
| 78 | + pub view: Option<String>, | |
| 79 | +} | |
| 80 | + | |
| 81 | +/// Manage categories (staff only): an indented table in Classic, nested | |
| 82 | +/// cards in Focus ([`manage_focus`]). | |
| 76 | 83 | pub async fn manage_categories( |
| 77 | 84 | State(state): State<AppState>, |
| 85 | + Query(q): Query<ManageQuery>, | |
| 86 | + headers: HeaderMap, | |
| 78 | 87 | user: UserContext, |
| 79 | −) -> Result<Html<String>, StatusCode> { | |
| 88 | +) -> Result<Response, StatusCode> { | |
| 80 | 89 | require_staff(&user)?; |
| 90 | + let view = super::focus::view_for(&state, q.view.as_deref(), Some(&user), &headers); | |
| 91 | + let page = if view.mode == super::focus::ViewMode::Focus { | |
| 92 | + manage_focus(&state, &user).await? | |
| 93 | + } else { | |
| 94 | + manage_classic(&state, &user).await? | |
| 95 | + }; | |
| 96 | + Ok(view.respond(page)) | |
| 97 | +} | |
| 81 | 98 | |
| 99 | +async fn manage_classic(state: &AppState, user: &UserContext) -> Result<Html<String>, StatusCode> { | |
| 82 | 100 | let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load categories"))?; |
| 83 | 101 | #[derive(Serialize)] |
| 84 | 102 | struct Row { |
@@ -106,14 +124,196 @@ pub async fn manage_categories(
| 106 | 124 | }) |
| 107 | 125 | .collect(); |
| 108 | 126 | |
| 109 | − render(&state, "admin/categories.html", context! { | |
| 127 | + render(state, "admin/categories.html", context! { | |
| 110 | 128 | title => "Manage categories", |
| 111 | 129 | app_name => state.config.app_name, |
| 112 | − user => user.user, | |
| 130 | + user => user.user.clone(), | |
| 113 | 131 | categories => rows, |
| 114 | 132 | }) |
| 115 | 133 | } |
| 116 | 134 | |
| 135 | +// --- Focus: nested cards with inline add (pin_flip's board and its `+`) --- | |
| 136 | + | |
| 137 | +/// Colours offered to a new category, picked by how many siblings it has so | |
| 138 | +/// neighbours differ. The classic form keeps [`DEFAULT_COLOR`]. | |
| 139 | +const NEW_COLORS: [&str; 10] = [ | |
| 140 | + "#0088CC", "#12A89D", "#3AB54A", "#F7941D", "#E45735", | |
| 141 | + "#BF1E2E", "#92278F", "#652D90", "#25AAE2", "#8C6238", | |
| 142 | +]; | |
| 143 | + | |
| 144 | +/// A category card and everything nested in it. | |
| 145 | +#[derive(Serialize)] | |
| 146 | +struct CardNode { | |
| 147 | + id: Uuid, | |
| 148 | + name: String, | |
| 149 | + slug: String, | |
| 150 | + description: Option<String>, | |
| 151 | + widget_bg: String, | |
| 152 | + min_trust: Option<i32>, | |
| 153 | + depth: i32, | |
| 154 | + /// Live topics in this category alone, and with everything under it. | |
| 155 | + own_count: u64, | |
| 156 | + count: u64, | |
| 157 | + children: Vec<CardNode>, | |
| 158 | + /// The `+` inside this card: absent at [`MAX_DEPTH`]. | |
| 159 | + add: Option<AddSlot>, | |
| 160 | + /// Just added inline (highlighted once). | |
| 161 | + fresh: bool, | |
| 162 | +} | |
| 163 | + | |
| 164 | +/// A `+` (or, `adding`, its inline form) for a new category under `parent`. | |
| 165 | +#[derive(Serialize)] | |
| 166 | +struct AddSlot { | |
| 167 | + /// Empty for the top level (the form's `parent_id`). | |
| 168 | + parent: String, | |
| 169 | + parent_name: Option<String>, | |
| 170 | + depth: i32, | |
| 171 | + adding: bool, | |
| 172 | + color: String, | |
| 173 | + min_trust: String, | |
| 174 | + name: String, | |
| 175 | + error: Option<String>, | |
| 176 | +} | |
| 177 | + | |
| 178 | +fn add_slot(tree: &CategoryTree, parent: Option<Uuid>) -> Option<AddSlot> { | |
| 179 | + let (depth, siblings, parent_cat) = match parent { | |
| 180 | + Some(pid) => (tree.depth(pid) + 1, tree.children(pid).len(), tree.get(pid)), | |
| 181 | + None => (1, tree.roots().len(), None), | |
| 182 | + }; | |
| 183 | + if depth > MAX_DEPTH { | |
| 184 | + return None; | |
| 185 | + } | |
| 186 | + Some(AddSlot { | |
| 187 | + parent: parent.map(|p| p.to_string()).unwrap_or_default(), | |
| 188 | + parent_name: parent_cat.map(|c| c.name.clone()), | |
| 189 | + depth, | |
| 190 | + adding: false, | |
| 191 | + color: NEW_COLORS[siblings % NEW_COLORS.len()].to_string(), | |
| 192 | + // A new subcategory starts as visible as its parent. | |
| 193 | + min_trust: parent_cat | |
| 194 | + .and_then(|c| c.min_trust_level) | |
| 195 | + .map(|n| n.to_string()) | |
| 196 | + .unwrap_or_else(|| "public".into()), | |
| 197 | + name: String::new(), | |
| 198 | + error: None, | |
| 199 | + }) | |
| 200 | +} | |
| 201 | + | |
| 202 | +fn card_node( | |
| 203 | + tree: &CategoryTree, | |
| 204 | + counts: &std::collections::HashMap<Uuid, u64>, | |
| 205 | + all: &std::collections::HashSet<Uuid>, | |
| 206 | + id: Uuid, | |
| 207 | + depth: i32, | |
| 208 | + fresh: Option<Uuid>, | |
| 209 | +) -> Option<CardNode> { | |
| 210 | + let c = tree.get(id)?; | |
| 211 | + Some(CardNode { | |
| 212 | + id, | |
| 213 | + name: c.name.clone(), | |
| 214 | + slug: c.slug.clone(), | |
| 215 | + description: c.description.clone(), | |
| 216 | + widget_bg: color_widget_css(&tree.widget_colors(id)), | |
| 217 | + min_trust: c.min_trust_level, | |
| 218 | + depth, | |
| 219 | + own_count: counts.get(&id).copied().unwrap_or(0), | |
| 220 | + count: super::focus::subtree_count(tree, all, counts, id), | |
| 221 | + children: tree | |
| 222 | + .children(id) | |
| 223 | + .iter() | |
| 224 | + .filter_map(|&ch| card_node(tree, counts, all, ch, depth + 1, fresh)) | |
| 225 | + .collect(), | |
| 226 | + add: add_slot(tree, Some(id)), | |
| 227 | + fresh: fresh == Some(id), | |
| 228 | + }) | |
| 229 | +} | |
| 230 | + | |
| 231 | +async fn manage_focus(state: &AppState, user: &UserContext) -> Result<Html<String>, StatusCode> { | |
| 232 | + let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load categories"))?; | |
| 233 | + let counts = super::focus::topic_counts(state).await?; | |
| 234 | + let all: std::collections::HashSet<Uuid> = tree.flatten().into_iter().map(|(id, _)| id).collect(); | |
| 235 | + let cards: Vec<CardNode> = tree | |
| 236 | + .roots() | |
| 237 | + .iter() | |
| 238 | + .filter_map(|&id| card_node(&tree, &counts, &all, id, 1, None)) | |
| 239 | + .collect(); | |
| 240 | + render(state, "admin/categories_focus.html", context! { | |
| 241 | + title => "Manage categories", | |
| 242 | + app_name => state.config.app_name, | |
| 243 | + user => user.user.clone(), | |
| 244 | + cards => cards, | |
| 245 | + add => add_slot(&tree, None), | |
| 246 | + max_depth => MAX_DEPTH, | |
| 247 | + }) | |
| 248 | +} | |
| 249 | + | |
| 250 | +#[derive(Debug, Deserialize)] | |
| 251 | +pub struct SlotQuery { | |
| 252 | + /// The parent category; absent for the top level. | |
| 253 | + pub parent: Option<Uuid>, | |
| 254 | + /// `1` = the form, otherwise the `+` again (cancel). | |
| 255 | + pub form: Option<u8>, | |
| 256 | +} | |
| 257 | + | |
| 258 | +/// The inline add's swaps: `+` -> form (`?form=1`) and form -> `+` (cancel). | |
| 259 | +pub async fn category_add_slot( | |
| 260 | + State(state): State<AppState>, | |
| 261 | + Query(q): Query<SlotQuery>, | |
| 262 | + user: UserContext, | |
| 263 | +) -> Result<Html<String>, StatusCode> { | |
| 264 | + require_staff(&user)?; | |
| 265 | + let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load categories"))?; | |
| 266 | + if q.parent.is_some_and(|p| tree.get(p).is_none()) { | |
| 267 | + return Err(StatusCode::NOT_FOUND); | |
| 268 | + } | |
| 269 | + let mut slot = add_slot(&tree, q.parent).ok_or(StatusCode::UNPROCESSABLE_ENTITY)?; | |
| 270 | + slot.adding = q.form == Some(1); | |
| 271 | + render(&state, "admin/_category_add.html", context! { add => slot }) | |
| 272 | +} | |
| 273 | + | |
| 274 | +/// The inline add's submit: on success the new card followed by a fresh `+` | |
| 275 | +/// (so another can follow); on a problem the form again, with what was typed | |
| 276 | +/// and why. Always 200, so htmx swaps either. | |
| 277 | +pub async fn create_category_inline( | |
| 278 | + State(state): State<AppState>, | |
| 279 | + user: UserContext, | |
| 280 | + Form(form): Form<CategoryForm>, | |
| 281 | +) -> Result<Html<String>, StatusCode> { | |
| 282 | + require_staff(&user)?; | |
| 283 | + let parent = parse_parent(&form.parent_id).ok().flatten(); | |
| 284 | + let typed = (form.name.clone(), form.color.clone(), form.min_trust_level.clone()); | |
| 285 | + match insert_category(&state, form, true).await? { | |
| 286 | + Ok(created) => { | |
| 287 | + let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load categories"))?; | |
| 288 | + let depth = tree.depth(created.id); | |
| 289 | + let counts = std::collections::HashMap::new(); | |
| 290 | + let all = std::collections::HashSet::from([created.id]); | |
| 291 | + let card = card_node(&tree, &counts, &all, created.id, depth, Some(created.id)) | |
| 292 | + .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?; | |
| 293 | + render(&state, "admin/_category_added.html", context! { | |
| 294 | + card => card, | |
| 295 | + add => add_slot(&tree, created.parent_id), | |
| 296 | + max_depth => MAX_DEPTH, | |
| 297 | + }) | |
| 298 | + } | |
| 299 | + Err(msg) => { | |
| 300 | + let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load categories"))?; | |
| 301 | + let mut slot = add_slot(&tree, parent.filter(|p| tree.get(*p).is_some())) | |
| 302 | + .ok_or(StatusCode::UNPROCESSABLE_ENTITY)?; | |
| 303 | + slot.adding = true; | |
| 304 | + slot.name = typed.0; | |
| 305 | + if let Some(c) = typed.1.filter(|c| !c.trim().is_empty()) { | |
| 306 | + slot.color = c; | |
| 307 | + } | |
| 308 | + if let Some(t) = typed.2 { | |
| 309 | + slot.min_trust = t; | |
| 310 | + } | |
| 311 | + slot.error = Some(msg.to_string()); | |
| 312 | + render(&state, "admin/_category_add.html", context! { add => slot }) | |
| 313 | + } | |
| 314 | + } | |
| 315 | +} | |
| 316 | + | |
| 117 | 317 | /// New-category form (staff only). |
| 118 | 318 | pub async fn new_category_form( |
| 119 | 319 | State(state): State<AppState>, |
@@ -138,31 +338,42 @@ pub async fn edit_category_form(
| 138 | 338 | render_form(&state, &user, Some(category), None).await |
| 139 | 339 | } |
| 140 | 340 | |
| 141 | −/// Create a category (staff only). | |
| 142 | −pub async fn create_category( | |
| 143 | − State(state): State<AppState>, | |
| 144 | − user: UserContext, | |
| 145 | − Form(form): Form<CategoryForm>, | |
| 146 | −) -> Result<Response, StatusCode> { | |
| 147 | − require_staff(&user)?; | |
| 148 | − | |
| 341 | +/// Validate and insert a category. `Ok(Err(msg))` is a problem with the form | |
| 342 | +/// (shown to the user); `Err` a server failure. With no position given the | |
| 343 | +/// classic form keeps its old default (0); the inline add puts the new | |
| 344 | +/// category after its siblings. | |
| 345 | +async fn insert_category( | |
| 346 | + state: &AppState, | |
| 347 | + form: CategoryForm, | |
| 348 | + append: bool, | |
| 349 | +) -> Result<Result<categories::Model, &'static str>, StatusCode> { | |
| 149 | 350 | let name = form.name.trim().to_string(); |
| 150 | 351 | let slug = derive_slug(&form); |
| 151 | 352 | if name.is_empty() || slug.is_empty() { |
| 152 | − return form_err(&state, &user, None, "Name (and a non-empty slug) are required.").await; | |
| 353 | + return Ok(Err("Name (and a non-empty slug) are required.")); | |
| 153 | 354 | } |
| 154 | − if slug_taken(&state, &slug, None).await? { | |
| 155 | − return form_err(&state, &user, None, "That slug is already in use.").await; | |
| 355 | + if slug_taken(state, &slug, None).await? { | |
| 356 | + return Ok(Err("That slug is already in use.")); | |
| 156 | 357 | } |
| 157 | − | |
| 158 | 358 | let parent = match parse_parent(&form.parent_id) { |
| 159 | 359 | Ok(p) => p, |
| 160 | − Err(e) => return form_err(&state, &user, None, e).await, | |
| 360 | + Err(e) => return Ok(Err(e)), | |
| 161 | 361 | }; |
| 162 | 362 | let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load tree"))?; |
| 163 | 363 | let parent_id = match tree.validate_parent(parent, None) { |
| 164 | 364 | Ok(p) => p, |
| 165 | − Err(e) => return form_err(&state, &user, None, e).await, | |
| 365 | + Err(e) => return Ok(Err(e)), | |
| 366 | + }; | |
| 367 | + let position = match form.position { | |
| 368 | + Some(p) => p, | |
| 369 | + None if append => { | |
| 370 | + let siblings = match parent_id { | |
| 371 | + Some(pid) => tree.children(pid), | |
| 372 | + None => tree.roots(), | |
| 373 | + }; | |
| 374 | + siblings.iter().filter_map(|id| tree.get(*id)).map(|c| c.position + 1).max().unwrap_or(0) | |
| 375 | + } | |
| 376 | + None => 0, | |
| 166 | 377 | }; |
| 167 | 378 | |
| 168 | 379 | let now = Utc::now(); |
@@ -172,15 +383,27 @@ pub async fn create_category(
| 172 | 383 | slug: Set(slug), |
| 173 | 384 | description: Set(clean_opt(form.description)), |
| 174 | 385 | color: Set(clean_color(form.color)), |
| 175 | − position: Set(form.position.unwrap_or(0)), | |
| 386 | + position: Set(position), | |
| 176 | 387 | parent_id: Set(parent_id), |
| 177 | 388 | min_trust_level: Set(parse_min_trust(&form.min_trust_level)), |
| 178 | 389 | created_at: Set(now.into()), |
| 179 | 390 | updated_at: Set(now.into()), |
| 180 | 391 | }; |
| 181 | − active.insert(state.db.as_ref()).await.map_err(ise("insert category"))?; | |
| 392 | + let model = active.insert(state.db.as_ref()).await.map_err(ise("insert category"))?; | |
| 393 | + Ok(Ok(model)) | |
| 394 | +} | |
| 182 | 395 | |
| 183 | − Ok(Redirect::to("/admin/categories").into_response()) | |
| 396 | +/// Create a category (staff only). | |
| 397 | +pub async fn create_category( | |
| 398 | + State(state): State<AppState>, | |
| 399 | + user: UserContext, | |
| 400 | + Form(form): Form<CategoryForm>, | |
| 401 | +) -> Result<Response, StatusCode> { | |
| 402 | + require_staff(&user)?; | |
| 403 | + match insert_category(&state, form, false).await? { | |
| 404 | + Ok(_) => Ok(Redirect::to("/admin/categories").into_response()), | |
| 405 | + Err(e) => form_err(&state, &user, None, e).await, | |
| 406 | + } | |
| 184 | 407 | } |
| 185 | 408 | |
| 186 | 409 | /// Update a category (staff only). |
modified
src/handlers/mod.rs
+2
−2
@@ -26,8 +26,8 @@ pub use auth::{
| 26 | 26 | show_invites, show_login, show_profile, show_register, update_settings, verify_email, |
| 27 | 27 | }; |
| 28 | 28 | pub use categories::{ |
| 29 | − create_category, delete_category, edit_category_form, manage_categories, new_category_form, | |
| 30 | − update_category, | |
| 29 | + category_add_slot, create_category, create_category_inline, delete_category, edit_category_form, | |
| 30 | + manage_categories, new_category_form, update_category, | |
| 31 | 31 | }; |
| 32 | 32 | pub use forum::{ |
| 33 | 33 | categories_index, category_show, create_post, create_topic, delete_topic, new_topic_form, restore_topic, |
modified
src/main.rs
+2
−0
@@ -152,6 +152,8 @@ async fn main() -> Result<(), anyhow::Error> {
| 152 | 152 | get(handlers::manage_categories).post(handlers::create_category), |
| 153 | 153 | ) |
| 154 | 154 | .route("/admin/categories/new", get(handlers::new_category_form)) |
| 155 | + .route("/admin/categories/slot", get(handlers::category_add_slot)) | |
| 156 | + .route("/admin/categories/inline", post(handlers::create_category_inline)) | |
| 155 | 157 | .route( |
| 156 | 158 | "/admin/categories/{id}/edit", |
| 157 | 159 | get(handlers::edit_category_form), |
added
templates/admin/_category_add.html
+43
−0
@@ -0,0 +1,43 @@
| 1 | +{# The inline add (src/handlers/categories.rs add_slot): a `+` tile that | |
| 2 | + swaps to a form, which swaps to the new card plus a fresh `+` (or back to | |
| 3 | + the `+` on Cancel / Escape). `add` is an AddSlot. Every request targets the | |
| 4 | + slot itself, never the boosted #content. #} | |
| 5 | +{% set slot_id = "cat-add-" ~ (add.parent or "root") %} | |
| 6 | +{% set slot_url = "/admin/categories/slot?parent=" ~ add.parent if add.parent else "/admin/categories/slot?" %} | |
| 7 | +{% if not add.adding %} | |
| 8 | +<button type="button" id="{{ slot_id }}" | |
| 9 | + hx-get="{{ slot_url }}&form=1" hx-target="this" hx-select="unset" hx-swap="outerHTML" | |
| 10 | + title="{% if add.parent_name %}Add a subcategory to {{ add.parent_name }}{% else %}Add a category{% endif %}" | |
| 11 | + aria-label="{% if add.parent_name %}Add a subcategory to {{ add.parent_name }}{% else %}Add a category{% endif %}" | |
| 12 | + class="flex items-center justify-center gap-2 rounded-box border-2 border-dashed border-base-300 text-base-content/50 transition-all hover:-translate-y-0.5 hover:border-primary/60 hover:text-primary focus-visible:outline-2 focus-visible:outline-primary | |
| 13 | + {% if add.depth == 1 %}min-h-40 text-3xl{% elif add.depth == 2 %}min-h-24 text-2xl{% else %}min-h-14 text-xl{% endif %}"> | |
| 14 | + <i class="fas fa-plus" aria-hidden="true"></i> | |
| 15 | + {% if add.depth == 1 %}<span class="text-sm font-medium">Add category</span>{% endif %} | |
| 16 | +</button> | |
| 17 | +{% else %} | |
| 18 | +<form id="{{ slot_id }}" hx-post="/admin/categories/inline" hx-target="this" hx-select="unset" hx-swap="outerHTML" | |
| 19 | + hx-on:keydown="if (event.key === 'Escape') { event.preventDefault(); this.querySelector('[data-cancel]').click(); }" | |
| 20 | + class="flex flex-col gap-2 rounded-box border-2 border-primary/60 bg-base-100 p-3 shadow-md"> | |
| 21 | + <input type="hidden" name="parent_id" value="{{ add.parent }}"> | |
| 22 | + <p class="text-xs font-semibold opacity-70"> | |
| 23 | + {% if add.parent_name %}New subcategory in {{ add.parent_name }}{% else %}New category{% endif %} | |
| 24 | + </p> | |
| 25 | + <input type="text" name="name" value="{{ add.name }}" required autofocus maxlength="100" | |
| 26 | + placeholder="Name" aria-label="Name" class="input input-sm input-bordered w-full"> | |
| 27 | + <div class="flex items-center gap-2"> | |
| 28 | + <input type="color" name="color" value="{{ add.color }}" aria-label="Colour" title="Colour" | |
| 29 | + class="h-8 w-10 shrink-0 cursor-pointer rounded border border-base-300 bg-transparent"> | |
| 30 | + <select name="min_trust_level" aria-label="Who can see it" class="select select-sm select-bordered min-w-0 flex-1"> | |
| 31 | + {% for v, label in [("public", "Public"), ("0", "Logged-in"), ("1", "TL1+"), ("2", "TL2+"), ("3", "TL3+"), ("4", "TL4+")] %} | |
| 32 | + <option value="{{ v }}" {% if add.min_trust == v %}selected{% endif %}>{{ label }}</option> | |
| 33 | + {% endfor %} | |
| 34 | + </select> | |
| 35 | + </div> | |
| 36 | + {% if add.error %}<p class="text-xs text-error" role="alert">{{ add.error }}</p>{% endif %} | |
| 37 | + <div class="flex justify-end gap-1"> | |
| 38 | + <button type="button" data-cancel class="btn btn-ghost btn-xs" | |
| 39 | + hx-get="{{ slot_url }}" hx-target="closest form" hx-select="unset" hx-swap="outerHTML">Cancel</button> | |
| 40 | + <button type="submit" class="btn btn-primary btn-xs"><i class="fas fa-plus mr-1"></i>Add</button> | |
| 41 | + </div> | |
| 42 | +</form> | |
| 43 | +{% endif %} |
added
templates/admin/_category_added.html
+4
−0
@@ -0,0 +1,4 @@
| 1 | +{# What replaces the inline form after a successful add: the new card, then a | |
| 2 | + fresh `+` for its parent so another can follow (none at the depth limit). #} | |
| 3 | +{% with node = card %}{% include "admin/_category_card.html" %}{% endwith %} | |
| 4 | +{% if add %}{% include "admin/_category_add.html" %}{% endif %} |
added
templates/admin/_category_card.html
+46
−0
@@ -0,0 +1,46 @@
| 1 | +{# A category card in Focus management (src/handlers/categories.rs | |
| 2 | + card_node): its header, then its subcategories as nested cards and the `+` | |
| 3 | + that adds one inline, as pin_flip's board nests groups. `node` is the card; | |
| 4 | + `max_depth` the nesting limit. Recursive. #} | |
| 5 | +<article id="cat-card-{{ node.id }}" data-depth="{{ node.depth }}" aria-label="{{ node.name }}" | |
| 6 | + class="flex min-w-0 flex-col gap-3 rounded-box border | |
| 7 | + {% if node.depth == 1 %}border-primary/40 bg-base-200 p-4 shadow-sm | |
| 8 | + {% elif node.depth == 2 %}border-base-300 bg-base-100 p-3 | |
| 9 | + {% else %}border-base-300 bg-base-200/60 p-2.5{% endif %} | |
| 10 | + {% if node.fresh %}ring-2 ring-accent ring-offset-2 ring-offset-base-100{% endif %}"> | |
| 11 | + <header class="flex items-start gap-2"> | |
| 12 | + <span class="{% if node.depth == 1 %}size-7{% elif node.depth == 2 %}size-5{% else %}size-4{% endif %} mt-0.5 shrink-0 rounded-full border border-base-content/20" | |
| 13 | + style="background: {{ node.widget_bg }}" aria-hidden="true"></span> | |
| 14 | + <div class="min-w-0 flex-1"> | |
| 15 | + <a href="/c/{{ node.slug }}?view=focus" | |
| 16 | + class="block truncate font-semibold hover:text-primary {% if node.depth == 1 %}text-lg{% elif node.depth == 3 %}text-sm{% endif %}" | |
| 17 | + title="Open {{ node.name }}">{{ node.name }}</a> | |
| 18 | + <div class="flex flex-wrap items-center gap-1 text-xs"> | |
| 19 | + <code class="opacity-60">{{ node.slug }}</code> | |
| 20 | + {% if node.min_trust is none %}<span class="badge badge-ghost badge-xs">Public</span> | |
| 21 | + {% elif node.min_trust == 0 %}<span class="badge badge-warning badge-xs">Logged-in</span> | |
| 22 | + {% else %}<span class="badge badge-warning badge-xs">TL{{ node.min_trust }}+</span>{% endif %} | |
| 23 | + <span class="font-mono opacity-60" title="{{ node.own_count }} here, {{ node.count }} including subcategories">{{ node.count }} topics</span> | |
| 24 | + </div> | |
| 25 | + {% if node.description and node.depth < 3 %}<p class="mt-1 line-clamp-2 text-xs opacity-70">{{ node.description }}</p>{% endif %} | |
| 26 | + </div> | |
| 27 | + <div class="flex shrink-0 items-center"> | |
| 28 | + <a href="/admin/categories/{{ node.id }}/edit" class="btn btn-ghost btn-xs btn-square" | |
| 29 | + title="Edit {{ node.name }}" aria-label="Edit {{ node.name }}"><i class="fas fa-pen"></i></a> | |
| 30 | + <form method="post" action="/admin/categories/{{ node.id }}/delete" class="inline" hx-boost="false" | |
| 31 | + onsubmit="return confirm('Delete "{{ node.name }}"? Subcategories move up a level; this category\'s own topics and posts are deleted.');"> | |
| 32 | + <button type="submit" class="btn btn-ghost btn-xs btn-square text-error" | |
| 33 | + title="Delete {{ node.name }}" aria-label="Delete {{ node.name }}"><i class="fas fa-trash"></i></button> | |
| 34 | + </form> | |
| 35 | + </div> | |
| 36 | + </header> | |
| 37 | + | |
| 38 | + {% if node.children or node.add %} | |
| 39 | + <div class="grid gap-2 {% if node.depth == 1 %}grid-cols-[repeat(auto-fill,minmax(15rem,1fr))] gap-3{% else %}grid-cols-[repeat(auto-fill,minmax(10rem,1fr))]{% endif %}"> | |
| 40 | + {% for child in node.children %} | |
| 41 | + {% with node = child %}{% include "admin/_category_card.html" %}{% endwith %} | |
| 42 | + {% endfor %} | |
| 43 | + {% if node.add %}{% with add = node.add %}{% include "admin/_category_add.html" %}{% endwith %}{% endif %} | |
| 44 | + </div> | |
| 45 | + {% endif %} | |
| 46 | +</article> |
modified
templates/admin/categories.html
+7
−3
@@ -4,9 +4,13 @@
| 4 | 4 | <div class="space-y-4 text-left"> |
| 5 | 5 | <div class="flex items-center justify-between"> |
| 6 | 6 | <h1 class="text-3xl font-bold">Manage categories</h1> |
| 7 | − <a href="/admin/categories/new" class="btn btn-primary btn-sm"> | |
| 8 | − <i class="fas fa-plus mr-1"></i> New category | |
| 9 | − </a> | |
| 7 | + <div class="flex flex-wrap items-center gap-2"> | |
| 8 | + {% set toggle_base = "/admin/categories" %}{% set view_mode = "classic" %} | |
| 9 | + {% include "forum/_view_toggle.html" %} | |
| 10 | + <a href="/admin/categories/new" class="btn btn-primary btn-sm"> | |
| 11 | + <i class="fas fa-plus mr-1"></i> New category | |
| 12 | + </a> | |
| 13 | + </div> | |
| 10 | 14 | </div> |
| 11 | 15 | |
| 12 | 16 | {% if categories|length == 0 %} |
added
templates/admin/categories_focus.html
+31
−0
@@ -0,0 +1,31 @@
| 1 | +{% extends "root.html" %} | |
| 2 | +{# Manage categories, Focus (src/handlers/categories.rs manage_focus): the | |
| 3 | + top level as cards with their subcategories nested inside, down to the | |
| 4 | + depth limit, each level ending in a `+` that adds one inline. Full width, | |
| 5 | + as the other Focus pages. #} | |
| 6 | +{% block width %}w-full max-w-none{% endblock %} | |
| 7 | +{% block main_class %}px-3 py-3 sm:px-4 lg:px-5{% endblock %} | |
| 8 | +{% block content %} | |
| 9 | +<div class="space-y-4 text-left"> | |
| 10 | + <div class="flex flex-wrap items-center justify-between gap-3"> | |
| 11 | + <div> | |
| 12 | + <h1 class="text-3xl font-bold">Manage categories</h1> | |
| 13 | + <p class="text-sm opacity-60">Up to {{ max_depth }} levels. <kbd class="kbd kbd-xs">+</kbd> adds one in place; Esc cancels.</p> | |
| 14 | + </div> | |
| 15 | + <div class="flex flex-wrap items-center gap-2"> | |
| 16 | + {% set toggle_base = "/admin/categories" %}{% set view_mode = "focus" %} | |
| 17 | + {% include "forum/_view_toggle.html" %} | |
| 18 | + <a href="/admin/categories/new" class="btn btn-ghost btn-sm" title="The full form: slug, description, position, parent"> | |
| 19 | + <i class="fas fa-file-pen mr-1"></i> Full form | |
| 20 | + </a> | |
| 21 | + </div> | |
| 22 | + </div> | |
| 23 | + | |
| 24 | + <div id="cat-board" class="grid grid-cols-[repeat(auto-fill,minmax(26rem,1fr))] items-start gap-4"> | |
| 25 | + {% for card in cards %} | |
| 26 | + {% with node = card %}{% include "admin/_category_card.html" %}{% endwith %} | |
| 27 | + {% endfor %} | |
| 28 | + {% if add %}{% include "admin/_category_add.html" %}{% endif %} | |
| 29 | + </div> | |
| 30 | +</div> | |
| 31 | +{% endblock %} |
Parents: 644543b