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 | 3 | use std::collections::BTreeMap; |
| 4 | 4 | |
| 5 | 5 | use jiff::Timestamp; |
| 6 | -use turso::Value; | |
| 6 | +use turso::{Row, Value}; | |
| 7 | 7 | |
| 8 | 8 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; |
| 9 | 9 | use super::{Store, col_int, col_text, fetch_all}; |
| 10 | -use crate::error::Result; | |
| 10 | +use crate::error::{Error, Result}; | |
| 11 | 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 | 64 | impl Store { |
| 14 | 65 | /// Contributions per day in `tz` by `user_id` at or after `since` (an RFC 3339 stamp), as |
| 15 | 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 | 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 | 153 | #[cfg(test)] |
| @@ -81,6 +170,56 @@ mod tests { | ||
| 81 | 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 | 223 | #[tokio::test] |
| 85 | 224 | async fn counts_pushes_issues_pulls_and_comments_per_day() { |
| 86 | 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 | 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 | 51 | /// Unix seconds for a stamp written by [`now_stamp`]. `None` when the text is not a stamp. |
| 44 | 52 | pub fn stamp_to_unix(stamp: &str) -> Option<i64> { |
| 45 | 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 | 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 | 37 | /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC". |
| 23 | 38 | pub fn validate_timezone(name: &str) -> Result<()> { |
| 24 | 39 | TimeZone::get(name) |
crates/forge-web/src/i18n/misc.rsmodified+45 -0
| @@ -6,6 +6,8 @@ | ||
| 6 | 6 | |
| 7 | 7 | use std::fmt::Display; |
| 8 | 8 | |
| 9 | +use forge_core::store::contributions::ContributionKind; | |
| 10 | + | |
| 9 | 11 | use super::Lang; |
| 10 | 12 | |
| 11 | 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| @@ -144,6 +146,7 @@ pub enum K { | ||
| 144 | 146 | MonLabel, |
| 145 | 147 | WedLabel, |
| 146 | 148 | FriLabel, |
| 149 | + NoContributionsDay, | |
| 147 | 150 | // Dashboard greeting and "Needs your attention" column. |
| 148 | 151 | GoodMorning, |
| 149 | 152 | GoodAfternoon, |
| @@ -285,6 +288,7 @@ const ALL: &[K] = &[ | ||
| 285 | 288 | K::MonLabel, |
| 286 | 289 | K::WedLabel, |
| 287 | 290 | K::FriLabel, |
| 291 | + K::NoContributionsDay, | |
| 288 | 292 | K::GoodMorning, |
| 289 | 293 | K::GoodAfternoon, |
| 290 | 294 | K::GoodEvening, |
| @@ -572,6 +576,8 @@ pub fn t(lang: Lang, key: K) -> &'static str { | ||
| 572 | 576 | (WedLabel, Tr) => "Çar", |
| 573 | 577 | (FriLabel, En) => "Fri", |
| 574 | 578 | (FriLabel, Tr) => "Cum", |
| 579 | + (NoContributionsDay, En) => "No contributions on this day.", | |
| 580 | + (NoContributionsDay, Tr) => "Bu günde katkı yok.", | |
| 575 | 581 | (GoodMorning, En) => "Good morning", |
| 576 | 582 | (GoodMorning, Tr) => "Günaydın", |
| 577 | 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 | 748 | /// The activity line for an inbox row: the verb, then the actor when there is one. English reads |
| 704 | 749 | /// "opened by tahinli"; Turkish reads "tahinli tarafından açıldı", or "Açıldı" with no actor. |
| 705 | 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 | 8 | use forge_core::store::RepoSort; |
| 9 | 9 | use forge_core::store::repo_listing::RepoScope; |
| 10 | +use forge_core::time::stamp_from_unix; | |
| 10 | 11 | use forge_core::{Notification, Repo, Visibility}; |
| 11 | -use jiff::Timestamp; | |
| 12 | +use jiff::{Timestamp, civil}; | |
| 12 | 13 | use topcoat::Result; |
| 13 | 14 | use topcoat::context::{Cx, app_context}; |
| 14 | 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 | 17 | use topcoat::view::{View, component, view}; |
| 17 | 18 | |
| 18 | 19 | use crate::auth::{current_user, percent_encode}; |
| @@ -23,7 +24,7 @@ use crate::live::{Topic, stream_changes}; | ||
| 23 | 24 | use crate::pages::issue_parts::stamp_unix; |
| 24 | 25 | use crate::pages::repo_settings::{notice_banner_for, take_notice}; |
| 25 | 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 | 28 | use crate::views::{SIDE_REPOS, display_name, now_unix, relative_time, shell}; |
| 28 | 29 | |
| 29 | 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 | 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 | 45 | /// Unread inbox rows in the "Needs your attention" column. |
| 39 | 46 | const ATTENTION_ROWS: u32 = 5; |
| 40 | 47 | |
| @@ -44,6 +51,7 @@ struct Dashboard { | ||
| 44 | 51 | hour: u8, |
| 45 | 52 | repos: Vec<Repo>, |
| 46 | 53 | calendar: Calendar, |
| 54 | + selected: Option<DayDetail>, | |
| 47 | 55 | attention: Vec<AttentionRow>, |
| 48 | 56 | } |
| 49 | 57 | |
| @@ -63,8 +71,9 @@ async fn home(cx: &Cx) -> Result<impl View> { | ||
| 63 | 71 | let state = app_context::<AppState>(cx); |
| 64 | 72 | let viewer = current_user(cx).await?; |
| 65 | 73 | let lang = i18n::lang(cx, viewer.as_ref().map(|(u, _)| u)); |
| 74 | + let query = query_params::<DayQuery>(cx)?; | |
| 66 | 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 | 77 | None => None, |
| 69 | 78 | }; |
| 70 | 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 | 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 | 232 | async fn load_dashboard( |
| 177 | 233 | state: &AppState, |
| 178 | 234 | user: &forge_core::User, |
| 179 | 235 | lang: Lang, |
| 236 | + day: Option<&str>, | |
| 180 | 237 | ) -> Result<Dashboard> { |
| 181 | 238 | let repos = state.store.list_repos_by_owner(&user.id).await?; |
| 182 | 239 | let local_now = forge_core::tz::local_datetime(Timestamp::now(), &user.timezone); |
| @@ -190,6 +247,10 @@ async fn load_dashboard( | ||
| 190 | 247 | .contribution_counts(&user.id, &calendar_since(), &user.timezone) |
| 191 | 248 | .await?; |
| 192 | 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 | 254 | let now_secs = now_unix(); |
| 194 | 255 | let attention = state |
| 195 | 256 | .store |
| @@ -203,6 +264,7 @@ async fn load_dashboard( | ||
| 203 | 264 | hour: u8::try_from(local_now.hour()).unwrap_or_default(), |
| 204 | 265 | repos, |
| 205 | 266 | calendar, |
| 267 | + selected, | |
| 206 | 268 | attention, |
| 207 | 269 | }) |
| 208 | 270 | } |
| @@ -232,7 +294,7 @@ async fn dashboard(dash: Dashboard, lang: Lang) -> Result<impl View> { | ||
| 232 | 294 | Ok(view! { |
| 233 | 295 | <div data-live="/dashboard/events"> |
| 234 | 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 | 298 | <div class="dash-cols"> |
| 237 | 299 | <section class="dash-repos" aria-labelledby="repos-h"> |
| 238 | 300 | <div class="sec-head"> |
crates/forge-web/src/views/heatmap.rsmodified+139 -22
| @@ -4,13 +4,17 @@ | ||
| 4 | 4 | //! `build` is pure date and streak maths over the counts from the store. `contribution_calendar` |
| 5 | 5 | //! draws it with classes only (CSP): the grid placement comes from CSS (`grid-auto-flow: column` |
| 6 | 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 | 10 | use std::collections::HashMap; |
| 9 | 11 | |
| 12 | +use forge_core::store::contributions::{ContributionItem, ContributionKind}; | |
| 13 | +use forge_core::time::stamp_to_unix; | |
| 10 | 14 | use topcoat::Result; |
| 11 | 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 | 18 | use crate::i18n::Lang; |
| 15 | 19 | use crate::i18n::misc::{self as g, K}; |
| 16 | 20 | |
| @@ -28,6 +32,13 @@ pub struct Day { | ||
| 28 | 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 | 42 | /// The calendar as the component draws it. `weeks[c][r]` is the day in column `c` (a week, |
| 32 | 43 | /// Monday first) and row `r` (0 = Monday, 6 = Sunday); `None` for the days after today. |
| 33 | 44 | #[derive(Clone, Debug, PartialEq, Eq)] |
| @@ -166,16 +177,80 @@ fn weekday(days: i64) -> u8 { | ||
| 166 | 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 | 181 | struct CellView { |
| 171 | - class: &'static str, | |
| 182 | + class: String, | |
| 183 | + href: Option<String>, | |
| 172 | 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 | 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 | 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 | 254 | let range = { |
| 180 | 255 | let from = g::date_text( |
| 181 | 256 | lang, |
| @@ -202,6 +277,20 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | ||
| 202 | 277 | let fri = g::t(lang, K::FriLabel); |
| 203 | 278 | let less = g::t(lang, K::Less); |
| 204 | 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 | 295 | // Month labels: one slot per column, empty where no label starts. |
| 207 | 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 | 304 | .iter() |
| 216 | 305 | .flat_map(|week| week.iter()) |
| 217 | 306 | .map(|slot| match slot { |
| 218 | - Some(day) => CellView { | |
| 219 | - class: LEVEL_CLASS[usize::from(day.level)], | |
| 220 | - title: g::contributions_on( | |
| 221 | - lang, | |
| 222 | - day.count, | |
| 223 | - &g::date_text(lang, day.date.0, day.date.1, day.date.2), | |
| 224 | - ), | |
| 225 | - }, | |
| 307 | + Some(day) => { | |
| 308 | + let picked = selected_date == Some(day.date); | |
| 309 | + let level = LEVEL_CLASS[usize::from(day.level)]; | |
| 310 | + CellView { | |
| 311 | + class: if picked { | |
| 312 | + format!("{level} selected") | |
| 313 | + } else { | |
| 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 | 324 | None => CellView { |
| 227 | - class: "cell blank", | |
| 325 | + class: "cell blank".to_owned(), | |
| 326 | + href: None, | |
| 228 | 327 | title: String::new(), |
| 229 | 328 | }, |
| 230 | 329 | }) |
| @@ -273,9 +372,13 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | ||
| 273 | 372 | <span>(label.as_str())</span> |
| 274 | 373 | } |
| 275 | 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 | 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 | 383 | </div> |
| 281 | 384 | </div> |
| @@ -293,6 +396,25 @@ pub async fn contribution_calendar(calendar: Calendar, lang: Lang) -> Result<imp | ||
| 293 | 396 | <span>(more)</span> |
| 294 | 397 | </div> |
| 295 | 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 | 418 | </section> |
| 297 | 419 | }) |
| 298 | 420 | } |
| @@ -356,12 +478,7 @@ mod tests { | ||
| 356 | 478 | // (case, counts, current streak, longest streak) |
| 357 | 479 | type Case<'a> = (&'a str, &'a [(&'a str, u32)], u64, u64); |
| 358 | 480 | let cases: [Case<'_>; 2] = [ |
| 359 | - ( | |
| 360 | - "old run", | |
| 361 | - &[("2026-10-01", 3), ("2026-10-02", 3)], | |
| 362 | - 0, | |
| 363 | - 2, | |
| 364 | - ), | |
| 481 | + ("old run", &[("2026-10-01", 3), ("2026-10-02", 3)], 0, 2), | |
| 365 | 482 | ( |
| 366 | 483 | "gap then two days", |
| 367 | 484 | &[ |
style/_heatmap.scssmodified+49 -0
| @@ -194,6 +194,16 @@ $gap: 3px; | ||
| 194 | 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 | 207 | .cell.blank { |
| 198 | 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 | 264 | .legend { |
| 216 | 265 | display: flex; |
| 217 | 266 | align-items: center; |