diff --git a/src/modes/server/pages.rs b/src/modes/server/pages.rs index ade85af..84ab692 100644 --- a/src/modes/server/pages.rs +++ b/src/modes/server/pages.rs @@ -101,21 +101,36 @@ fn build_item_list(conn: &Connection, params: &ListQueryParams, columns: &[Colum html.push_str("

Items

"); html.push_str("

API Documentation

"); - // Add recent tags section + // Add recent tags section using the items we already have html.push_str("

Recent Tags

"); - let recent_tags_query = " - SELECT DISTINCT t.name - FROM tags t - JOIN item_tags it ON t.id = it.tag_id - ORDER BY it.id DESC - LIMIT 20 - "; - let mut stmt = conn.prepare(recent_tags_query)?; - let tag_iter = stmt.query_map([], |row| { - Ok(row.get::<_, String>(0)?) - })?; - let recent_tags: Vec = tag_iter.collect::, _>>()?; + // Collect all tags from the items, keeping track of their timestamps + let mut all_tags_with_time: Vec<(String, chrono::DateTime)> = Vec::new(); + for item in &sorted_items { + if let Some(item_id) = item.id { + if let Some(tags) = tags_map.get(&item_id) { + for tag in tags { + all_tags_with_time.push((tag.name.clone(), item.ts)); + } + } + } + } + + // Sort by timestamp descending and deduplicate + all_tags_with_time.sort_by(|a, b| b.1.cmp(&a.1)); + + // Get unique tags in order of most recent appearance + let mut seen = std::collections::HashSet::new(); + let mut recent_tags = Vec::new(); + for (tag, _) in all_tags_with_time { + if !seen.contains(&tag) { + seen.insert(tag.clone()); + recent_tags.push(tag); + if recent_tags.len() >= 20 { + break; + } + } + } if recent_tags.is_empty() { html.push_str("

No tags found

");