Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): show years on analytics dates outside the current year

Richie McIlroybcf09d41678d
diff --git a/apps/web/__tests__/unit/analytics-chart-dates.test.ts b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
index d679b250e..8a4b5dd4e 100644
--- a/apps/web/__tests__/unit/analytics-chart-dates.test.ts
+++ b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
@@ -87,9 +87,73 @@ describe("granularityOf", () => {
describe("weekly titles", () => {
it("uses the bucket's real end date when the week is short", () => {
expect(
- formatBucketTitle("2026-07-01T00:00:00Z", "week", "2026-07-03T00:00:00Z"),
+ formatBucketTitle(
+ "2026-07-01T00:00:00Z",
+ "week",
+ "2026-07-03T00:00:00Z",
+ Date.parse("2026-10-24T12:00:00Z"),
+ ),
).toBe(
`${new Date("2026-07-01T00:00:00Z").toLocaleDateString([], { day: "numeric", month: "short", timeZone: "UTC" })} to ${new Date("2026-07-03T00:00:00Z").toLocaleDateString([], { day: "numeric", month: "short", timeZone: "UTC" })}`,
);
});
});
+
+describe("dates from other years", () => {
+ const now = Date.parse("2026-10-24T12:00:00Z");
+ const withYear = (iso: string) =>
+ new Date(iso).toLocaleDateString([], {
+ day: "numeric",
+ month: "short",
+ year: "numeric",
+ timeZone: "UTC",
+ });
+
+ it("adds the year to day and week titles outside the current year", () => {
+ expect(
+ formatBucketTitle("2024-06-28T00:00:00Z", "day", undefined, now),
+ ).toContain("2024");
+ expect(
+ formatBucketTitle("2026-06-28T00:00:00Z", "day", undefined, now),
+ ).not.toContain("2026");
+ expect(
+ formatBucketTitle(
+ "2024-06-28T00:00:00Z",
+ "week",
+ "2024-07-04T00:00:00Z",
+ now,
+ ),
+ ).toBe(
+ `${withYear("2024-06-28T00:00:00Z")} to ${withYear("2024-07-04T00:00:00Z")}`,
+ );
+ expect(
+ formatBucketTitle(
+ "2025-12-29T00:00:00Z",
+ "week",
+ "2026-01-04T00:00:00Z",
+ now,
+ ),
+ ).toBe(
+ `${withYear("2025-12-29T00:00:00Z")} to ${withYear("2026-01-04T00:00:00Z")}`,
+ );
+ });
+
+ it("counts whole years for old buckets", () => {
+ expect(formatRelative("2024-03-08T00:00:00Z", "week", now)).toBe(
+ "2 years ago",
+ );
+ });
+
+ it("labels multi-year ticks with the full year", () => {
+ expect(formatBucketTick("2024-04-01T00:00:00Z", "week", 900)).toBe(
+ new Date("2024-04-01T00:00:00Z").toLocaleDateString([], {
+ month: "short",
+ year: "numeric",
+ timeZone: "UTC",
+ }),
+ );
+ expect(formatBucketTick("2024-04-01T00:00:00Z", "week", 900)).toContain(
+ "2024",
+ );
+ });
+});
diff --git a/apps/web/app/(org)/dashboard/analytics/components/format.ts b/apps/web/app/(org)/dashboard/analytics/components/format.ts
index f9eaa03ab..da91be7fa 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/format.ts
+++ b/apps/web/app/(org)/dashboard/analytics/components/format.ts
@@ -38,7 +38,7 @@ export const formatBucketTick = (
if (spanDays > 300) {
return date.toLocaleDateString([], {
month: "short",
- year: "2-digit",
+ year: "numeric",
timeZone: "UTC",
});
}
@@ -53,9 +53,11 @@ export const formatBucketTitle = (
bucket: string,
granularity: Granularity,
bucketEnd?: string,
+ now = Date.now(),
) => {
const date = new Date(bucket);
if (Number.isNaN(date.getTime())) return bucket;
+ const thisYear = new Date(now).getUTCFullYear();
if (granularity === "hour") {
const day = date.toLocaleDateString([], {
weekday: "short",
@@ -73,10 +75,13 @@ export const formatBucketTitle = (
const end = bucketEnd
? new Date(bucketEnd)
: new Date(date.getTime() + 6 * 24 * 60 * 60 * 1000);
+ const showYear =
+ date.getUTCFullYear() !== thisYear || end.getUTCFullYear() !== thisYear;
const short = (d: Date) =>
d.toLocaleDateString([], {
day: "numeric",
month: "short",
+ year: showYear ? "numeric" : undefined,
timeZone: "UTC",
});
return `${short(date)} to ${short(end)}`;
@@ -85,6 +90,7 @@ export const formatBucketTitle = (
weekday: "long",
day: "numeric",
month: "long",
+ year: date.getUTCFullYear() !== thisYear ? "numeric" : undefined,
timeZone: "UTC",
});
};
@@ -120,7 +126,7 @@ export const formatRelative = (
if (days < 60) return `${days} days ago`;
const months = Math.round(days / 30.4);
if (months < 24) return `${months} months ago`;
- return `${Math.round(days / 365)} years ago`;
+ return `${Math.floor(days / 365)} years ago`;
};
const regionNames =