//! When a project last moved, and THE order projects read in. The port of //! `packages/core/src/engine/projectActivity.ts`. //! //! ONE function, in core, because several surfaces read it and they must //! agree: the project card's "last activity" line, the project page's header, //! and the Mac window's sidebar. Several //! implementations of "newest" is several chances for a card and the page it //! opens to disagree about the same project in front of the same person. //! //! Instants are compared by PARSED value, not lexically: //! `2026-08-21T04:00+05:30` is EARLIER than `2026-08-20T23:00Z` and a string //! compare says the opposite. The value RETURNED is the original string, so a //! caller formats what the row actually holds. use std::cmp::Ordering; use std::collections::HashMap; use cotton_store::protocol::js::parse_date_ms; /// The two streams a project's activity comes from. /// /// Memory rode as a third until the memory-home change made it a link kind; it now arrives /// in `linked_updated_at`. #[derive(Debug, Clone, Default)] pub struct ProjectActivityInput<'a> { /// `updated_at` of every record linked to the project — memory included. pub linked_updated_at: &'a [Option<&'a str>], /// `last_active_at` of every session bound to the project. pub session_last_active_at: &'a [Option<&'a str>], } /// The newest instant across both streams, or `None` when nothing moved. pub fn project_last_activity<'a>(input: &ProjectActivityInput<'a>) -> Option<&'a str> { let mut best: Option<&'a str> = None; let mut best_value = i64::MIN; for stream in [input.linked_updated_at, input.session_last_active_at] { for instant in stream.iter().flatten() { // A row whose timestamp does not parse says nothing about when the // project moved. Dropping it is not a silent fallback: the // alternative is NaN poisoning every comparison after it. let Some(value) = parse_date_ms(instant) else { continue; }; if best.is_none() || value > best_value { best_value = value; best = Some(instant); } } } best } /// The shape the order below reads. /// /// Structural rather than a service type, for the reason the TypeScript gives: /// the engine stays free of the service layer, and every project shape in the /// product — the phone's rows, the Mac window's sidebar entries — satisfies it. pub trait OrderedProject { fn id(&self) -> &str; fn name(&self) -> &str; } /// THE order projects read in — last activity first, name breaking the tie so /// the order is TOTAL. /// /// Home's band and the projects list are ONE TAP apart over the same rows, and /// until this comparator existed they sorted at opposite ends: the band by /// activity, the page by `ORDER BY created_at ASC`. The same list reading /// newest-first in one place and oldest-first in the other is a contradiction /// the reader has to hold, not a preference either screen earned. Activity wins /// because it is what a working context actually is: the one you touched last. /// /// A project with no instant sorts to the BOTTOM, never the top: `overviews` /// answers for ACTIVE projects only, so the archive has nothing to read here /// and falls to the alphabet — which is the order an archive wants anyway. /// /// The TypeScript returns a comparator closure over its `overviews` record; /// this is the comparison itself, taking that record, because a Rust caller /// passes it to `sort_by` rather than storing it. pub fn project_order( a: &T, b: &T, overviews: &HashMap>, ) -> Ordering { let at = |id: &str| -> Option { overviews .get(id) .and_then(Option::as_deref) .and_then(parse_date_ms) }; // Newest first — `at(b) - at(a)` in the TypeScript, where two projects with // no instant subtract to NaN, which is falsy, and fall through to the name. match (at(b.id()), at(a.id())) { (Some(newer), Some(older)) if newer != older => newer.cmp(&older), (Some(_), None) => Ordering::Greater, (None, Some(_)) => Ordering::Less, _ => a.name().cmp(b.name()), } } #[cfg(test)] #[path = "project_activity_tests.rs"] mod tests;