Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve(web): add analytics formatters and hand-drawn doodles

Richie McIlroy95c3a08cdbef
diff --git a/apps/web/__tests__/unit/analytics-chart-dates.test.ts b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
index a7eefb2b0..e5eddc3a2 100644
--- a/apps/web/__tests__/unit/analytics-chart-dates.test.ts
+++ b/apps/web/__tests__/unit/analytics-chart-dates.test.ts
@@ -1,81 +1,63 @@
-import { createElement, type ReactNode } from "react";
-import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import ChartArea from "@/app/(org)/dashboard/analytics/components/ChartArea";
-
-vi.mock("recharts", () => ({
- AreaChart: ({ data }: { data: Array<{ label: string; views: number }> }) =>
- createElement(
- "div",
- null,
- data.map((point) =>
- createElement(
- "span",
- { key: point.label },
- `${point.label}: ${point.views}`,
- ),
- ),
- ),
- Area: () => null,
- CartesianGrid: () => null,
- XAxis: () => null,
- YAxis: () => null,
-}));
-
-vi.mock("@/components/ui/chart", () => ({
- ChartContainer: ({ children }: { children: ReactNode }) => children,
- ChartTooltip: () => null,
- ChartTooltipContent: () => null,
-}));
-
-const renderChart = (buckets: string[]) =>
- renderToStaticMarkup(
- createElement(ChartArea, {
- selectedMetrics: ["views"],
- data: buckets.map((bucket, index) => ({
- bucket,
- views: index + 1,
- caps: 0,
- comments: 0,
- reactions: 0,
- })),
- }),
- );
+import {
+ formatBucketTick,
+ formatBucketTitle,
+ formatRelative,
+} from "@/app/(org)/dashboard/analytics/components/format";
describe.each(["America/Los_Angeles", "Asia/Kolkata", "UTC"])(
"analytics chart dates in %s",
(timeZone) => {
beforeEach(() => vi.stubEnv("TZ", timeZone));
afterEach(() => vi.unstubAllEnvs());
+
it.each([
- ["2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"],
- ["2026-03-08T00:00:00Z", "2026-03-09T00:00:00Z"],
- ["2026-11-01T00:00:00Z", "2026-11-02T00:00:00Z"],
- ])("labels UTC daily buckets on their actual date: %s", (...buckets) => {
- const chart = renderChart(buckets);
- for (const [index, bucket] of buckets.entries()) {
- const label = new Date(bucket).toLocaleDateString([], {
- month: "short",
+ "2026-01-01T00:00:00Z",
+ "2026-03-08T00:00:00Z",
+ "2026-11-01T00:00:00Z",
+ ])("labels UTC daily buckets on their actual date: %s", (bucket) => {
+ const expected = new Date(bucket).toLocaleDateString([], {
+ day: "numeric",
+ month: "short",
+ timeZone: "UTC",
+ });
+ expect(formatBucketTick(bucket, "day", 7)).toBe(expected);
+ expect(formatBucketTitle(bucket, "day")).toContain(
+ new Date(bucket).toLocaleDateString([], {
day: "numeric",
timeZone: "UTC",
- });
- expect(chart).toContain(`${label}: ${index + 1}`);
- }
+ }),
+ );
});
it("keeps hourly labels in the viewer's local time", () => {
- const buckets = ["2026-01-01T00:00:00Z", "2026-01-01T01:00:00Z"];
- const chart = renderChart(buckets);
- for (const [index, bucket] of buckets.entries()) {
- const label = new Date(bucket).toLocaleTimeString([], {
- hour: "numeric",
- });
- expect(chart).toContain(`${label}: ${index + 1}`);
- }
- });
-
- it("shows the empty state when there are no buckets", () => {
- expect(renderChart([])).toContain("No analytics data yet.");
+ const bucket = "2026-01-01T01:00:00Z";
+ expect(formatBucketTick(bucket, "hour", 1)).toBe(
+ new Date(bucket).toLocaleTimeString([], { hour: "numeric" }),
+ );
});
},
);
+
+describe("formatRelative", () => {
+ const now = Date.UTC(2026, 9, 24, 15, 30);
+
+ it("describes daily buckets against the UTC day", () => {
+ expect(formatRelative("2026-10-24T00:00:00Z", "day", now)).toBe("Today");
+ expect(formatRelative("2026-10-23T00:00:00Z", "day", now)).toBe(
+ "Yesterday",
+ );
+ expect(formatRelative("2026-10-13T00:00:00Z", "day", now)).toBe(
+ "11 days ago",
+ );
+ });
+
+ it("describes hourly buckets in hours", () => {
+ expect(formatRelative("2026-10-24T15:00:00Z", "hour", now)).toBe(
+ "This hour",
+ );
+ expect(formatRelative("2026-10-24T12:00:00Z", "hour", now)).toBe(
+ "3 hours ago",
+ );
+ });
+});
diff --git a/apps/web/app/(org)/dashboard/analytics/components/Doodles.tsx b/apps/web/app/(org)/dashboard/analytics/components/Doodles.tsx
new file mode 100644
index 000000000..3eec096bc
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/analytics/components/Doodles.tsx
@@ -0,0 +1,411 @@
+import type { CSSProperties, ReactNode } from "react";
+import styles from "./doodles.module.css";
+
+export const BOIL_FILTER_ID = "analytics-doodle-boil";
+export const BOIL_FILTER_LARGE_ID = "analytics-doodle-boil-lg";
+
+export const doodleStyles = styles;
+
+export const timing = (delay: number, duration?: number) =>
+ ({
+ "--d": `${delay}s`,
+ ...(duration === undefined ? {} : { "--t": `${duration}s` }),
+ }) as CSSProperties;
+
+export const cx = (...names: (string | false | null | undefined)[]) =>
+ names.filter(Boolean).join(" ");
+
+export function DoodleFilters() {
+ return (
+ <svg
+ aria-hidden="true"
+ width="0"
+ height="0"
+ style={{ position: "absolute", width: 0, height: 0 }}
+ >
+ <defs>
+ <filter
+ id={BOIL_FILTER_ID}
+ x="-20%"
+ y="-20%"
+ width="140%"
+ height="140%"
+ >
+ <feTurbulence
+ type="fractalNoise"
+ baseFrequency="0.09"
+ numOctaves="2"
+ seed="1"
+ result="noise"
+ >
+ <animate
+ attributeName="seed"
+ values="1;3;5;7"
+ dur="0.6s"
+ repeatCount="indefinite"
+ calcMode="discrete"
+ />
+ </feTurbulence>
+ <feDisplacementMap
+ in="SourceGraphic"
+ in2="noise"
+ scale="1.6"
+ xChannelSelector="R"
+ yChannelSelector="G"
+ />
+ </filter>
+ <filter
+ id={BOIL_FILTER_LARGE_ID}
+ x="-20%"
+ y="-20%"
+ width="140%"
+ height="140%"
+ >
+ <feTurbulence
+ type="fractalNoise"
+ baseFrequency="0.05"
+ numOctaves="2"
+ seed="2"
+ result="noise"
+ >
+ <animate
+ attributeName="seed"
+ values="2;4;6;8"
+ dur="0.6s"
+ repeatCount="indefinite"
+ calcMode="discrete"
+ />
+ </feTurbulence>
+ <feDisplacementMap
+ in="SourceGraphic"
+ in2="noise"
+ scale="2.6"
+ xChannelSelector="R"
+ yChannelSelector="G"
+ />
+ </filter>
+ </defs>
+ </svg>
+ );
+}
+
+function IconFrame({
+ children,
+ size = 26,
+ className,
+}: {
+ children: ReactNode;
+ size?: number;
+ className?: string;
+}) {
+ return (
+ <svg
+ className={className}
+ width={size}
+ height={size}
+ viewBox="0 0 48 48"
+ aria-hidden="true"
+ overflow="visible"
+ >
+ <g className={styles.boil} filter={`url(#${BOIL_FILTER_ID})`}>
+ {children}
+ </g>
+ </svg>
+ );
+}
+
+const W = 3.4;
+
+export function EyeDoodle({ size }: { size?: number }) {
+ return (
+ <IconFrame size={size}>
+ <g className={styles.blink}>
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.7)}
+ strokeWidth={W}
+ pathLength={1}
+ d="M 5 25 Q 24 6 43 25 Q 24 42 5 25 Z"
+ />
+ <circle
+ className={cx(styles.accentFill, styles.pop)}
+ style={timing(0.55)}
+ cx="24"
+ cy="24.5"
+ r="6.5"
+ />
+ <circle
+ className={cx(styles.paperFill, styles.pop)}
+ style={timing(0.7)}
+ cx="26.4"
+ cy="22.4"
+ r="1.8"
+ />
+ </g>
+ </IconFrame>
+ );
+}
+
+export function BubbleDoodle({ size }: { size?: number }) {
+ return (
+ <IconFrame size={size}>
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.8)}
+ strokeWidth={W}
+ pathLength={1}
+ d="M 12 9 L 37 9 Q 43 9 43 15 L 43 29 Q 43 35 37 35 L 21 35 L 12 42 L 13 35 Q 6 35 6 29 L 6 15 Q 6 9 12 9 Z"
+ />
+ {[15.5, 24.5, 33.5].map((dotX, i) => (
+ <g key={dotX} className={styles.typing} style={timing(i * 0.12)}>
+ <circle
+ className={cx(styles.accentFill, styles.pop)}
+ style={timing(0.6 + i * 0.12)}
+ cx={dotX}
+ cy="22"
+ r="2.6"
+ />
+ </g>
+ ))}
+ </IconFrame>
+ );
+}
+
+export function SmileDoodle({ size }: { size?: number }) {
+ return (
+ <IconFrame size={size}>
+ <g className={styles.wiggle}>
+ <circle
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.75)}
+ strokeWidth={W}
+ pathLength={1}
+ cx="24"
+ cy="24"
+ r="17.5"
+ />
+ <circle
+ className={cx(styles.inkFill, styles.pop)}
+ style={timing(0.55)}
+ cx="18"
+ cy="20"
+ r="2.4"
+ />
+ <circle
+ className={cx(styles.inkFill, styles.pop)}
+ style={timing(0.65)}
+ cx="30"
+ cy="20"
+ r="2.4"
+ />
+ <path
+ className={cx(styles.accent, styles.draw)}
+ style={timing(0.75, 0.35)}
+ strokeWidth={W}
+ pathLength={1}
+ d="M 16.5 28 Q 24 35.5 31.5 28"
+ />
+ </g>
+ </IconFrame>
+ );
+}
+
+export function ReelDoodle({ size }: { size?: number }) {
+ return (
+ <IconFrame size={size}>
+ <g className={styles.bounce}>
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.8)}
+ strokeWidth={W}
+ pathLength={1}
+ d="M 11 11 L 37 11 Q 42 11 42 16 L 42 32 Q 42 37 37 37 L 11 37 Q 6 37 6 32 L 6 16 Q 6 11 11 11 Z"
+ />
+ <circle
+ className={cx(styles.accentFill, styles.pop)}
+ style={timing(0.6)}
+ cx="24"
+ cy="24"
+ r="6"
+ />
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.75, 0.25)}
+ strokeWidth={W}
+ pathLength={1}
+ d="M 17 43 L 31 43"
+ />
+ </g>
+ </IconFrame>
+ );
+}
+
+function StageFrame({ children }: { children: ReactNode }) {
+ return (
+ <svg
+ width="132"
+ height="104"
+ viewBox="0 0 132 104"
+ aria-hidden="true"
+ overflow="visible"
+ >
+ <g className={styles.boil} filter={`url(#${BOIL_FILTER_LARGE_ID})`}>
+ {children}
+ </g>
+ </svg>
+ );
+}
+
+export function PaperPlaneDoodle() {
+ return (
+ <StageFrame>
+ <path
+ className={cx(styles.soft, styles.march)}
+ strokeWidth={2.4}
+ d="M 8 86 Q 26 92 42 78 Q 56 66 70 64"
+ />
+ <g className={styles.float}>
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.1, 0.8)}
+ strokeWidth={4.2}
+ pathLength={1}
+ d="M 72 60 L 122 18 L 104 74 L 92 58 Z"
+ />
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.75, 0.35)}
+ strokeWidth={4.2}
+ pathLength={1}
+ d="M 92 58 L 122 18"
+ />
+ <path
+ className={cx(styles.accent, styles.draw)}
+ style={timing(1, 0.3)}
+ strokeWidth={4.2}
+ pathLength={1}
+ d="M 92 58 L 90 72 L 98 66"
+ />
+ </g>
+ <path
+ className={cx(styles.accent, styles.pop)}
+ style={timing(1.3)}
+ strokeWidth={3.2}
+ d="M 30 22 L 30 32 M 25 27 L 35 27"
+ />
+ <path
+ className={cx(styles.accent, styles.pop)}
+ style={timing(1.45)}
+ strokeWidth={3.2}
+ d="M 54 10 L 58 6"
+ />
+ </StageFrame>
+ );
+}
+
+export function SquiggleLoader() {
+ return (
+ <svg
+ width="160"
+ height="56"
+ viewBox="0 0 160 56"
+ aria-hidden="true"
+ overflow="visible"
+ >
+ <g className={styles.boil} filter={`url(#${BOIL_FILTER_LARGE_ID})`}>
+ <path
+ className={cx(styles.soft, styles.scribble)}
+ strokeWidth={3.4}
+ pathLength={1}
+ d="M 6 44 Q 22 40 32 30 Q 42 20 54 32 Q 66 44 80 26 Q 92 10 106 22 Q 118 32 128 16 Q 136 6 154 10"
+ />
+ </g>
+ </svg>
+ );
+}
+
+export function GlobeDoodle() {
+ return (
+ <IconFrame size={56}>
+ <circle
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.8)}
+ strokeWidth={2.6}
+ pathLength={1}
+ cx="24"
+ cy="24"
+ r="17"
+ />
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.6, 0.5)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 24 7 Q 14 24 24 41 M 24 7 Q 34 24 24 41"
+ />
+ <path
+ className={cx(styles.accent, styles.draw)}
+ style={timing(0.9, 0.4)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 8 19 L 40 19 M 8 29 L 40 29"
+ />
+ </IconFrame>
+ );
+}
+
+export function LaptopDoodle() {
+ return (
+ <IconFrame size={56}>
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.7)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 11 12 L 37 12 Q 39 12 39 14 L 39 31 L 9 31 L 9 14 Q 9 12 11 12 Z"
+ />
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.6, 0.35)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 4 35 L 44 35 Q 42 39 38 39 L 10 39 Q 6 39 4 35 Z"
+ />
+ <path
+ className={cx(styles.accent, styles.draw)}
+ style={timing(0.9, 0.3)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 16 25 L 21 20 L 26 23 L 32 17"
+ />
+ </IconFrame>
+ );
+}
+
+export function StackDoodle() {
+ return (
+ <IconFrame size={56}>
+ <path
+ className={cx(styles.ink, styles.draw)}
+ style={timing(0.05, 0.7)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 10 16 L 38 16 Q 41 16 41 19 L 41 37 Q 41 40 38 40 L 10 40 Q 7 40 7 37 L 7 19 Q 7 16 10 16 Z"
+ />
+ <path
+ className={cx(styles.soft, styles.draw)}
+ style={timing(0.55, 0.35)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 11 11 L 37 11 M 15 6.5 L 33 6.5"
+ />
+ <path
+ className={cx(styles.accent, styles.draw)}
+ style={timing(0.85, 0.35)}
+ strokeWidth={2.6}
+ pathLength={1}
+ d="M 21 23 L 29 28 L 21 33 Z"
+ />
+ </IconFrame>
+ );
+}
diff --git a/apps/web/app/(org)/dashboard/analytics/components/doodles.module.css b/apps/web/app/(org)/dashboard/analytics/components/doodles.module.css
new file mode 100644
index 000000000..ede6727d7
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/analytics/components/doodles.module.css
@@ -0,0 +1,238 @@
+.ink {
+ fill: none;
+ stroke: var(--gray-12);
+ stroke-linecap: round;
+ stroke-linejoin: round;
+}
+
+.soft {
+ fill: none;
+ stroke: var(--gray-9);
+ stroke-linecap: round;
+ stroke-linejoin: round;
+}
+
+.accent {
+ fill: none;
+ stroke: var(--doodle-accent, #4785ff);
+ stroke-linecap: round;
+ stroke-linejoin: round;
+}
+
+.accentFill {
+ fill: var(--doodle-accent, #4785ff);
+}
+
+.inkFill {
+ fill: var(--gray-12);
+}
+
+.paperFill {
+ fill: var(--gray-1);
+}
+
+.draw {
+ stroke-dasharray: 1;
+ stroke-dashoffset: 1;
+ animation-name: draw;
+ animation-duration: var(--t, 0.7s);
+ animation-timing-function: ease;
+ animation-delay: var(--d, 0s);
+ animation-fill-mode: forwards;
+}
+
+.appear {
+ opacity: 0;
+ animation-name: fade-in;
+ animation-duration: 0.35s;
+ animation-timing-function: ease;
+ animation-delay: var(--d, 0s);
+ animation-fill-mode: forwards;
+}
+
+.pop {
+ opacity: 0;
+ transform-box: fill-box;
+ transform-origin: center;
+ animation-name: pop;
+ animation-duration: 0.5s;
+ animation-timing-function: ease;
+ animation-delay: var(--d, 0s);
+ animation-fill-mode: forwards;
+}
+
+.march {
+ stroke-dasharray: 2 5;
+ animation: march 1.1s linear infinite;
+}
+
+.float {
+ animation: float 3.2s ease-in-out 1.2s infinite;
+}
+
+.scribble {
+ stroke-dasharray: 1;
+ stroke-dashoffset: 1;
+ animation: scribble 2.4s ease-in-out infinite;
+}
+
+/* Hover behaviours: the parent sets data-doodle-hover when hovered. */
+
+[data-doodle-hover] .blink {
+ transform-box: fill-box;
+ transform-origin: center;
+ animation: blink 0.45s ease;
+}
+
+[data-doodle-hover] .wiggle {
+ transform-box: fill-box;
+ transform-origin: center;
+ animation: wiggle 0.6s ease;
+}
+
+[data-doodle-hover] .bounce {
+ transform-box: fill-box;
+ transform-origin: center bottom;
+ animation: bounce 0.6s ease;
+}
+
+[data-doodle-hover] .typing {
+ transform-box: fill-box;
+ transform-origin: center;
+ animation-name: typing;
+ animation-duration: 0.9s;
+ animation-timing-function: ease;
+ animation-delay: var(--d, 0s);
+}
+
+@keyframes draw {
+ to {
+ stroke-dashoffset: 0;
+ }
+}
+
+@keyframes fade-in {
+ to {
+ opacity: 1;
+ }
+}
+
+@keyframes pop {
+ from {
+ opacity: 0;
+ transform: scale(0.2);
+ }
+ 60% {
+ opacity: 1;
+ transform: scale(1.15);
+ }
+ to {
+ opacity: 1;
+ transform: scale(1);
+ }
+}
+
+@keyframes march {
+ to {
+ stroke-dashoffset: -7;
+ }
+}
+
+@keyframes float {
+ 0%,
+ 100% {
+ transform: translate(0, 0) rotate(0);
+ }
+ 50% {
+ transform: translate(3px, -4px) rotate(-2deg);
+ }
+}
+
+@keyframes scribble {
+ 0% {
+ stroke-dashoffset: 1;
+ opacity: 1;
+ }
+ 55% {
+ stroke-dashoffset: 0;
+ opacity: 1;
+ }
+ 80% {
+ stroke-dashoffset: 0;
+ opacity: 0;
+ }
+ 100% {
+ stroke-dashoffset: 1;
+ opacity: 0;
+ }
+}
+
+@keyframes blink {
+ 0%,
+ 100% {
+ transform: scaleY(1);
+ }
+ 45% {
+ transform: scaleY(0.1);
+ }
+}
+
+@keyframes wiggle {
+ 0%,
+ 100% {
+ transform: rotate(0);
+ }
+ 25% {
+ transform: rotate(-10deg);
+ }
+ 60% {
+ transform: rotate(8deg);
+ }
+}
+
+@keyframes bounce {
+ 0%,
+ 100% {
+ transform: translateY(0);
+ }
+ 40% {
+ transform: translateY(-3px);
+ }
+}
+
+@keyframes typing {
+ 0%,
+ 100% {
+ transform: translateY(0);
+ }
+ 40% {
+ transform: translateY(-2.5px);
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .boil {
+ filter: none;
+ }
+
+ .draw,
+ .appear,
+ .pop {
+ animation-duration: 0.01ms;
+ animation-delay: 0s;
+ }
+
+ .march,
+ .float,
+ .scribble,
+ [data-doodle-hover] .blink,
+ [data-doodle-hover] .wiggle,
+ [data-doodle-hover] .bounce,
+ [data-doodle-hover] .typing {
+ animation: none;
+ }
+
+ .scribble {
+ stroke-dashoffset: 0;
+ }
+}
diff --git a/apps/web/app/(org)/dashboard/analytics/components/format.ts b/apps/web/app/(org)/dashboard/analytics/components/format.ts
new file mode 100644
index 000000000..28a5a384a
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/analytics/components/format.ts
@@ -0,0 +1,122 @@
+export type Granularity = "hour" | "day" | "week";
+
+export const formatCompact = (value: number) => {
+ if (value >= 1_000_000)
+ return `${(value / 1_000_000).toFixed(value >= 10_000_000 ? 0 : 1)}M`;
+ if (value >= 10_000) return `${Math.round(value / 1_000)}k`;
+ if (value >= 1_000) return `${(value / 1_000).toFixed(1)}k`;
+ return value.toLocaleString();
+};
+
+export const formatPercent = (fraction: number) => {
+ const pct = fraction * 100;
+ if (pct > 0 && pct < 1) return "<1%";
+ return `${Math.round(pct)}%`;
+};
+
+export const formatBucketTick = (
+ bucket: string,
+ granularity: Granularity,
+ spanDays: number,
+) => {
+ const date = new Date(bucket);
+ if (Number.isNaN(date.getTime())) return bucket;
+ if (granularity === "hour") {
+ return date.toLocaleTimeString([], { hour: "numeric" });
+ }
+ if (spanDays > 300) {
+ return date.toLocaleDateString([], {
+ month: "short",
+ year: "2-digit",
+ timeZone: "UTC",
+ });
+ }
+ return date.toLocaleDateString([], {
+ day: "numeric",
+ month: "short",
+ timeZone: "UTC",
+ });
+};
+
+export const formatBucketTitle = (bucket: string, granularity: Granularity) => {
+ const date = new Date(bucket);
+ if (Number.isNaN(date.getTime())) return bucket;
+ if (granularity === "hour") {
+ const day = date.toLocaleDateString([], {
+ weekday: "short",
+ day: "numeric",
+ month: "short",
+ });
+ const start = date.toLocaleTimeString([], { hour: "numeric" });
+ const end = new Date(date.getTime() + 60 * 60 * 1000).toLocaleTimeString(
+ [],
+ { hour: "numeric" },
+ );
+ return `${day}, ${start} to ${end}`;
+ }
+ if (granularity === "week") {
+ const end = new Date(date.getTime() + 6 * 24 * 60 * 60 * 1000);
+ const short = (d: Date) =>
+ d.toLocaleDateString([], {
+ day: "numeric",
+ month: "short",
+ timeZone: "UTC",
+ });
+ return `${short(date)} to ${short(end)}`;
+ }
+ return date.toLocaleDateString([], {
+ weekday: "long",
+ day: "numeric",
+ month: "long",
+ timeZone: "UTC",
+ });
+};
+
+export const formatRelative = (
+ bucket: string,
+ granularity: Granularity,
+ now = Date.now(),
+) => {
+ const date = new Date(bucket);
+ if (Number.isNaN(date.getTime())) return "";
+ if (granularity === "hour") {
+ const hours = Math.floor((now - date.getTime()) / (60 * 60 * 1000));
+ if (hours <= 0) return "This hour";
+ if (hours === 1) return "1 hour ago";
+ return `${hours} hours ago`;
+ }
+ const today = new Date(now);
+ const todayUtc = Date.UTC(
+ today.getUTCFullYear(),
+ today.getUTCMonth(),
+ today.getUTCDate(),
+ );
+ const days = Math.round((todayUtc - date.getTime()) / (24 * 60 * 60 * 1000));
+ if (granularity === "week") {
+ const weeks = Math.floor(days / 7);
+ if (weeks <= 0) return "This week";
+ if (weeks === 1) return "Last week";
+ if (weeks < 9) return `${weeks} weeks ago`;
+ }
+ if (days <= 0) return "Today";
+ if (days === 1) return "Yesterday";
+ 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`;
+};
+
+const regionNames =
+ typeof Intl !== "undefined" && "DisplayNames" in Intl
+ ? new Intl.DisplayNames(["en"], { type: "region" })
+ : null;
+
+export const countryName = (code: string) => {
+ const trimmed = code.trim();
+ if (!/^[A-Za-z]{2}$/.test(trimmed)) return trimmed;
+ try {
+ return regionNames?.of(trimmed.toUpperCase()) ?? trimmed;
+ } catch {
+ return trimmed;
+ }
+};