From cb9577d586d9cb324b042a0c05d97d231f9c2e75 Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Tue, 13 Jan 2026 09:10:20 -1000 Subject: Implement Trello smart sorting by newest card activity - Update GetBoards SQL query with LEFT JOIN and GROUP BY - Sort by: 1) Has cards, 2) Newest card (MAX ID), 3) Board name - Update GetBoardsWithCards to match SQL sorting behavior - Leverage Trello ID chronological sortability (newer > older) - Active boards with recent activity appear first - All tests passing Co-Authored-By: Claude Sonnet 4.5 --- PHASE_2_SURGICAL_PLAN.md | 2 +- SESSION_STATE.md | 12 +++++++++--- internal/api/trello.go | 28 +++++++++++++++++++++++++++- internal/store/sqlite.go | 10 +++++++--- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/PHASE_2_SURGICAL_PLAN.md b/PHASE_2_SURGICAL_PLAN.md index e300639..bd761e5 100644 --- a/PHASE_2_SURGICAL_PLAN.md +++ b/PHASE_2_SURGICAL_PLAN.md @@ -43,7 +43,7 @@ Refactor the frontend and router to support 4 distinct tabs. ``` ## 3. Trello: Smart Sorting & Logic -**Status:** [ ] Pending +**Status:** [x] Complete ```text Enhance Trello sorting in `internal/api/trello.go` and `internal/store/sqlite.go`. diff --git a/SESSION_STATE.md b/SESSION_STATE.md index dd8e30f..4c03fb2 100644 --- a/SESSION_STATE.md +++ b/SESSION_STATE.md @@ -51,6 +51,13 @@ Frontend modernization with tabs, HTMX, and Tailwind build pipeline complete. - web/templates/partials/planning-tab.html: New tab for Trello boards - web/templates/partials/meals-tab.html: New tab for PlanToEat meals - Clean separation of concerns: Tasks (due items), Planning (all boards), Notes (knowledge), Meals (calendar) +- **Trello Smart Sorting:** Activity-based board ordering by newest card + - internal/store/sqlite.go:426-438: Updated GetBoards SQL query with LEFT JOIN and GROUP BY + - Sort order: 1) Has cards (COUNT > 0), 2) Newest card (MAX(c.id) DESC), 3) Board name (ASC) + - internal/api/trello.go:220-254: Updated GetBoardsWithCards sorting logic + - Finds maximum card ID in each board (Trello IDs are chronologically sortable) + - Active boards with recent activity appear first, inactive boards at bottom + - All tests passing (go test ./...) - **Build Pipeline:** npm + PostCSS + Tailwind configuration (replaced CDN) - package.json, tailwind.config.js, postcss.config.js, Makefile - Custom design system with brand colors (Trello, Todoist, Obsidian, PlanToEat) @@ -94,9 +101,8 @@ Frontend modernization with tabs, HTMX, and Tailwind build pipeline complete. - **Decision:** Unified Atom Model - Abstract all data sources (Trello, Todoist, Obsidian, PlanToEat) into a single `models.Atom` type for consistent handling, sorting, and rendering across the UI. ## 📋 Next Steps -1. **Phase 2 Step 3:** Trello smart sorting (activity-based, modification date). -2. **Phase 2 Step 4:** Todoist "due first" sorting. -3. **Phase 2 Remaining:** Obsidian search & categorization, visual overhaul (glassmorphism), write operations, PWA. +1. **Phase 2 Step 4:** Todoist "due first" sorting. +2. **Phase 2 Remaining:** Obsidian search & categorization, visual overhaul (glassmorphism), write operations, PWA. ## ⚠️ Known Blockers / Debt - None currently. diff --git a/internal/api/trello.go b/internal/api/trello.go index b9391d6..c78ebc8 100644 --- a/internal/api/trello.go +++ b/internal/api/trello.go @@ -217,13 +217,39 @@ func (c *TrelloClient) GetBoardsWithCards(ctx context.Context) ([]models.Board, wg.Wait() - // Sort boards: Non-empty boards first, then alphabetical by name + // Sort boards: Non-empty boards first, newest card activity, then alphabetical by name + // Trello card IDs are chronologically sortable (newer IDs > older IDs) sort.Slice(boards, func(i, j int) bool { hasCardsI := len(boards[i].Cards) > 0 hasCardsJ := len(boards[j].Cards) > 0 + + // 1. Prioritize boards with cards if hasCardsI != hasCardsJ { return hasCardsI // true (non-empty) comes before false } + + // 2. If both have cards, compare by newest card (max ID) + if hasCardsI && hasCardsJ { + maxIDI := "" + for _, card := range boards[i].Cards { + if maxIDI == "" || card.ID > maxIDI { + maxIDI = card.ID + } + } + + maxIDJ := "" + for _, card := range boards[j].Cards { + if maxIDJ == "" || card.ID > maxIDJ { + maxIDJ = card.ID + } + } + + if maxIDI != maxIDJ { + return maxIDI > maxIDJ // Newer (larger) ID comes first + } + } + + // 3. Fallback to alphabetical by name return boards[i].Name < boards[j].Name }) diff --git a/internal/store/sqlite.go b/internal/store/sqlite.go index 5063336..fa8f2b1 100644 --- a/internal/store/sqlite.go +++ b/internal/store/sqlite.go @@ -424,13 +424,17 @@ func (s *Store) SaveBoards(boards []models.Board) error { // GetBoards retrieves all boards with their cards from the database func (s *Store) GetBoards() ([]models.Board, error) { - // Fetch boards, sorted by: non-empty boards first, then alphabetical + // Fetch boards, sorted by: non-empty boards first, newest card activity, then alphabetical + // Trello card IDs are chronologically sortable (newer IDs > older IDs) boardRows, err := s.db.Query(` SELECT b.id, b.name FROM boards b + LEFT JOIN cards c ON c.board_id = b.id + GROUP BY b.id, b.name ORDER BY - CASE WHEN (SELECT COUNT(*) FROM cards c WHERE c.board_id = b.id) > 0 THEN 0 ELSE 1 END, - b.name + CASE WHEN COUNT(c.id) > 0 THEN 0 ELSE 1 END, + MAX(c.id) DESC, + b.name ASC `) if err != nil { return nil, err -- cgit v1.2.3