Skip to content
TypeScript

feat: make Analytics responsive

Vipul Kumarc190b212f3e4
diff --git a/src/components/pages/analytics/analytics-page.tsx b/src/components/pages/analytics/analytics-page.tsx
index 9d2bec9..dcfc34f 100644
--- a/src/components/pages/analytics/analytics-page.tsx
+++ b/src/components/pages/analytics/analytics-page.tsx
@@ -20,7 +20,7 @@ import type { Invoice, InvoiceStatus, LeaveType } from "@/lib/types";
import { cn } from "@/lib/utils";
import { useToday } from "@/hooks/use-today";
import { useAppStore } from "@/stores/app-store";
-import { PageHeader, PageRoot, StatIcon, type StatGlyph } from "@/components/pages/shared/page-chrome";
+import { PageHeader, PageRoot, StatGrid, StatTile } from "@/components/pages/shared/page-chrome";
import { ChartCard, ColumnChart, DataTable, SERIES, TipRow, type Column, type Series } from "./charts";
const fmtDays = (v: number) => `${formatDays(Math.round(v * 100) / 100)}d`;
@@ -43,9 +43,9 @@ export function AnalyticsPage() {
title="Overview"
description={`January 1 – ${MONTHS[fromKey(today).getMonth()]} ${fromKey(today).getDate()}, ${year} · leave, time and revenue at a glance`}
/>
- <div className="grid gap-4 p-4">
+ <div className="grid gap-3 p-4 md:gap-4">
<KpiRow />
- <div className="grid grid-cols-1 gap-4 xl:grid-cols-2">
+ <div className="grid grid-cols-1 gap-3 md:gap-4 xl:grid-cols-2">
<LeavePerMonthCard />
<HoursCard />
<LeaveByTypeCard />
@@ -58,19 +58,6 @@ export function AnalyticsPage() {
/* ------------------------------------------------------------------ KPIs */
-function Kpi({ icon, label, value, hint }: { icon: StatGlyph; label: string; value: React.ReactNode; hint: React.ReactNode }) {
- return (
- <div className="flex min-w-0 flex-col gap-3 rounded-xl border border-hairline bg-surface p-4">
- <span className="flex items-center gap-2">
- <StatIcon name={icon} size={16} />
- <span className="t-14 truncate text-fg-2">{label}</span>
- </span>
- <span className="text-[28px] leading-none font-medium tracking-[-0.84px] text-fg">{value}</span>
- <span className="t-12 truncate text-fg-2">{hint}</span>
- </div>
- );
-}
-
function KpiRow() {
const today = useToday();
const requests = useAppStore((s) => s.requests);
@@ -103,32 +90,32 @@ function KpiRow() {
const { stats } = kpis;
return (
- <div className="grid grid-cols-2 gap-4 xl:grid-cols-4">
- <Kpi
+ <StatGrid>
+ <StatTile
icon="holidays"
label="Days off approved"
value={formatDays(stats.taken + stats.upcoming)}
hint={`${formatDays(stats.taken)} taken · ${formatDays(stats.upcoming)} booked ahead`}
/>
- <Kpi
+ <StatTile
icon="available"
label="Approval rate"
value={kpis.decided ? `${Math.round((kpis.approved / kpis.decided) * 100)}%` : "—"}
hint={`${plural(kpis.thisYear.length, "request", "requests")} · ${stats.pendingCount} pending`}
/>
- <Kpi
+ <StatTile
icon="per-year"
label="Hours, last 30 days"
value={fmtHours(kpis.minutes)}
hint={kpis.minutes ? `${Math.round((kpis.billable / kpis.minutes) * 100)}% billable` : "No time tracked"}
/>
- <Kpi
+ <StatTile
icon="upcoming"
label="Revenue collected"
value={money(kpis.paid, kpis.currency)}
hint={`${money(kpis.outstanding, kpis.currency)} outstanding`}
/>
- </div>
+ </StatGrid>
);
}
@@ -171,7 +158,7 @@ function LeavePerMonthCard() {
<ChartCard
title="Leave taken per month"
description={`${plural(Math.round(total * 100) / 100, "approved day", "approved days")} in ${year}`}
- chart={<ColumnChart columns={columns} series={series} format={fmtDays} formatTick={(v) => formatDays(v)} />}
+ chart={<ColumnChart columns={columns} series={series} format={fmtDays} formatTick={(v) => formatDays(v)} compactLabelEvery={2} />}
table={
<DataTable
head={["Month", "Approved days"]}
@@ -210,6 +197,7 @@ function HoursCard() {
key: date,
label: String(d.getDate()),
title: `${WEEKDAYS[d.getDay()]}, ${formatShort(date)}`,
+ short: formatShort(date),
values: sum(date, date),
emphasis: date === today,
};
@@ -223,6 +211,7 @@ function HoursCard() {
key: from,
label: formatShort(from),
title: `Week of ${formatShort(from)}`,
+ short: formatShort(from),
values: sum(from, to),
emphasis: from === current,
};
@@ -243,7 +232,7 @@ function HoursCard() {
description={`${fmtHours(billable + other)} in the last ${mode === "day" ? "14 days" : "12 weeks"} · ${fmtHours(billable)} billable`}
legend={series}
actions={
- <div className="mr-1 flex items-center rounded-lg bg-app p-0.5" role="group" aria-label="Group by">
+ <div className="flex items-center rounded-lg bg-app p-0.5 md:mr-1" role="group" aria-label="Group by">
{(["day", "week"] as const).map((m) => (
<button
key={m}
@@ -251,7 +240,7 @@ function HoursCard() {
aria-pressed={mode === m}
onClick={() => setMode(m)}
className={cn(
- "t-12 h-6 rounded-md px-2 outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-brand/40",
+ "t-12 h-8 rounded-md px-3 outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-brand/40 md:h-6 md:px-2",
mode === m ? "bg-surface text-fg shadow-[0_1px_2px_rgba(0,0,0,0.06)]" : "text-fg-2 hover-fine:text-fg",
)}
>
@@ -268,12 +257,22 @@ function HoursCard() {
formatTick={tickHours}
unit={60}
labelEvery={mode === "week" ? 2 : 1}
+ compactLabelEvery={mode === "week" ? 3 : 2}
/>
}
table={
<DataTable
head={[mode === "day" ? "Day" : "Week", "Billable", "Non-billable", "Total"]}
- rows={buckets.map((b) => [b.title, fmtHours(b.values[0]), fmtHours(b.values[1]), fmtHours(b.values[0] + b.values[1])])}
+ rows={buckets.map((b) => [
+ // Short dates on phones keep all four columns in view.
+ <>
+ <span className="md:hidden">{b.short}</span>
+ <span className="max-md:hidden">{b.title}</span>
+ </>,
+ fmtHours(b.values[0]),
+ fmtHours(b.values[1]),
+ fmtHours(b.values[0] + b.values[1]),
+ ])}
foot={["Total", fmtHours(billable), fmtHours(other), fmtHours(billable + other)]}
/>
}
@@ -390,14 +389,14 @@ function InvoicesCard() {
title="Invoice revenue by status"
description={`${money(total, currency)} invoiced across ${plural(invoices.length, "invoice", "invoices")}`}
actions={
- <Button variant="ghost" size="sm" asChild className="mr-1">
+ <Button variant="ghost" size="sm" asChild className="max-md:h-9 md:mr-1">
<Link href="/invoice">
Invoices <ArrowRightIcon />
</Link>
</Button>
}
chart={
- <div className="grid gap-5 pt-1">
+ <div className="grid min-w-0 gap-5 pt-1">
<div className="flex h-3 gap-[2px]" role="img" aria-label="Share of invoiced revenue by status">
{visible.length === 0 ? <span className="h-full flex-1 rounded-[4px] bg-hairline" /> : null}
{visible.map((r, i) => (
@@ -428,23 +427,31 @@ function InvoicesCard() {
{rows.map((r) => {
const s = INVOICE_STATUS[r.status];
return (
- <li key={r.status} className="flex h-11 items-center gap-3 border-b border-subtle last:border-b-0">
- <span
- className="inline-flex h-[22px] shrink-0 items-center gap-1 rounded-[6px] border pr-1.5 pl-1 [&_svg]:size-3.5"
- style={{
- background: `var(--status-${s.tone}-tint)`,
- borderColor: `var(--status-${s.tone}-border)`,
- color: `var(--status-${s.tone})`,
- }}
- >
- {s.icon}
- <span className="salt text-[12px] leading-none tracking-[-0.24px] whitespace-nowrap">{s.label}</span>
+ <li
+ key={r.status}
+ className="flex items-center gap-3 border-b border-subtle py-2.5 last:border-b-0 md:h-11 md:py-0"
+ >
+ {/* Phones: badge over count on the left, amount over share on the right. */}
+ <span className="flex min-w-0 flex-1 flex-col items-start gap-1.5 md:flex-row md:items-center md:gap-3">
+ <span
+ className="inline-flex h-[22px] shrink-0 items-center gap-1 rounded-[6px] border pr-1.5 pl-1 [&_svg]:size-3.5"
+ style={{
+ background: `var(--status-${s.tone}-tint)`,
+ borderColor: `var(--status-${s.tone}-border)`,
+ color: `var(--status-${s.tone})`,
+ }}
+ >
+ {s.icon}
+ <span className="salt text-[12px] leading-none tracking-[-0.24px] whitespace-nowrap">{s.label}</span>
+ </span>
+ <span className="t-12 text-fg-2 md:flex-1">{plural(r.count, "invoice", "invoices")}</span>
</span>
- <span className="t-12 flex-1 text-fg-2">{plural(r.count, "invoice", "invoices")}</span>
- <span className="t-12 w-10 text-right text-placeholder tabular-nums">
- {total ? `${Math.round((r.amount / total) * 100)}%` : "—"}
+ <span className="flex shrink-0 flex-col items-end gap-1.5 md:flex-row md:items-center md:gap-3">
+ <span className="t-12 text-right text-placeholder tabular-nums max-md:order-last md:w-10">
+ {total ? `${Math.round((r.amount / total) * 100)}%` : "—"}
+ </span>
+ <span className="t-14 text-right text-fg tabular-nums md:w-24">{money(r.amount, currency)}</span>
</span>
- <span className="t-14 w-24 text-right text-fg tabular-nums">{money(r.amount, currency)}</span>
</li>
);
})}
diff --git a/src/components/pages/analytics/charts.tsx b/src/components/pages/analytics/charts.tsx
index d29f566..9b479fa 100644
--- a/src/components/pages/analytics/charts.tsx
+++ b/src/components/pages/analytics/charts.tsx
@@ -54,6 +54,7 @@ export function ChartCard({
className?: string;
}) {
const [view, setView] = useState<"chart" | "table">("chart");
+ const showLegend = !!legend && legend.length > 1 && view === "chart";
return (
<Card className={className}>
<CardHeader
@@ -61,7 +62,7 @@ export function ChartCard({
description={description}
actions={
<>
- {actions}
+ {actions ? <div className="hidden md:contents">{actions}</div> : null}
<div className="flex items-center rounded-lg bg-app p-0.5" role="group" aria-label="View">
<ToggleIcon active={view === "chart"} label="Chart view" onClick={() => setView("chart")}>
<ChartColumnIcon />
@@ -73,8 +74,19 @@ export function ChartCard({
</>
}
/>
- {legend && legend.length > 1 && view === "chart" ? <Legend series={legend} className="px-4 pt-3" /> : null}
+ {showLegend ? <Legend series={legend} className="px-4 pt-3 max-md:hidden" /> : null}
+ {/* Phones: card actions leave the header so the title keeps its width — beside the legend
+ for charts that have one, otherwise in a footer row. */}
+ {legend && legend.length > 1 && (actions || showLegend) ? (
+ <div className="flex min-h-11 items-center gap-3 px-4 pt-3 md:hidden">
+ {showLegend ? <Legend series={legend} className="min-w-0 flex-1 gap-x-3 gap-y-1" /> : <span className="flex-1" />}
+ {actions}
+ </div>
+ ) : null}
<div className="flex-1 p-4">{view === "chart" ? chart : table}</div>
+ {actions && !(legend && legend.length > 1) ? (
+ <div className="flex justify-end border-t border-subtle px-2 py-1.5 md:hidden">{actions}</div>
+ ) : null}
</Card>
);
}
@@ -99,7 +111,7 @@ function ToggleIcon({
aria-pressed={active}
onClick={onClick}
className={cn(
- "flex size-6 items-center justify-center rounded-md outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-brand/40 [&_svg]:size-3.5",
+ "flex size-8 items-center justify-center rounded-md outline-none transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-brand/40 md:size-6 [&_svg]:size-3.5",
active ? "bg-surface text-fg shadow-[0_1px_2px_rgba(0,0,0,0.06)]" : "text-icon hover-fine:text-fg",
)}
>
@@ -160,6 +172,7 @@ export function ColumnChart({
formatTick = format,
height = 180,
labelEvery = 1,
+ compactLabelEvery = labelEvery,
unit = 1,
}: {
columns: Column[];
@@ -168,6 +181,8 @@ export function ColumnChart({
formatTick?: (v: number) => string;
height?: number;
labelEvery?: number;
+ /** Label stride below md, counted from the emphasized column so it always keeps its label. */
+ compactLabelEvery?: number;
/** Ticks are rounded in this unit (e.g. 60 to get whole hours from minutes). */
unit?: number;
}) {
@@ -178,6 +193,8 @@ export function ColumnChart({
const ticks = scaled.ticks.map((t) => t * unit);
const peak = max > 0 ? totals.indexOf(max) : -1;
const y = (v: number) => (v / top) * height;
+ const emphasized = columns.findLastIndex((c) => c.emphasis);
+ const anchor = emphasized >= 0 ? emphasized : columns.length - 1;
return (
<div className="flex gap-2 pt-4">
@@ -265,14 +282,24 @@ export function ColumnChart({
style={{ gridTemplateColumns: `repeat(${columns.length}, minmax(0, 1fr))` }}
aria-hidden
>
- {columns.map((c, i) => (
- <span
- key={c.key}
- className={cn("t-12-tight salt truncate text-center", c.emphasis ? "text-fg" : "text-placeholder")}
- >
- {(columns.length - 1 - i) % labelEvery === 0 ? c.label : ""}
- </span>
- ))}
+ {columns.map((c, i) => {
+ const wide = (columns.length - 1 - i) % labelEvery === 0;
+ const compact = Math.abs(i - anchor) % compactLabelEvery === 0;
+ return (
+ <span key={c.key} className="flex min-w-0 justify-center">
+ <span
+ className={cn(
+ "t-12-tight salt text-center whitespace-nowrap md:min-w-0 md:truncate",
+ c.emphasis ? "text-fg" : "text-placeholder",
+ wide && !compact && "max-md:invisible",
+ !wide && compact && "md:invisible",
+ )}
+ >
+ {wide || compact ? c.label : ""}
+ </span>
+ </span>
+ );
+ })}
</div>
</div>
</div>
@@ -296,7 +323,7 @@ export function DataTable({
<thead className="sticky top-0 bg-app">
<tr className="h-10">
{head.map((h, i) => (
- <th key={h} className={cn("t-14 px-3 font-medium text-fg-2", i === 0 ? "text-left" : "text-right")}>
+ <th key={h} className={cn("t-14 px-2 font-medium whitespace-nowrap text-fg-2 md:px-3", i === 0 ? "text-left" : "text-right")}>
{h}
</th>
))}
@@ -306,7 +333,7 @@ export function DataTable({
{rows.map((r, ri) => (
<tr key={ri} className="h-10 border-t border-subtle">
{r.map((cell, i) => (
- <td key={i} className={cn("t-14 px-3 text-table", i === 0 ? "text-left" : "text-right tabular-nums")}>
+ <td key={i} className={cn("t-14 px-2 whitespace-nowrap text-table md:px-3", i === 0 ? "text-left" : "text-right tabular-nums")}>
{cell}
</td>
))}
@@ -317,7 +344,7 @@ export function DataTable({
<tfoot>
<tr className="h-10 border-t border-hairline bg-app">
{foot.map((cell, i) => (
- <td key={i} className={cn("t-14 px-3 text-fg", i === 0 ? "text-left" : "text-right tabular-nums")}>
+ <td key={i} className={cn("t-14 px-2 whitespace-nowrap text-fg md:px-3", i === 0 ? "text-left" : "text-right tabular-nums")}>
{cell}
</td>
))}