Clicking a contribution calendar day lists that day's pushes, issues, pull requests and comments
Browse files at this commit3d6ee62b3d97ea481579ce72c9e946427ce67c7dParent: 6201c1a- feat
- clicking a contribution calendar day lists that day's pushes, issues, pull requests and comments
7 changed files +465 -30
- crates/forge-core/src/store/contributions.rs +141 -2
- crates/forge-core/src/time.rs +8 -0
- crates/forge-core/src/tz.rs +15 -0
- crates/forge-web/src/i18n/misc.rs +45 -0
- crates/forge-web/src/pages/dashboard.rs +68 -6
- crates/forge-web/src/views/heatmap.rs +139 -22
- style/_heatmap.scss +49 -0
crates/forge-core/src/store/contributions.rsmodified+141 -2
| @@ -3,13 +3,64 @@ | |||
| 3 | use std::collections::BTreeMap; | 3 | use std::collections::BTreeMap; |
| 4 | 4 | ||
| 5 | use jiff::Timestamp; | 5 | use jiff::Timestamp; |
| 6 | -use turso::Value; | 6 | +use turso::{Row, Value}; |
| 7 | 7 | ||
| 8 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; | 8 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; |
| 9 | use super::{Store, col_int, col_text, fetch_all}; | 9 | use super::{Store, col_int, col_text, fetch_all}; |
| 10 | -use crate::error::Result; | 10 | +use crate::error::{Error, Result}; |
| 11 | use crate::tz::local_date; | 11 | use crate::tz::local_date; |
| 12 | 12 | ||
| 13 | +/// Most contributions `contributions_between` returns for one day. | ||
| 14 | +const DAY_LIMIT: u32 = 200; | ||
| 15 | + | ||
| 16 | +/// What a contribution was: a branch push, or an issue, pull request or comment the user wrote. | ||
| 17 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 18 | +pub enum ContributionKind { | ||
| 19 | + Push, | ||
| 20 | + Issue, | ||
| 21 | + Pull, | ||
| 22 | + Comment, | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +/// One contribution, with the repo it landed in. | ||
| 26 | +#[derive(Clone, Debug, PartialEq, Eq)] | ||
| 27 | +pub struct ContributionItem { | ||
| 28 | + pub kind: ContributionKind, | ||
| 29 | + pub repo_owner: String, | ||
| 30 | + pub repo_name: String, | ||
| 31 | + /// When it happened, as an RFC 3339 UTC stamp. | ||
| 32 | + pub created_at: String, | ||
| 33 | + /// The pushed ref such as `refs/heads/main`; empty for everything else. | ||
| 34 | + pub ref_name: String, | ||
| 35 | + /// The issue or pull request number (a comment's is its issue's); 0 for a push. | ||
| 36 | + pub number: i64, | ||
| 37 | + /// The issue or pull request title; empty for a push. | ||
| 38 | + pub title: String, | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +fn map_item(row: &Row) -> Result<ContributionItem> { | ||
| 42 | + let kind = match col_text(row, 0)?.as_str() { | ||
| 43 | + "push" => ContributionKind::Push, | ||
| 44 | + "issue" => ContributionKind::Issue, | ||
| 45 | + "pull" => ContributionKind::Pull, | ||
| 46 | + "comment" => ContributionKind::Comment, | ||
| 47 | + other => { | ||
| 48 | + return Err(Error::Database(format!( | ||
| 49 | + "unknown contribution kind {other:?}" | ||
| 50 | + ))); | ||
| 51 | + } | ||
| 52 | + }; | ||
| 53 | + Ok(ContributionItem { | ||
| 54 | + kind, | ||
| 55 | + repo_owner: col_text(row, 1)?, | ||
| 56 | + repo_name: col_text(row, 2)?, | ||
| 57 | + created_at: col_text(row, 3)?, | ||
| 58 | + ref_name: col_text(row, 4)?, | ||
| 59 | + number: col_int(row, 5)?, | ||
| 60 | + title: col_text(row, 6)?, | ||
| 61 | + }) | ||
| 62 | +} | ||
| 63 | + | ||
| 13 | impl Store { | 64 | impl Store { |
| 14 | /// Contributions per day in `tz` by `user_id` at or after `since` (an RFC 3339 stamp), as | 65 | /// Contributions per day in `tz` by `user_id` at or after `since` (an RFC 3339 stamp), as |
| 15 | /// `(YYYY-MM-DD, count)` sorted by day ascending; days with no activity are left out. | 66 | /// `(YYYY-MM-DD, count)` sorted by day ascending; days with no activity are left out. |
| @@ -59,6 +110,44 @@ impl Store { | |||
| 59 | } | 110 | } |
| 60 | Ok(days.into_iter().collect()) | 111 | Ok(days.into_iter().collect()) |
| 61 | } | 112 | } |
| 113 | + | ||
| 114 | + /// The contributions by `user_id` with a stamp in `[from, to)`, both RFC 3339 stamps, newest | ||
| 115 | + /// first and at most `DAY_LIMIT` of them. The same kinds as `contribution_counts` count. | ||
| 116 | + pub async fn contributions_between( | ||
| 117 | + &self, | ||
| 118 | + user_id: &str, | ||
| 119 | + from: &str, | ||
| 120 | + to: &str, | ||
| 121 | + ) -> Result<Vec<ContributionItem>> { | ||
| 122 | + // Parameters: ?1 user, ?2 from, ?3 to, ?4 and ?5 zero oids. | ||
| 123 | + let sql = "SELECT 'push' AS created_kind, o.username, r.name, p.created_at, p.ref_name, \ | ||
| 124 | + 0, '' FROM push_events p \ | ||
| 125 | + JOIN repos r ON r.id = p.repo_id JOIN users o ON o.id = r.owner_id \ | ||
| 126 | + WHERE p.actor_id = ?1 AND p.created_at >= ?2 AND p.created_at < ?3 \ | ||
| 127 | + AND p.new_oid NOT IN (?4, ?5) \ | ||
| 128 | + UNION ALL \ | ||
| 129 | + SELECT i.kind, o.username, r.name, i.created_at, '', i.number, i.title FROM issues i \ | ||
| 130 | + JOIN repos r ON r.id = i.repo_id JOIN users o ON o.id = r.owner_id \ | ||
| 131 | + WHERE i.author_id = ?1 AND i.created_at >= ?2 AND i.created_at < ?3 \ | ||
| 132 | + AND i.kind IN ('issue', 'pull') \ | ||
| 133 | + UNION ALL \ | ||
| 134 | + SELECT 'comment', o.username, r.name, c.created_at, '', i.number, i.title \ | ||
| 135 | + FROM comments c JOIN issues i ON i.id = c.issue_id \ | ||
| 136 | + JOIN repos r ON r.id = i.repo_id JOIN users o ON o.id = r.owner_id \ | ||
| 137 | + WHERE c.author_id = ?1 AND c.created_at >= ?2 AND c.created_at < ?3 \ | ||
| 138 | + ORDER BY created_at DESC LIMIT ?6"; | ||
| 139 | + let params = vec![ | ||
| 140 | + Value::Text(user_id.to_string()), | ||
| 141 | + Value::Text(from.to_string()), | ||
| 142 | + Value::Text(to.to_string()), | ||
| 143 | + Value::Text(ZERO_OID_SHA1.to_string()), | ||
| 144 | + Value::Text(ZERO_OID_SHA256.to_string()), | ||
| 145 | + Value::Integer(i64::from(DAY_LIMIT)), | ||
| 146 | + ]; | ||
| 147 | + | ||
| 148 | + let conn = self.read_conn().await; | ||
| 149 | + fetch_all(&conn, sql, params, map_item).await | ||
| 150 | + } | ||
| 62 | } | 151 | } |
| 63 | 152 | ||
| 64 | #[cfg(test)] | 153 | #[cfg(test)] |
| @@ -81,6 +170,56 @@ mod tests { | |||
| 81 | .unwrap(); | 170 | .unwrap(); |
| 82 | } | 171 | } |
| 83 | 172 | ||
| 173 | + #[tokio::test] | ||
| 174 | + async fn lists_one_day_newest_first() { | ||
| 175 | + let dir = tempfile::tempdir().unwrap(); | ||
| 176 | + let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | ||
| 177 | + let tahinli = store | ||
| 178 | + .create_user("tahinli", None, "x", false) | ||
| 179 | + .await | ||
| 180 | + .unwrap(); | ||
| 181 | + let demo = store | ||
| 182 | + .create_repo(&tahinli.id, "demo", "", Visibility::Public, "main") | ||
| 183 | + .await | ||
| 184 | + .unwrap(); | ||
| 185 | + store | ||
| 186 | + .record_push_event_at( | ||
| 187 | + &demo.id, | ||
| 188 | + &tahinli.id, | ||
| 189 | + "refs/heads/main", | ||
| 190 | + ZERO, | ||
| 191 | + OID, | ||
| 192 | + "2026-03-01T10:00:00.000000000Z", | ||
| 193 | + ) | ||
| 194 | + .await | ||
| 195 | + .unwrap(); | ||
| 196 | + let issue = store | ||
| 197 | + .create_issue(&demo.id, &tahinli.id, "first", "") | ||
| 198 | + .await | ||
| 199 | + .unwrap(); | ||
| 200 | + set_created_at( | ||
| 201 | + &store, | ||
| 202 | + "issues", | ||
| 203 | + &issue.id, | ||
| 204 | + "2026-03-01T12:00:00.000000000Z", | ||
| 205 | + ) | ||
| 206 | + .await; | ||
| 207 | + | ||
| 208 | + let items = store | ||
| 209 | + .contributions_between( | ||
| 210 | + &tahinli.id, | ||
| 211 | + "2026-03-01T00:00:00.000000000Z", | ||
| 212 | + "2026-03-02T00:00:00.000000000Z", | ||
| 213 | + ) | ||
| 214 | + .await | ||
| 215 | + .unwrap(); | ||
| 216 | + let kinds: Vec<_> = items.iter().map(|item| item.kind).collect(); | ||
| 217 | + assert_eq!(kinds, vec![ContributionKind::Issue, ContributionKind::Push]); | ||
| 218 | + assert_eq!(items[0].repo_owner, "tahinli"); | ||
| 219 | + assert_eq!((items[0].number, items[0].title.as_str()), (1, "first")); | ||
| 220 | + assert_eq!(items[1].ref_name, "refs/heads/main"); | ||
| 221 | + } | ||
| 222 | + | ||
| 84 | #[tokio::test] | 223 | #[tokio::test] |
| 85 | async fn counts_pushes_issues_pulls_and_comments_per_day() { | 224 | async fn counts_pushes_issues_pulls_and_comments_per_day() { |
| 86 | let dir = tempfile::tempdir().unwrap(); | 225 | let dir = tempfile::tempdir().unwrap(); |
crates/forge-core/src/time.rsmodified+8 -0
| @@ -40,6 +40,14 @@ pub fn stamp_ago(ago: Duration) -> String { | |||
| 40 | format_stamp(OffsetDateTime::now_utc().saturating_sub(delta)) | 40 | format_stamp(OffsetDateTime::now_utc().saturating_sub(delta)) |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | +/// The stamp for Unix seconds `unix`, in the same format as [`now_stamp`]. `None` when the time is | ||
| 44 | +/// out of the range the format covers. | ||
| 45 | +pub fn stamp_from_unix(unix: i64) -> Option<String> { | ||
| 46 | + OffsetDateTime::from_unix_timestamp(unix) | ||
| 47 | + .ok() | ||
| 48 | + .map(format_stamp) | ||
| 49 | +} | ||
| 50 | + | ||
| 43 | /// Unix seconds for a stamp written by [`now_stamp`]. `None` when the text is not a stamp. | 51 | /// Unix seconds for a stamp written by [`now_stamp`]. `None` when the text is not a stamp. |
| 44 | pub fn stamp_to_unix(stamp: &str) -> Option<i64> { | 52 | pub fn stamp_to_unix(stamp: &str) -> Option<i64> { |
| 45 | OffsetDateTime::parse(stamp, &time::format_description::well_known::Rfc3339) | 53 | OffsetDateTime::parse(stamp, &time::format_description::well_known::Rfc3339) |
crates/forge-core/src/tz.rsmodified+15 -0
| @@ -19,6 +19,21 @@ pub fn local_date(ts: Timestamp, tz: &str) -> civil::Date { | |||
| 19 | local_datetime(ts, tz).date() | 19 | local_datetime(ts, tz).date() |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | +/// Unix seconds for the start of the civil day `date` in `tz`, and for the start of the day after | ||
| 23 | +/// it. `None` at the edge of the range jiff can represent. Unknown zones fall back to UTC. | ||
| 24 | +pub fn day_span(date: civil::Date, tz: &str) -> Option<(i64, i64)> { | ||
| 25 | + let zone = zone_or_utc(tz); | ||
| 26 | + let start = date.at(0, 0, 0, 0).to_zoned(zone.clone()).ok()?.timestamp(); | ||
| 27 | + let next = date | ||
| 28 | + .tomorrow() | ||
| 29 | + .ok()? | ||
| 30 | + .at(0, 0, 0, 0) | ||
| 31 | + .to_zoned(zone) | ||
| 32 | + .ok()? | ||
| 33 | + .timestamp(); | ||
| 34 | + Some((start.as_second(), next.as_second())) | ||
| 35 | +} | ||
| 36 | + | ||
| 22 | /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC". | 37 | /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC". |
| 23 | pub fn validate_timezone(name: &str) -> Result<()> { | 38 | pub fn validate_timezone(name: &str) -> Result<()> { |
| 24 | TimeZone::get(name) | 39 | TimeZone::get(name) |
crates/forge-web/src/i18n/misc.rsmodified+45 -0
| @@ -6,6 +6,8 @@ | |||
| 6 | 6 | ||
| 7 | use std::fmt::Display; | 7 | use std::fmt::Display; |
| 8 | 8 | ||
| 9 | +use forge_core::store::contributions::ContributionKind; | ||
| 10 | + | ||
| 9 | use super::Lang; | 11 | use super::Lang; |
| 10 | 12 | ||
| 11 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| @@ -144,6 +146,7 @@ pub enum K { | |||
| 144 | MonLabel, | 146 | MonLabel, |
| 145 | WedLabel, | 147 | WedLabel, |
| 146 | FriLabel, | 148 | FriLabel, |
| 149 | + NoContributionsDay, | ||
| 147 | // Dashboard greeting and "Needs your attention" column. | 150 | // Dashboard greeting and "Needs your attention" column. |
| 148 | GoodMorning, | 151 | GoodMorning, |
| 149 | GoodAfternoon, | 152 | GoodAfternoon, |
| @@ -285,6 +288,7 @@ const ALL: &[K] = &[ | |||
| 285 | K::MonLabel, | 288 | K::MonLabel, |
| 286 | K::WedLabel, | 289 | K::WedLabel, |
| 287 | K::FriLabel, | 290 | K::FriLabel, |
| 291 | + K::NoContributionsDay, | ||
| 288 | K::GoodMorning, | 292 | K::GoodMorning, |
| 289 | K::GoodAfternoon, | 293 | K::GoodAfternoon, |
| 290 | K::GoodEvening, | 294 | K::GoodEvening, |
| @@ -572,6 +576,8 @@ pub fn t(lang: Lang, key: K) -> &'static str { | |||
| 572 | (WedLabel, Tr) => "Çar", | 576 | (WedLabel, Tr) => "Çar", |
| 573 | (FriLabel, En) => "Fri", | 577 | (FriLabel, En) => "Fri", |
| 574 | (FriLabel, Tr) => "Cum", | 578 | (FriLabel, Tr) => "Cum", |
| 579 | + (NoContributionsDay, En) => "No contributions on this day.", | ||
| 580 | + (NoContributionsDay, Tr) => "Bu günde katkı yok.", | ||
| 575 | (GoodMorning, En) => "Good morning", | 581 | (GoodMorning, En) => "Good morning", |
| 576 | (GoodMorning, Tr) => "Günaydın", | 582 | (GoodMorning, Tr) => "Günaydın", |
| 577 | (GoodAfternoon, En) => "Good afternoon", | 583 | (GoodAfternoon, En) => "Good afternoon", |
| @@ -700,6 +706,45 @@ pub fn contributions_on(lang: Lang, n: u64, date: &str) -> String { | |||
| 700 | } | 706 | } |
| 701 | } | 707 | } |
| 702 | 708 | ||
| 709 | +/// The heading above a selected calendar day's contributions, e.g. `Contributions on 16 Oct 2026`. | ||
| 710 | +pub fn contributions_on_day(lang: Lang, date: &str) -> String { | ||
| 711 | + match lang { | ||
| 712 | + Lang::En => format!("Contributions on {date}"), | ||
| 713 | + Lang::Tr => format!("{date} tarihindeki katkılar"), | ||
| 714 | + } | ||
| 715 | +} | ||
| 716 | + | ||
| 717 | +/// The words either side of a day item's link. The link is the repo for a push and `#N title` | ||
| 718 | +/// otherwise, so `repo` (`owner/repo`) goes in the words only where the link is not the repo. | ||
| 719 | +pub fn day_item_words( | ||
| 720 | + lang: Lang, | ||
| 721 | + kind: ContributionKind, | ||
| 722 | + branch: &str, | ||
| 723 | + repo: &str, | ||
| 724 | +) -> (String, String) { | ||
| 725 | + match (lang, kind) { | ||
| 726 | + (Lang::En, ContributionKind::Push) => (format!("Pushed to {branch} in"), String::new()), | ||
| 727 | + (Lang::En, ContributionKind::Issue) => ("Opened issue".to_owned(), format!("in {repo}")), | ||
| 728 | + (Lang::En, ContributionKind::Pull) => { | ||
| 729 | + ("Opened pull request".to_owned(), format!("in {repo}")) | ||
| 730 | + } | ||
| 731 | + (Lang::En, ContributionKind::Comment) => ("Commented on".to_owned(), format!("in {repo}")), | ||
| 732 | + (Lang::Tr, ContributionKind::Push) => { | ||
| 733 | + (String::new(), format!("deposunda {branch} dalına gönderim")) | ||
| 734 | + } | ||
| 735 | + (Lang::Tr, ContributionKind::Issue) => { | ||
| 736 | + (String::new(), format!("sorunu {repo} deposunda açtı")) | ||
| 737 | + } | ||
| 738 | + (Lang::Tr, ContributionKind::Pull) => ( | ||
| 739 | + String::new(), | ||
| 740 | + format!("çekme isteğini {repo} deposunda açtı"), | ||
| 741 | + ), | ||
| 742 | + (Lang::Tr, ContributionKind::Comment) => { | ||
| 743 | + (String::new(), format!("konusuna {repo} deposunda yorum yaptı")) | ||
| 744 | + } | ||
| 745 | + } | ||
| 746 | +} | ||
| 747 | + | ||
| 703 | /// The activity line for an inbox row: the verb, then the actor when there is one. English reads | 748 | /// The activity line for an inbox row: the verb, then the actor when there is one. English reads |
| 704 | /// "opened by tahinli"; Turkish reads "tahinli tarafından açıldı", or "Açıldı" with no actor. | 749 | /// "opened by tahinli"; Turkish reads "tahinli tarafından açıldı", or "Açıldı" with no actor. |
| 705 | pub fn activity_line(lang: Lang, verb: K, actor: Option<&str>) -> String { | 750 | pub fn activity_line(lang: Lang, verb: K, actor: Option<&str>) -> String { |
crates/forge-web/src/pages/dashboard.rsmodified+68 -6
| @@ -7,12 +7,13 @@ use std::time::Duration; | |||
| 7 | 7 | ||
| 8 | use forge_core::store::RepoSort; | 8 | use forge_core::store::RepoSort; |
| 9 | use forge_core::store::repo_listing::RepoScope; | 9 | use forge_core::store::repo_listing::RepoScope; |
| 10 | +use forge_core::time::stamp_from_unix; | ||
| 10 | use forge_core::{Notification, Repo, Visibility}; | 11 | use forge_core::{Notification, Repo, Visibility}; |
| 11 | -use jiff::Timestamp; | 12 | +use jiff::{Timestamp, civil}; |
| 12 | use topcoat::Result; | 13 | use topcoat::Result; |
| 13 | use topcoat::context::{Cx, app_context}; | 14 | use topcoat::context::{Cx, app_context}; |
| 14 | use topcoat::router::response::Response; | 15 | use topcoat::router::response::Response; |
| 15 | -use topcoat::router::{Body, StatusCode, page, route}; | 16 | +use topcoat::router::{Body, StatusCode, page, query_params, route}; |
| 16 | use topcoat::view::{View, component, view}; | 17 | use topcoat::view::{View, component, view}; |
| 17 | 18 | ||
| 18 | use crate::auth::{current_user, percent_encode}; | 19 | use crate::auth::{current_user, percent_encode}; |
| @@ -23,7 +24,7 @@ use crate::live::{Topic, stream_changes}; | |||
| 23 | use crate::pages::issue_parts::stamp_unix; | 24 | use crate::pages::issue_parts::stamp_unix; |
| 24 | use crate::pages::repo_settings::{notice_banner_for, take_notice}; | 25 | use crate::pages::repo_settings::{notice_banner_for, take_notice}; |
| 25 | use crate::state::AppState; | 26 | use crate::state::AppState; |
| 26 | -use crate::views::heatmap::{self, Calendar, contribution_calendar}; | 27 | +use crate::views::heatmap::{self, Calendar, DayDetail, contribution_calendar}; |
| 27 | use crate::views::{SIDE_REPOS, display_name, now_unix, relative_time, shell}; | 28 | use crate::views::{SIDE_REPOS, display_name, now_unix, relative_time, shell}; |
| 28 | 29 | ||
| 29 | /// How far back the calendar reads contributions: 53 weeks plus the days of the current week. | 30 | /// How far back the calendar reads contributions: 53 weeks plus the days of the current week. |
| @@ -35,6 +36,12 @@ fn calendar_since() -> String { | |||
| 35 | forge_core::time::stamp_ago(Duration::from_secs((CALENDAR_DAYS + 1) * 86_400)) | 36 | forge_core::time::stamp_ago(Duration::from_secs((CALENDAR_DAYS + 1) * 86_400)) |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 39 | +/// The `day` query: a calendar day whose contributions to list under the calendar. | ||
| 40 | +#[query_params(error = bad_request)] | ||
| 41 | +struct DayQuery { | ||
| 42 | + day: Option<String>, | ||
| 43 | +} | ||
| 44 | + | ||
| 38 | /// Unread inbox rows in the "Needs your attention" column. | 45 | /// Unread inbox rows in the "Needs your attention" column. |
| 39 | const ATTENTION_ROWS: u32 = 5; | 46 | const ATTENTION_ROWS: u32 = 5; |
| 40 | 47 | ||
| @@ -44,6 +51,7 @@ struct Dashboard { | |||
| 44 | hour: u8, | 51 | hour: u8, |
| 45 | repos: Vec<Repo>, | 52 | repos: Vec<Repo>, |
| 46 | calendar: Calendar, | 53 | calendar: Calendar, |
| 54 | + selected: Option<DayDetail>, | ||
| 47 | attention: Vec<AttentionRow>, | 55 | attention: Vec<AttentionRow>, |
| 48 | } | 56 | } |
| 49 | 57 | ||
| @@ -63,8 +71,9 @@ async fn home(cx: &Cx) -> Result<impl View> { | |||
| 63 | let state = app_context::<AppState>(cx); | 71 | let state = app_context::<AppState>(cx); |
| 64 | let viewer = current_user(cx).await?; | 72 | let viewer = current_user(cx).await?; |
| 65 | let lang = i18n::lang(cx, viewer.as_ref().map(|(u, _)| u)); | 73 | let lang = i18n::lang(cx, viewer.as_ref().map(|(u, _)| u)); |
| 74 | + let query = query_params::<DayQuery>(cx)?; | ||
| 66 | let dash = match &viewer { | 75 | let dash = match &viewer { |
| 67 | - Some((user, _)) => Some(load_dashboard(state, user, lang).await?), | 76 | + Some((user, _)) => Some(load_dashboard(state, user, lang, query.day.as_deref()).await?), |
| 68 | None => None, | 77 | None => None, |
| 69 | }; | 78 | }; |
| 70 | let has_users = viewer.is_none() && state.store.user_count().await? > 0; | 79 | let has_users = viewer.is_none() && state.store.user_count().await? > 0; |
| @@ -172,11 +181,59 @@ async fn dashboard_fingerprint(state: &AppState, user: &forge_core::User) -> Res | |||
| 172 | Ok(lines.join("\n")) | 181 | Ok(lines.join("\n")) |
| 173 | } | 182 | } |
| 174 | 183 | ||
| 175 | -/// Reads the dashboard data for a signed-in user. | 184 | +/// The day the `day` query asks for: a strict `YYYY-MM-DD` date from the calendar's first day to |
| 185 | +/// today. Anything else (malformed, a future day, or before the calendar) is ignored. | ||
| 186 | +fn selected_day( | ||
| 187 | + day: Option<&str>, | ||
| 188 | + first: (i64, i64, i64), | ||
| 189 | + today: (i64, i64, i64), | ||
| 190 | +) -> Option<civil::Date> { | ||
| 191 | + let text = day?; | ||
| 192 | + let date: civil::Date = text.parse().ok()?; | ||
| 193 | + // The parser takes more forms than the calendar link uses, so the date must print back as sent. | ||
| 194 | + if date.to_string() != text { | ||
| 195 | + return None; | ||
| 196 | + } | ||
| 197 | + let ymd = ( | ||
| 198 | + i64::from(date.year()), | ||
| 199 | + i64::from(date.month()), | ||
| 200 | + i64::from(date.day()), | ||
| 201 | + ); | ||
| 202 | + (first..=today).contains(&ymd).then_some(date) | ||
| 203 | +} | ||
| 204 | + | ||
| 205 | +/// The contributions the user made on their local day `date`, newest first. | ||
| 206 | +async fn day_detail( | ||
| 207 | + state: &AppState, | ||
| 208 | + user: &forge_core::User, | ||
| 209 | + date: civil::Date, | ||
| 210 | +) -> Result<Option<DayDetail>> { | ||
| 211 | + let Some((start, end)) = forge_core::tz::day_span(date, &user.timezone) else { | ||
| 212 | + return Ok(None); | ||
| 213 | + }; | ||
| 214 | + let (Some(from), Some(to)) = (stamp_from_unix(start), stamp_from_unix(end)) else { | ||
| 215 | + return Ok(None); | ||
| 216 | + }; | ||
| 217 | + let items = state | ||
| 218 | + .store | ||
| 219 | + .contributions_between(&user.id, &from, &to) | ||
| 220 | + .await?; | ||
| 221 | + Ok(Some(DayDetail { | ||
| 222 | + date: ( | ||
| 223 | + i64::from(date.year()), | ||
| 224 | + i64::from(date.month()), | ||
| 225 | + i64::from(date.day()), | ||
| 226 | + ), | ||
| 227 | + items, | ||
| 228 | + })) | ||
| 229 | +} | ||
| 230 | + | ||
| 231 | +/// Reads the dashboard data for a signed-in user. `day` is the raw `day` query value. | ||
| 176 | async fn load_dashboard( | 232 | async fn load_dashboard( |
| 177 | state: &AppState, | 233 | state: &AppState, |
| 178 | user: &forge_core::User, | 234 | user: &forge_core::User, |
| 179 | lang: Lang, | 235 | lang: Lang, |
| 236 | + day: Option<&str>, | ||
| 180 | ) -> Result<Dashboard> { | 237 | ) -> Result<Dashboard> { |
| 181 | let repos = state.store.list_repos_by_owner(&user.id).await?; | 238 | let repos = state.store.list_repos_by_owner(&user.id).await?; |
| 182 | let local_now = forge_core::tz::local_datetime(Timestamp::now(), &user.timezone); | 239 | let local_now = forge_core::tz::local_datetime(Timestamp::now(), &user.timezone); |
| @@ -190,6 +247,10 @@ async fn load_dashboard( | |||
| 190 | .contribution_counts(&user.id, &calendar_since(), &user.timezone) | 247 | .contribution_counts(&user.id, &calendar_since(), &user.timezone) |
| 191 | .await?; | 248 | .await?; |
| 192 | let calendar = heatmap::build(&counts, today); | 249 | let calendar = heatmap::build(&counts, today); |
| 250 | + let selected = match selected_day(day, calendar.first_day, today) { | ||
| 251 | + Some(date) => day_detail(state, user, date).await?, | ||
| 252 | + None => None, | ||
| 253 | + }; | ||
| 193 | let now_secs = now_unix(); | 254 | let now_secs = now_unix(); |
| 194 | let attention = state | 255 | let attention = state |
| 195 | .store | 256 | .store |
| @@ -203,6 +264,7 @@ async fn load_dashboard( | |||
| 203 | hour: u8::try_from(local_now.hour()).unwrap_or_default(), | 264 | hour: u8::try_from(local_now.hour()).unwrap_or_default(), |
| 204 | repos, | 265 | repos, |
| 205 | calendar, | 266 | calendar, |
| 267 | + selected, | ||
| 206 | attention, | 268 | attention, |
| 207 | }) | 269 | }) |
| 208 | } | 270 | } |
| @@ -232,7 +294,7 @@ async fn dashboard(dash: Dashboard, lang: Lang) -> Result<impl View> { | |||
| 232 | Ok(view! { | 294 | Ok(view! { |
| 233 | <div data-live="/dashboard/events"> | 295 | <div data-live="/dashboard/events"> |
| 234 | <h1 class="greet">(greeting)</h1> | 296 | <h1 class="greet">(greeting)</h1> |
| 235 | - contribution_calendar(calendar: dash.calendar, lang: lang) | 297 | + contribution_calendar(calendar: dash.calendar, selected: dash.selected, lang: lang) |
| 236 | <div class="dash-cols"> | 298 | <div class="dash-cols"> |
| 237 | <section class="dash-repos" aria-labelledby="repos-h"> | 299 | <section class="dash-repos" aria-labelledby="repos-h"> |
| 238 | <div class="sec-head"> | 300 | <div class="sec-head"> |
crates/forge-web/src/views/heatmap.rsmodified+139 -22
| @@ -4,13 +4,17 @@ | |||
| 4 | //! `build` is pure date and streak maths over the counts from the store. `contribution_calendar` | 4 | //! `build` is pure date and streak maths over the counts from the store. `contribution_calendar` |
| 5 | //! draws it with classes only (CSP): the grid placement comes from CSS (`grid-auto-flow: column` | 5 | //! draws it with classes only (CSP): the grid placement comes from CSS (`grid-auto-flow: column` |
| 6 | //! with seven rows), so every cell is an element and the blank slots keep the columns aligned. | 6 | //! with seven rows), so every cell is an element and the blank slots keep the columns aligned. |
| 7 | +//! Each day is a link to `?day=` (a plain link, no script): the page then lists that day's | ||
| 8 | +//! contributions under the calendar, and clicking the selected day again clears it. | ||
| 7 | 9 | ||
| 8 | use std::collections::HashMap; | 10 | use std::collections::HashMap; |
| 9 | 11 | ||
| 12 | +use forge_core::store::contributions::{ContributionItem, ContributionKind}; | ||
| 13 | +use forge_core::time::stamp_to_unix; | ||
| 10 | use topcoat::Result; | 14 | use topcoat::Result; |
| 11 | use topcoat::view::{View, component, view}; | 15 | use topcoat::view::{View, component, view}; |
| 12 | 16 | ||
| 13 | -use super::civil_from_days; | 17 | +use super::{civil_from_days, now_unix, relative_time}; |
| 14 | use crate::i18n::Lang; | 18 | use crate::i18n::Lang; |
| 15 | use crate::i18n::misc::{self as g, K}; | 19 | use crate::i18n::misc::{self as g, K}; |
| 16 | 20 | ||
| @@ -28,6 +32,13 @@ pub struct Day { | |||
| 28 | pub level: u8, | 32 | pub level: u8, |
| 29 | } | 33 | } |
| 30 | 34 | ||
| 35 | +/// The day picked in the calendar: its date and the contributions made on it, newest first. | ||
| 36 | +#[derive(Clone, Debug, PartialEq, Eq)] | ||
| 37 | +pub struct DayDetail { | ||
| 38 | + pub date: (i64, i64, i64), | ||
| 39 | + pub items: Vec<ContributionItem>, | ||
| 40 | +} | ||
| 41 | + | ||
| 31 | /// The calendar as the component draws it. `weeks[c][r]` is the day in column `c` (a week, | 42 | /// The calendar as the component draws it. `weeks[c][r]` is the day in column `c` (a week, |
| 32 | /// Monday first) and row `r` (0 = Monday, 6 = Sunday); `None` for the days after today. | 43 | /// Monday first) and row `r` (0 = Monday, 6 = Sunday); `None` for the days after today. |
| 33 | #[derive(Clone, Debug, PartialEq, Eq)] | 44 | #[derive(Clone, Debug, PartialEq, Eq)] |
| @@ -166,16 +177,80 @@ fn weekday(days: i64) -> u8 { | |||
| 166 | (days + 3).rem_euclid(7) as u8 | 177 | (days + 3).rem_euclid(7) as u8 |
| 167 | } | 178 | } |
| 168 | 179 | ||
| 169 | -/// One grid slot as the view draws it. | 180 | +/// One grid slot as the view draws it. Days link to their contributions; blank slots do not. |
| 170 | struct CellView { | 181 | struct CellView { |
| 171 | - class: &'static str, | 182 | + class: String, |
| 183 | + href: Option<String>, | ||
| 172 | title: String, | 184 | title: String, |
| 173 | } | 185 | } |
| 174 | 186 | ||
| 187 | +/// One line of the selected day's list, with its strings ready for the view. | ||
| 188 | +struct DayRow { | ||
| 189 | + before: String, | ||
| 190 | + href: String, | ||
| 191 | + link: String, | ||
| 192 | + after: String, | ||
| 193 | + updated: String, | ||
| 194 | + relative: String, | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +/// The selected day's heading and rows, ready for the view. | ||
| 198 | +struct DayView { | ||
| 199 | + heading: String, | ||
| 200 | + rows: Vec<DayRow>, | ||
| 201 | +} | ||
| 202 | + | ||
| 203 | +/// The link for a calendar day: the day's contributions below the calendar, or back to the | ||
| 204 | +/// plain calendar when the day is the selected one. | ||
| 205 | +fn day_href(date: (i64, i64, i64), selected: bool) -> String { | ||
| 206 | + if selected { | ||
| 207 | + "/#contributions-h".to_owned() | ||
| 208 | + } else { | ||
| 209 | + format!( | ||
| 210 | + "/?day={:04}-{:02}-{:02}#contributions-h", | ||
| 211 | + date.0, date.1, date.2 | ||
| 212 | + ) | ||
| 213 | + } | ||
| 214 | +} | ||
| 215 | + | ||
| 216 | +/// The row for one contribution in the selected day's list. | ||
| 217 | +fn day_row(lang: Lang, item: &ContributionItem, now: i64) -> DayRow { | ||
| 218 | + let repo = format!("{}/{}", item.repo_owner, item.repo_name); | ||
| 219 | + let branch = item | ||
| 220 | + .ref_name | ||
| 221 | + .strip_prefix("refs/heads/") | ||
| 222 | + .unwrap_or(&item.ref_name); | ||
| 223 | + let (before, after) = g::day_item_words(lang, item.kind, branch, &repo); | ||
| 224 | + let (href, link) = match item.kind { | ||
| 225 | + ContributionKind::Push => (format!("/{repo}"), repo.clone()), | ||
| 226 | + ContributionKind::Issue | ContributionKind::Comment => ( | ||
| 227 | + format!("/{repo}/issues/{}", item.number), | ||
| 228 | + format!("#{} {}", item.number, item.title), | ||
| 229 | + ), | ||
| 230 | + ContributionKind::Pull => ( | ||
| 231 | + format!("/{repo}/pulls/{}", item.number), | ||
| 232 | + format!("#{} {}", item.number, item.title), | ||
| 233 | + ), | ||
| 234 | + }; | ||
| 235 | + DayRow { | ||
| 236 | + before, | ||
| 237 | + href, | ||
| 238 | + link, | ||
| 239 | + after, | ||
| 240 | + updated: item.created_at.clone(), | ||
| 241 | + relative: relative_time(lang, stamp_to_unix(&item.created_at).unwrap_or(now), now), | ||
| 242 | + } | ||
| 243 | +} | ||
| 244 | + | ||
| 175 | /// The contribution card: heading and date range, the four stats, the month labels and the | 245 | /// The contribution card: heading and date range, the four stats, the month labels and the |
| 176 | -/// 53-week grid, then the motivating line and the Less/More legend. | 246 | +/// 53-week grid, then the motivating line and the Less/More legend, then the selected day's |
| 247 | +/// contributions when a day is picked. | ||
| 177 | #[component] | 248 | #[component] |
| 178 | -pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<impl View> { | 249 | +pub async fn contribution_calendar( |
| 250 | + calendar: Calendar, | ||
| 251 | + selected: Option<DayDetail>, | ||
| 252 | + lang: Lang, | ||
| 253 | +) -> Result<impl View> { | ||
| 179 | let range = { | 254 | let range = { |
| 180 | let from = g::date_text( | 255 | let from = g::date_text( |
| 181 | lang, | 256 | lang, |
| @@ -202,6 +277,20 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | |||
| 202 | let fri = g::t(lang, K::FriLabel); | 277 | let fri = g::t(lang, K::FriLabel); |
| 203 | let less = g::t(lang, K::Less); | 278 | let less = g::t(lang, K::Less); |
| 204 | let more = g::t(lang, K::More); | 279 | let more = g::t(lang, K::More); |
| 280 | + let empty_day = g::t(lang, K::NoContributionsDay); | ||
| 281 | + let now = now_unix(); | ||
| 282 | + let selected_date = selected.as_ref().map(|detail| detail.date); | ||
| 283 | + let day = selected.map(|detail| DayView { | ||
| 284 | + heading: g::contributions_on_day( | ||
| 285 | + lang, | ||
| 286 | + &g::date_text(lang, detail.date.0, detail.date.1, detail.date.2), | ||
| 287 | + ), | ||
| 288 | + rows: detail | ||
| 289 | + .items | ||
| 290 | + .iter() | ||
| 291 | + .map(|item| day_row(lang, item, now)) | ||
| 292 | + .collect(), | ||
| 293 | + }); | ||
| 205 | 294 | ||
| 206 | // Month labels: one slot per column, empty where no label starts. | 295 | // Month labels: one slot per column, empty where no label starts. |
| 207 | let mut month_slots = vec![String::new(); WEEKS]; | 296 | let mut month_slots = vec![String::new(); WEEKS]; |
| @@ -215,16 +304,26 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | |||
| 215 | .iter() | 304 | .iter() |
| 216 | .flat_map(|week| week.iter()) | 305 | .flat_map(|week| week.iter()) |
| 217 | .map(|slot| match slot { | 306 | .map(|slot| match slot { |
| 218 | - Some(day) => CellView { | 307 | + Some(day) => { |
| 219 | - class: LEVEL_CLASS[usize::from(day.level)], | 308 | + let picked = selected_date == Some(day.date); |
| 220 | - title: g::contributions_on( | 309 | + let level = LEVEL_CLASS[usize::from(day.level)]; |
| 221 | - lang, | 310 | + CellView { |
| 222 | - day.count, | 311 | + class: if picked { |
| 223 | - &g::date_text(lang, day.date.0, day.date.1, day.date.2), | 312 | + format!("{level} selected") |
| 224 | - ), | 313 | + } else { |
| 225 | - }, | 314 | + level.to_owned() |
| 315 | + }, | ||
| 316 | + href: Some(day_href(day.date, picked)), | ||
| 317 | + title: g::contributions_on( | ||
| 318 | + lang, | ||
| 319 | + day.count, | ||
| 320 | + &g::date_text(lang, day.date.0, day.date.1, day.date.2), | ||
| 321 | + ), | ||
| 322 | + } | ||
| 323 | + } | ||
| 226 | None => CellView { | 324 | None => CellView { |
| 227 | - class: "cell blank", | 325 | + class: "cell blank".to_owned(), |
| 326 | + href: None, | ||
| 228 | title: String::new(), | 327 | title: String::new(), |
| 229 | }, | 328 | }, |
| 230 | }) | 329 | }) |
| @@ -273,9 +372,13 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | |||
| 273 | <span>(label.as_str())</span> | 372 | <span>(label.as_str())</span> |
| 274 | } | 373 | } |
| 275 | </div> | 374 | </div> |
| 276 | - <div class="heat-grid" role="img" aria-label=(summary.as_str())> | 375 | + <div class="heat-grid" role="group" aria-label=(summary.as_str())> |
| 277 | for cell in cells.into_iter() { | 376 | for cell in cells.into_iter() { |
| 278 | - <span class=(cell.class) title=(cell.title.as_str())></span> | 377 | + if let Some(href) = cell.href { |
| 378 | + <a class=(cell.class.as_str()) href=(href.as_str()) title=(cell.title.as_str())></a> | ||
| 379 | + } else { | ||
| 380 | + <span class=(cell.class.as_str()) title=(cell.title.as_str())></span> | ||
| 381 | + } | ||
| 279 | } | 382 | } |
| 280 | </div> | 383 | </div> |
| 281 | </div> | 384 | </div> |
| @@ -293,6 +396,25 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | |||
| 293 | <span>(more)</span> | 396 | <span>(more)</span> |
| 294 | </div> | 397 | </div> |
| 295 | </div> | 398 | </div> |
| 399 | + if let Some(day) = day { | ||
| 400 | + <div class="heat-day"> | ||
| 401 | + <h3>(day.heading.as_str())</h3> | ||
| 402 | + if day.rows.is_empty() { | ||
| 403 | + <p class="muted">(empty_day)</p> | ||
| 404 | + } else { | ||
| 405 | + <ul> | ||
| 406 | + for row in day.rows.into_iter() { | ||
| 407 | + <li> | ||
| 408 | + <span>(row.before.as_str())</span> | ||
| 409 | + <a href=(row.href.as_str())>(row.link.as_str())</a> | ||
| 410 | + <span>(row.after.as_str())</span> | ||
| 411 | + <time data-rel="true" datetime=(row.updated.as_str())>(row.relative.as_str())</time> | ||
| 412 | + </li> | ||
| 413 | + } | ||
| 414 | + </ul> | ||
| 415 | + } | ||
| 416 | + </div> | ||
| 417 | + } | ||
| 296 | </section> | 418 | </section> |
| 297 | }) | 419 | }) |
| 298 | } | 420 | } |
| @@ -356,12 +478,7 @@ mod tests { | |||
| 356 | // (case, counts, current streak, longest streak) | 478 | // (case, counts, current streak, longest streak) |
| 357 | type Case<'a> = (&'a str, &'a [(&'a str, u32)], u64, u64); | 479 | type Case<'a> = (&'a str, &'a [(&'a str, u32)], u64, u64); |
| 358 | let cases: [Case<'_>; 2] = [ | 480 | let cases: [Case<'_>; 2] = [ |
| 359 | - ( | 481 | + ("old run", &[("2026-10-01", 3), ("2026-10-02", 3)], 0, 2), |
| 360 | - "old run", | ||
| 361 | - &[("2026-10-01", 3), ("2026-10-02", 3)], | ||
| 362 | - 0, | ||
| 363 | - 2, | ||
| 364 | - ), | ||
| 365 | ( | 482 | ( |
| 366 | "gap then two days", | 483 | "gap then two days", |
| 367 | &[ | 484 | &[ |
style/_heatmap.scssmodified+49 -0
| @@ -194,6 +194,16 @@ $gap: 3px; | |||
| 194 | outline-offset: 0; | 194 | outline-offset: 0; |
| 195 | } | 195 | } |
| 196 | 196 | ||
| 197 | +// A day is a link: it looks like its plain cell, and the picked day gets an accent outline. | ||
| 198 | +a.cell { | ||
| 199 | + text-decoration: none; | ||
| 200 | +} | ||
| 201 | + | ||
| 202 | +.heat-grid .cell.selected { | ||
| 203 | + outline: 2px solid var(--accent-ink); | ||
| 204 | + outline-offset: 1px; | ||
| 205 | +} | ||
| 206 | + | ||
| 197 | .cell.blank { | 207 | .cell.blank { |
| 198 | visibility: hidden; | 208 | visibility: hidden; |
| 199 | } | 209 | } |
| @@ -212,6 +222,45 @@ $gap: 3px; | |||
| 212 | } | 222 | } |
| 213 | } | 223 | } |
| 214 | 224 | ||
| 225 | +.heat-day { | ||
| 226 | + margin-top: 20px; | ||
| 227 | + padding-top: 18px; | ||
| 228 | + border-top: 1px solid var(--border); | ||
| 229 | + | ||
| 230 | + h3 { | ||
| 231 | + margin: 0 0 12px; | ||
| 232 | + font-size: 15px; | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + p { | ||
| 236 | + margin: 0; | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + ul { | ||
| 240 | + display: grid; | ||
| 241 | + gap: 10px; | ||
| 242 | + margin: 0; | ||
| 243 | + padding: 0; | ||
| 244 | + list-style: none; | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + li { | ||
| 248 | + display: flex; | ||
| 249 | + flex-wrap: wrap; | ||
| 250 | + align-items: baseline; | ||
| 251 | + gap: 2px 6px; | ||
| 252 | + font-size: 14px; | ||
| 253 | + color: var(--text); | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + time { | ||
| 257 | + margin-left: auto; | ||
| 258 | + font-size: 13px; | ||
| 259 | + color: var(--muted); | ||
| 260 | + white-space: nowrap; | ||
| 261 | + } | ||
| 262 | +} | ||
| 263 | + | ||
| 215 | .legend { | 264 | .legend { |
| 216 | display: flex; | 265 | display: flex; |
| 217 | align-items: center; | 266 | align-items: center; |