Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ Sources: [web app manifest](public/site.webmanifest), [service worker](public/sw

### Activity graphs and README embeds

Profiles display commit activity with current and longest streaks. The profile graph adapts to how long someone has been committing: the last 24 hours, 7 days, 30 days, or a year. Hovering a cell highlights that period's commits. Settings → Profile includes a live preview and copyable Markdown for embedding your /dev/null activity graph in a GitHub profile README. The graph is public at `/@{handle}/graph.svg`, uses your lane color, and supports `?theme=light`, `dark`, or `auto` (light by default). The SVG is cached for an hour, so a new commit may not appear in an external embed immediately.
Profiles display commit activity with current and longest streaks. The profile graph adapts to how long someone has been committing: the last 24 hours, 7 days, 30 days, 6 months, or a year. Hovering a cell highlights that period's commits. Settings → Profile includes a live preview and copyable Markdown for embedding your /dev/null activity graph in a GitHub profile README. The graph is public at `/@{handle}/graph.svg`, always shows the full year, uses your lane color, and supports `?theme=light`, `dark`, or `auto` (light by default). The SVG is cached for an hour, so a new commit may not appear in an external embed immediately.

Sources: [graph embed settings](resources/js/components/activity-graph-embed.tsx), [graph controller](app/Http/Controllers/UserActivityGraphController.php), [graph route and cache](routes/web.php).

Expand Down
19 changes: 14 additions & 5 deletions app/Enums/ActivityScale.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ enum ActivityScale: string
case Day = 'day';
case Week = 'week';
case Month = 'month';
case Half = 'half';
case Year = 'year';

/**
* The smallest scale whose window still contains everything since the given moment.
* The scales shorter than a year that a profile's activity can fit into, smallest first.
*/
public static function since(CarbonInterface $since, CarbonImmutable $now): self
public const array FITTING = [self::Day, self::Week, self::Month, self::Half];

/**
* The smallest of the given scales whose window still contains everything since the given moment, or else the year.
*
* @param list<self> $scales
*/
public static function since(CarbonInterface $since, CarbonImmutable $now, array $scales = self::FITTING): self
{
foreach ([self::Day, self::Week, self::Month] as $scale) {
foreach ($scales as $scale) {
if ($since->greaterThanOrEqualTo($scale->start($now))) {
return $scale;
}
Expand All @@ -34,21 +42,22 @@ public function start(CarbonImmutable $now): CarbonImmutable
self::Day => $now->startOfHour()->subHours(23),
self::Week => $now->startOfDay()->subDays(6),
self::Month => $now->startOfDay()->subDays(29),
self::Half => $now->subWeeks(25)->startOfWeek(CarbonInterface::SUNDAY),
self::Year => $now->subWeeks(52)->startOfWeek(CarbonInterface::SUNDAY),
};
}

/**
* The start of the window ending now, as the operations page has always counted: the last 24 hours, 7 days, or
* 30 days. The year keeps its whole weeks, since its grid needs them.
* 30 days. The week grids keep their whole weeks, since they need them.
*/
public function rollingStart(CarbonImmutable $now): CarbonImmutable
{
return match ($this) {
self::Day => $now->subDay(),
self::Week => $now->subWeek(),
self::Month => $now->subDays(30),
self::Year => $this->start($now),
self::Half, self::Year => $this->start($now),
};
}

Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Controllers\Api;

use App\Enums\ActivityScale;
use App\Http\Requests\ShowApiUserRequest;
use App\Http\Resources\CommitResource;
use App\Http\Resources\UserResource;
Expand Down Expand Up @@ -39,7 +40,8 @@ public function show(ShowApiUserRequest $request, #[CurrentUser] ?User $currentU
'follows_you' => $user->isFollowing($currentUser),
'is_muting' => $currentUser->isMuting($user),
'pasteis_received' => $user->pasteis_received,
'activity' => $activity->get($user, $currentUser),
// API clients don't know the half-year scale yet, so they keep getting the year for anyone past a month.
'activity' => $activity->get($user, $currentUser, scales: [ActivityScale::Day, ActivityScale::Week, ActivityScale::Month]),
'streaks' => $userStreaks,
'achievements' => $achievements->get($user, $currentUser, $userStreaks),
'pinned_commit' => $pinnedCommit instanceof Commit ? CommitResource::make($pinnedCommit) : null,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ShowSiteActivityRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function authorize(): Response
public function rules(): array
{
return [
'scale' => ['nullable', Rule::enum(ActivityScale::class)],
'scale' => ['nullable', Rule::enum(ActivityScale::class)->only([ActivityScale::Day, ActivityScale::Week, ActivityScale::Month, ActivityScale::Year])],
'metric' => ['nullable', Rule::enum(ActivityMetric::class)],
];
}
Expand Down
13 changes: 9 additions & 4 deletions app/Queries/UserActivityQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@
public function __construct(private ActivityBucketsQuery $buckets) {}

/**
* Without a scale, it is the smallest one that fits all of the visible activity.
* Without a scale, it is the smallest of the given ones that fits all of the visible activity, or else the year.
*
* @param list<ActivityScale> $scales
* @return array{scale: ActivityScale, buckets: list<ActivityBucket>}
*/
public function get(User $user, ?User $viewer, ?ActivityScale $scale = null): array
public function get(User $user, ?User $viewer, ?ActivityScale $scale = null, array $scales = ActivityScale::FITTING): array
{
$now = CarbonImmutable::now();

$scale ??= $this->scale($user, $viewer, $now);
$scale ??= $this->scale($user, $viewer, $now, $scales);

return [
'scale' => $scale,
'buckets' => $this->buckets->get($user->commits()->visibleTo($viewer)->toBase(), $scale, $scale->start($now), $now),
];
}

private function scale(User $user, ?User $viewer, CarbonImmutable $now): ActivityScale
/**
* @param list<ActivityScale> $scales
*/
private function scale(User $user, ?User $viewer, CarbonImmutable $now, array $scales): ActivityScale
{
$firstCommitAt = $user->commits()->visibleTo($viewer)->min('created_at');

return ActivityScale::since(
is_string($firstCommitAt) ? CarbonImmutable::parse($firstCommitAt) : $user->created_at,
$now,
$scales,
);
}
}
44 changes: 36 additions & 8 deletions resources/js/components/activity-graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ export const activityWindows: Record<ActivityScale, string> = {
day: 'in the last 24 hours',
week: 'in the last 7 days',
month: 'in the last 30 days',
half: 'in the last 6 months',
year: 'in the last year',
};

/**
* The scales drawn as a grid of weeks, one column per week, rather than a strip.
*/
export function isWeekGrid(scale: ActivityScale): boolean {
return scale === 'half' || scale === 'year';
}

function levelColor(level: number): string {
return level === 0
? 'var(--muted)'
Expand Down Expand Up @@ -85,7 +93,7 @@ export function activitySummary(
): string {
const total = activityTotal(activity);

return total === 0 && activity.scale !== 'year'
return total === 0 && !isWeekGrid(activity.scale)
? `No ${noun[1]} yet`
: `${count(total, noun)} ${activityWindows[activity.scale]}`;
}
Expand Down Expand Up @@ -178,7 +186,7 @@ export function ActivityGraph({
onPeriodHover?: (period: string | null) => void;
}) {
const total = activityTotal(activity);
const isEmpty = total === 0 && activity.scale !== 'year';
const isEmpty = total === 0 && !isWeekGrid(activity.scale);

return (
<section
Expand Down Expand Up @@ -278,8 +286,9 @@ export function ActivityCells({
data-activity-cells
style={{ '--lane': color } as CSSProperties}
>
{scale === 'year' ? (
<YearGrid
{isWeekGrid(scale) ? (
<WeekGrid
scale={scale}
buckets={buckets}
max={max}
summary={summary}
Expand Down Expand Up @@ -386,13 +395,20 @@ function Strip({
);
}

function YearGrid({
/**
* Shorter grids cap each week's column, so a few weeks don't stretch into oversized cells.
*/
const weekPitch = '1.5rem';

function WeekGrid({
scale,
buckets,
max,
summary,
onEnter,
onLeave,
}: CellsProps & { summary: string }) {
}: CellsProps & { scale: ActivityScale; summary: string }) {
const isYear = scale === 'year';
const weekStarts = buckets.filter((_, index) => index % 7 === 0);
const monthLabels = weekStarts.map((day, index) => {
const changesMonth =
Expand All @@ -404,7 +420,9 @@ function YearGrid({
.slice(1, 3)
.some((week) => monthOf(week.date) !== monthOf(day.date));

return changesMonth && !nextLabelIsClose
const isLastWeek = index === weekStarts.length - 1;

return changesMonth && !nextLabelIsClose && !isLastWeek
? monthNames[monthOf(day.date)]
: null;
});
Expand All @@ -425,7 +443,17 @@ function YearGrid({
<div
role="img"
aria-label={summary}
className="grid min-w-136 grid-cols-[auto_minmax(0,1fr)] gap-x-2 gap-y-1.5"
className={cn(
'grid grid-cols-[auto_minmax(0,1fr)] gap-x-2 gap-y-1.5',
isYear && 'min-w-136',
)}
style={
isYear
? undefined
: {
maxWidth: `calc(${weekStarts.length} * ${weekPitch} + 2.5rem)`,
}
}
>
<span aria-hidden />
<div
Expand Down
2 changes: 1 addition & 1 deletion resources/js/types/activity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ActivityScale = 'day' | 'week' | 'month' | 'year';
export type ActivityScale = 'day' | 'week' | 'month' | 'half' | 'year';

export type ActivityBucket = {
date: string;
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/Controllers/Api/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@
expect(json_decode($response->getContent(), flags: JSON_THROW_ON_ERROR)->bio_profiles)->toBeInstanceOf(stdClass::class);
});

it('keeps API clients on the year for a profile whose first visible commit is two months old', function (): void {
$this->travelTo(now()->setDate(2026, 10, 15)->setTime(12, 0));

$keyPair = apiKeyPairFor(User::factory()->create());
$user = User::factory()->create();

Commit::factory()->for($user)->create(['created_at' => '2026-08-15 09:00:00']);

signedApiRequest($keyPair, 'GET', route('api-user.show', $user))
->assertOk()
->assertJsonPath('activity.scale', 'year')
->assertJsonCount(369, 'activity.buckets')
->assertJsonPath('activity.buckets.0.date', '2025-10-12');
});

it('counts the pending commits of a signed-in user on their own profile', function (): void {
$user = User::factory()->create();
$keyPair = apiKeyPairFor($user);
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Controllers/SiteActivityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
->assertSessionHasErrors();
})->with([
'unknown scale' => [['scale' => 'decade']],
'half scale' => [['scale' => 'half']],
'unknown series' => [['metric' => 'stars']],
]);

Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/ActivityCells.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,61 @@ test('labels the cells with the summary and paints them in the given color', ()
assert.match(html, /aria-label="3 replies in the last 7 days"/);
assert.match(html, /--lane:var\(--muted-foreground\)/);
});

test('summarizes the half-year window, counting zero instead of saying none yet', () => {
assert.equal(
activitySummary(
{ scale: 'half', buckets: [{ date: '2026-10-15', count: 3 }] },
['commit', 'commits'],
),
'3 commits in the last 6 months',
);
assert.equal(
activitySummary({ scale: 'half', buckets: [] }, ['commit', 'commits']),
'0 commits in the last 6 months',
);
});

test('draws half and year as week grids with one column per week', () => {
for (const [scale, weeks] of [
['half', 26],
['year', 53],
] as const) {
const buckets = Array.from({ length: weeks * 7 }, (_, day) => ({
date: new Date(Date.UTC(2026, 0, 4 + day))
.toISOString()
.slice(0, 10),
count: day % 3,
}));
const html = renderToString(
<ActivityCells
activity={{ scale, buckets }}
color="red"
noun={['commit', 'commits']}
/>,
);

assert.match(
html,
new RegExp(`repeat\\(${weeks}, minmax\\(0, 1fr\\)\\)`),
);
assert.match(html, /grid-rows-7/);
}
});

test('leaves the last week unlabelled when it starts a month, so the label cannot spill past the grid', () => {
const buckets = Array.from({ length: 26 * 7 }, (_, day) => ({
date: new Date(Date.UTC(2026, 4, 10 + day)).toISOString().slice(0, 10),
count: 1,
}));
const html = renderToString(
<ActivityCells
activity={{ scale: 'half', buckets }}
color="red"
noun={['commit', 'commits']}
/>,
);

assert.match(html, /whitespace-nowrap">Oct</);
assert.doesNotMatch(html, /whitespace-nowrap">Nov</);
});
31 changes: 30 additions & 1 deletion tests/Unit/Queries/UserActivityQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
'start of the last 7 days' => ['2026-10-09 00:00:00', ActivityScale::Week],
'just before the last 7 days' => ['2026-10-08 23:59:59', ActivityScale::Month],
'start of the last 30 days' => ['2026-09-16 00:00:00', ActivityScale::Month],
'just before the last 30 days' => ['2026-09-15 23:59:59', ActivityScale::Year],
'just before the last 30 days' => ['2026-09-15 23:59:59', ActivityScale::Half],
'start of the last 26 weeks' => ['2026-04-19 00:00:00', ActivityScale::Half],
'just before the last 26 weeks' => ['2026-04-18 23:59:59', ActivityScale::Year],
'years ago' => ['2024-01-01 00:00:00', ActivityScale::Year],
]);

Expand All @@ -41,6 +43,8 @@
'just now' => ['2026-10-15 12:30:00', ActivityScale::Day],
'three days ago' => ['2026-10-12 09:00:00', ActivityScale::Week],
'two weeks ago' => ['2026-10-01 09:00:00', ActivityScale::Month],
'two months ago' => ['2026-08-15 09:00:00', ActivityScale::Half],
'four months ago' => ['2026-06-15 09:00:00', ActivityScale::Half],
'two years ago' => ['2024-10-15 09:00:00', ActivityScale::Year],
]);

Expand Down Expand Up @@ -85,6 +89,31 @@
]);
});

it('falls back to the year when the half year is not among the scales to choose from', function (): void {
$user = User::factory()->create();

Commit::factory()->for($user)->create(['created_at' => '2026-08-15 09:00:00']);

expect(resolve(UserActivityQuery::class)->get($user, $user, scales: [ActivityScale::Day, ActivityScale::Week, ActivityScale::Month]))
->scale->toBe(ActivityScale::Year)
->buckets->toHaveCount(369);
});

it('counts the commits of each day of the last 26 weeks', function (): void {
$user = User::factory()->create();

Commit::factory()->for($user)->create(['created_at' => '2026-06-15 09:00:00']);
Commit::factory()->for($user)->count(2)->create();

$activity = resolve(UserActivityQuery::class)->get($user, $user);

expect($activity['scale'])->toBe(ActivityScale::Half)
->and($activity['buckets'])->toHaveCount(180)
->and($activity['buckets'][0])->toBe(['date' => '2026-04-19', 'count' => 0])
->and($activity['buckets'][57])->toBe(['date' => '2026-06-15', 'count' => 1])
->and(end($activity['buckets']))->toBe(['date' => '2026-10-15', 'count' => 2]);
});

it('counts the commits of each day of the last 30 days', function (): void {
$user = User::factory()->create();

Expand Down