Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): derive analytics bucket size from the data on screen

Richie McIlroy78ca8d94c891
diff --git a/apps/web/__tests__/unit/analytics-chart-dates.test.ts b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
index e5eddc3a2..d679b250e 100644
--- a/apps/web/__tests__/unit/analytics-chart-dates.test.ts
+++ b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
@@ -3,6 +3,7 @@ import {
formatBucketTick,
formatBucketTitle,
formatRelative,
+ granularityOf,
} from "@/app/(org)/dashboard/analytics/components/format";
describe.each(["America/Los_Angeles", "Asia/Kolkata", "UTC"])(
@@ -61,3 +62,34 @@ describe("formatRelative", () => {
);
});
});
+
+describe("granularityOf", () => {
+ it("detects hourly buckets from their spacing", () => {
+ expect(
+ granularityOf([
+ { bucket: "2026-10-24T01:00:00Z" },
+ { bucket: "2026-10-24T02:00:00Z" },
+ ]),
+ ).toBe("hour");
+ });
+
+ it("treats daily or missing buckets as daily", () => {
+ expect(
+ granularityOf([
+ { bucket: "2026-10-23T00:00:00Z" },
+ { bucket: "2026-10-24T00:00:00Z" },
+ ]),
+ ).toBe("day");
+ expect(granularityOf([])).toBe("day");
+ });
+});
+
+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"),
+ ).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" })}`,
+ );
+ });
+});
diff --git a/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx b/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
index 219225ef8..b69de7949 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
+++ b/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
@@ -21,6 +21,7 @@ import { Fit, Layout, useRive } from "@/lib/rive";
import { useDashboardContext } from "../../Contexts";
import type { AnalyticsRange, OrgAnalyticsResponse } from "../types";
import { DoodleFilters } from "./Doodles";
+import { granularityOf } from "./format";
import Header from "./Header";
import OtherStats from "./OtherStats";
import StatsChart from "./StatsChart";
@@ -217,7 +218,7 @@ export function AnalyticsDashboard() {
data={analytics?.chart ?? []}
isLoading={query.isLoading}
capId={capId}
- granularity={range === "24h" ? "hour" : "day"}
+ granularity={granularityOf(analytics?.chart ?? [])}
animationKey={`${orgId}-${selectedSpaceId ?? ""}-${capId ?? ""}`}
emptyAction={
capId ? (
diff --git a/apps/web/app/(org)/dashboard/analytics/components/format.ts b/apps/web/app/(org)/dashboard/analytics/components/format.ts
index dc9193e56..f9eaa03ab 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/format.ts
+++ b/apps/web/app/(org)/dashboard/analytics/components/format.ts
@@ -1,5 +1,16 @@
export type Granularity = "hour" | "day" | "week";
+export const granularityOf = (
+ buckets: ReadonlyArray<{ bucket: string }>,
+): Granularity => {
+ const first = buckets[0];
+ const second = buckets[1];
+ if (!first || !second) return "day";
+ const gap =
+ new Date(second.bucket).getTime() - new Date(first.bucket).getTime();
+ return gap > 0 && gap <= 60 * 60 * 1000 ? "hour" : "day";
+};
+
export const formatCompact = (value: number) => {
if (value >= 1_000_000)
return `${(value / 1_000_000).toFixed(value >= 10_000_000 ? 0 : 1)}M`;