Skip to content

Commit 204a0fd

Browse files
authored
feat(desktop): contribution heatmap and graphical cards on projects overview (#1497)
1 parent 8e3c0ee commit 204a0fd

6 files changed

Lines changed: 543 additions & 139 deletions

File tree

‎desktop/src/features/projects/hooks.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export type ProjectActivitySummary = {
8484
createdAt: number;
8585
title: string;
8686
} | null;
87+
/** Activity event counts bucketed by local-time day key ("YYYY-MM-DD"). */
88+
activityByDay: Record<string, number>;
8789
};
8890

8991
export type {

‎desktop/src/features/projects/projectActivity.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ function normalizePubkey(pubkey) {
1717
return /^[a-fA-F0-9]{64}$/.test(pubkey) ? pubkey.toLowerCase() : null;
1818
}
1919

20+
// Local-time day key ("YYYY-MM-DD") so contribution graphs align with the
21+
// viewer's calendar, matching how GitHub buckets contribution days.
22+
function activityDayKey(createdAtSeconds) {
23+
const date = new Date(createdAtSeconds * 1000);
24+
const month = `${date.getMonth() + 1}`.padStart(2, "0");
25+
const day = `${date.getDate()}`.padStart(2, "0");
26+
return `${date.getFullYear()}-${month}-${day}`;
27+
}
28+
2029
function ensureSummary(summaryByRepoAddress, repoAddress) {
2130
const existing = summaryByRepoAddress.get(repoAddress);
2231
if (existing) return existing;
@@ -30,6 +39,7 @@ function ensureSummary(summaryByRepoAddress, repoAddress) {
3039
updatedAt: 0,
3140
participantPubkeys: [],
3241
latestCommit: null,
42+
activityByDay: {},
3343
};
3444
summaryByRepoAddress.set(repoAddress, summary);
3545
return summary;
@@ -61,6 +71,9 @@ export function summarizeProjectActivityEvents(events, projects) {
6171
summary.activityCount += 1;
6272
summary.updatedAt = Math.max(summary.updatedAt, event.created_at);
6373

74+
const dayKey = activityDayKey(event.created_at);
75+
summary.activityByDay[dayKey] = (summary.activityByDay[dayKey] ?? 0) + 1;
76+
6477
if (event.kind === 1621) {
6578
summary.issueCount += 1;
6679
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { cn } from "@/shared/lib/cn";
2+
3+
const WEEK_COUNT = 26;
4+
const DAYS_PER_WEEK = 7;
5+
6+
// Intensity ramp shared by the cells and the "Less … More" legend.
7+
const LEVEL_CLASSES = [
8+
"bg-muted/60 dark:bg-muted/40",
9+
"bg-emerald-500/30",
10+
"bg-emerald-500/50",
11+
"bg-emerald-500/75",
12+
"bg-emerald-500",
13+
];
14+
15+
function dayKeyOf(date: Date) {
16+
const month = `${date.getMonth() + 1}`.padStart(2, "0");
17+
const day = `${date.getDate()}`.padStart(2, "0");
18+
return `${date.getFullYear()}-${month}-${day}`;
19+
}
20+
21+
function levelFor(count: number) {
22+
if (count <= 0) return 0;
23+
if (count <= 2) return 1;
24+
if (count <= 5) return 2;
25+
if (count <= 9) return 3;
26+
return 4;
27+
}
28+
29+
/** Week columns (each 7 days, Sunday first) ending with the current week. */
30+
function buildWeeks(today: Date) {
31+
const start = new Date(
32+
today.getFullYear(),
33+
today.getMonth(),
34+
today.getDate() - today.getDay() - (WEEK_COUNT - 1) * DAYS_PER_WEEK,
35+
);
36+
37+
return Array.from({ length: WEEK_COUNT }, (_, weekIndex) =>
38+
Array.from({ length: DAYS_PER_WEEK }, (_, dayIndex) => {
39+
const date = new Date(start);
40+
date.setDate(start.getDate() + weekIndex * DAYS_PER_WEEK + dayIndex);
41+
return date;
42+
}),
43+
);
44+
}
45+
46+
function monthLabels(weeks: Date[][]) {
47+
return weeks.map((week, index) => {
48+
if (index === 0) return "";
49+
const month = week[0].getMonth();
50+
return month !== weeks[index - 1][0].getMonth()
51+
? week[0].toLocaleDateString(undefined, { month: "short" })
52+
: "";
53+
});
54+
}
55+
56+
/**
57+
* GitHub-style activity heatmap for the last 26 weeks, fed by per-day
58+
* activity counts (see `ProjectActivitySummary.activityByDay`).
59+
*/
60+
export function ProjectsContributionGraph({
61+
activityByDay,
62+
className,
63+
}: {
64+
activityByDay: Record<string, number>;
65+
className?: string;
66+
}) {
67+
const today = new Date();
68+
const weeks = buildWeeks(today);
69+
const labels = monthLabels(weeks);
70+
const gridTemplateColumns = `repeat(${weeks.length}, minmax(0, 1fr))`;
71+
const todayKey = dayKeyOf(today);
72+
73+
let totalContributions = 0;
74+
for (const week of weeks) {
75+
for (const day of week) {
76+
const key = dayKeyOf(day);
77+
if (key > todayKey) continue;
78+
totalContributions += activityByDay[key] ?? 0;
79+
}
80+
}
81+
82+
return (
83+
<div className={cn("space-y-2", className)}>
84+
<div className="grid gap-1" style={{ gridTemplateColumns }}>
85+
{labels.map((label, index) => (
86+
<span
87+
className="overflow-visible whitespace-nowrap text-2xs font-medium text-muted-foreground"
88+
// Columns are positional; labels have no stable content key.
89+
// biome-ignore lint/suspicious/noArrayIndexKey: fixed-size grid
90+
key={index}
91+
>
92+
{label}
93+
</span>
94+
))}
95+
</div>
96+
<div
97+
className="grid grid-flow-col grid-rows-7 gap-1"
98+
style={{ gridTemplateColumns }}
99+
>
100+
{weeks.map((week) =>
101+
week.map((day) => {
102+
const key = dayKeyOf(day);
103+
if (key > todayKey) {
104+
return <span aria-hidden className="aspect-square" key={key} />;
105+
}
106+
const count = activityByDay[key] ?? 0;
107+
const dateLabel = day.toLocaleDateString(undefined, {
108+
month: "short",
109+
day: "numeric",
110+
});
111+
return (
112+
<span
113+
className={cn(
114+
"aspect-square w-full rounded-[3px]",
115+
LEVEL_CLASSES[levelFor(count)],
116+
)}
117+
key={key}
118+
title={
119+
count > 0
120+
? `${count} ${count === 1 ? "event" : "events"} · ${dateLabel}`
121+
: `No activity · ${dateLabel}`
122+
}
123+
/>
124+
);
125+
}),
126+
)}
127+
</div>
128+
<div className="flex items-center justify-between gap-2 pt-1">
129+
<p className="text-xs text-muted-foreground">
130+
{totalContributions} {totalContributions === 1 ? "event" : "events"}{" "}
131+
in the last 6 months
132+
</p>
133+
<div className="flex items-center gap-1.5 text-2xs text-muted-foreground">
134+
Less
135+
{LEVEL_CLASSES.map((levelClass) => (
136+
<span
137+
className={cn("h-2.5 w-2.5 rounded-[3px]", levelClass)}
138+
key={levelClass}
139+
/>
140+
))}
141+
More
142+
</div>
143+
</div>
144+
</div>
145+
);
146+
}

‎desktop/src/features/projects/ui/ProjectsOverviewPanel.tsx‎

Lines changed: 88 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { normalizePubkey } from "@/shared/lib/pubkey";
1414
import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip";
1515
import { UserAvatar } from "@/shared/ui/UserAvatar";
1616
import { OverviewRailSection } from "./ProjectOverviewPanel";
17+
import { ProjectsContributionGraph } from "./ProjectsContributionGraph";
1718

1819
export type ProjectsOverviewSection =
1920
| "repositories"
@@ -30,8 +31,8 @@ type ProjectsOverviewPanelProps = {
3031
summaries?: Record<string, ProjectActivitySummary>;
3132
};
3233

33-
function pluralize(count: number, singular: string, plural = `${singular}s`) {
34-
return `${count} ${count === 1 ? singular : plural}`;
34+
function unitLabel(count: number, singular: string, plural = `${singular}s`) {
35+
return count === 1 ? singular : plural;
3536
}
3637

3738
function projectPeople(
@@ -78,34 +79,52 @@ function overviewStats(
7879
);
7980
}
8081

82+
function overviewActivityByDay(
83+
projects: Project[],
84+
summaries: Record<string, ProjectActivitySummary> | undefined,
85+
) {
86+
const merged: Record<string, number> = {};
87+
for (const project of projects) {
88+
const byDay = summaries?.[project.repoAddress]?.activityByDay;
89+
if (!byDay) continue;
90+
for (const [day, count] of Object.entries(byDay)) {
91+
merged[day] = (merged[day] ?? 0) + count;
92+
}
93+
}
94+
return merged;
95+
}
96+
8197
function StatPill({
98+
count,
8299
icon: Icon,
83100
label,
84101
onClick,
85-
value,
102+
unit,
86103
}: {
104+
count: number;
87105
icon: React.ComponentType<{ className?: string }>;
88106
label: string;
89107
onClick: () => void;
90-
value: string;
108+
unit: string;
91109
}) {
92110
return (
93111
<button
94-
className="flex items-center gap-3 rounded-xl bg-card px-3 py-2.5 text-left shadow-xs transition-colors hover:bg-muted/40"
112+
className="flex flex-col rounded-xl bg-card px-3.5 py-3 text-left shadow-xs transition-colors hover:bg-muted/40"
95113
onClick={onClick}
96114
type="button"
97115
>
98-
<span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-muted/60">
99-
<Icon className="h-4 w-4 text-muted-foreground" />
100-
</span>
101-
<div className="min-w-0">
102-
<span className="block text-xs font-medium text-foreground">
116+
<span className="flex w-full items-center justify-between gap-2">
117+
<span className="text-xs font-medium text-muted-foreground">
103118
{label}
104119
</span>
105-
<span className="mt-0.5 block truncate text-sm text-muted-foreground">
106-
{value}
120+
<Icon className="h-3.5 w-3.5 text-muted-foreground/70" />
121+
</span>
122+
<span className="mt-2 flex min-w-0 items-baseline gap-1.5">
123+
<span className="text-2xl font-semibold leading-none tracking-tight text-foreground">
124+
{count}
107125
</span>
108-
</div>
126+
<span className="truncate text-xs text-muted-foreground">{unit}</span>
127+
</span>
109128
</button>
110129
);
111130
}
@@ -120,46 +139,67 @@ export function ProjectsOverviewPanel({
120139
}: ProjectsOverviewPanelProps) {
121140
const stats = overviewStats(projects, summaries);
122141
const people = overviewPeople(projects, summaries);
142+
const activityByDay = overviewActivityByDay(projects, summaries);
123143

124144
return (
125145
<section className="mb-4 grid gap-4 xl:grid-cols-[minmax(0,1fr)_18rem]">
126-
<div className="overflow-hidden rounded-2xl border border-border/50 bg-muted/20 p-4">
127-
<div className="flex min-w-0 items-start gap-3">
128-
<WorkspaceEmojiIcon className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-muted/60 text-2xl" />
129-
<div className="-mt-1 min-w-0 flex-1 space-y-0.5">
130-
<h2 className="text-xl font-semibold leading-7 tracking-tight text-foreground">
131-
{relayName} Projects
132-
</h2>
133-
<p className="max-w-2xl text-sm font-normal text-muted-foreground">
134-
Browse shared repositories, pull requests, and local project
135-
checkouts in this workspace.
136-
</p>
146+
<div className="min-w-0 space-y-4">
147+
<div className="overflow-hidden rounded-2xl border border-border/50 bg-muted/20 p-4">
148+
<div className="flex min-w-0 items-start gap-3">
149+
<WorkspaceEmojiIcon className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-muted/60 text-2xl" />
150+
<div className="-mt-1 min-w-0 flex-1 space-y-0.5">
151+
<h2 className="text-xl font-semibold leading-7 tracking-tight text-foreground">
152+
{relayName} Projects
153+
</h2>
154+
<p className="max-w-2xl text-sm font-normal text-muted-foreground">
155+
Browse shared repositories, pull requests, and local project
156+
checkouts in this workspace.
157+
</p>
158+
</div>
159+
</div>
160+
<div className="mt-4 grid gap-2 sm:grid-cols-2 xl:grid-cols-4">
161+
<StatPill
162+
count={projects.length}
163+
icon={FolderGit2}
164+
label="Repositories"
165+
onClick={() => onSelectSection("repositories")}
166+
unit={unitLabel(projects.length, "project")}
167+
/>
168+
<StatPill
169+
count={stats.prs}
170+
icon={GitPullRequest}
171+
label="Pull requests"
172+
onClick={() => onSelectSection("prs")}
173+
unit={unitLabel(stats.prs, "PR")}
174+
/>
175+
<StatPill
176+
count={localRepositoryCount}
177+
icon={Radio}
178+
label="Local"
179+
onClick={() => onSelectSection("local")}
180+
unit={unitLabel(localRepositoryCount, "checkout")}
181+
/>
182+
<StatPill
183+
count={stats.issues}
184+
icon={CircleDot}
185+
label="Issues"
186+
onClick={() => onSelectSection("issues")}
187+
unit={unitLabel(stats.issues, "issue")}
188+
/>
137189
</div>
138190
</div>
139-
<div className="mt-4 grid gap-2 sm:grid-cols-2 xl:grid-cols-4">
140-
<StatPill
141-
icon={FolderGit2}
142-
label="Repositories"
143-
onClick={() => onSelectSection("repositories")}
144-
value={pluralize(projects.length, "project")}
145-
/>
146-
<StatPill
147-
icon={GitPullRequest}
148-
label="Pull requests"
149-
onClick={() => onSelectSection("prs")}
150-
value={pluralize(stats.prs, "PR")}
151-
/>
152-
<StatPill
153-
icon={Radio}
154-
label="Local"
155-
onClick={() => onSelectSection("local")}
156-
value={pluralize(localRepositoryCount, "checkout")}
157-
/>
158-
<StatPill
159-
icon={CircleDot}
160-
label="Issues"
161-
onClick={() => onSelectSection("issues")}
162-
value={pluralize(stats.issues, "issue")}
191+
<div className="overflow-hidden rounded-2xl border border-border/50 bg-muted/20 p-4">
192+
<div className="flex items-center justify-between gap-2">
193+
<h3 className="text-sm font-semibold text-foreground">
194+
Contribution activity
195+
</h3>
196+
<span className="text-xs text-muted-foreground">
197+
Commits, PRs & issues across all projects
198+
</span>
199+
</div>
200+
<ProjectsContributionGraph
201+
activityByDay={activityByDay}
202+
className="mt-3"
163203
/>
164204
</div>
165205
</div>

0 commit comments

Comments
 (0)