Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

analytics-charts.tsx

148 lines4.8 KB

1import { ChevronRight } from "lucide-react";
2import type { ReactNode } from "react";
3​
4import { cn } from "@/lib/utils";
5​
6import { RECENT_DAYS, useAnalyticsRows } from "./analytics-data";
7import { LazyDayBarChart, LazyFolderBarChart } from "./lazy-charts";
8​
9const numberFormat = new Intl.NumberFormat("en-GB");
10const dayFormat = new Intl.DateTimeFormat("en-GB", {
11 weekday: "short",
12 day: "numeric",
13 month: "short",
14});
15​
16/**
17 * The Analytics page: three charts drawn with TanStack Charts, each with
18 * its exact numbers as a table one click away. The chart library loads when
19 * the page first shows (see `lazy-charts.tsx`).
20 */
21export function AnalyticsCharts({ variant }: { variant: "mac" | "ios" }) {
22 const { folderRows, days } = useAnalyticsRows();
23 const card =
24 variant === "ios"
25 ? "rounded-[26px] bg-ios-card px-4 pt-3 pb-2"
26 : "rounded-xl border border-mac-separator bg-black/[0.025] px-4 pt-3 pb-2 dark:bg-white/[0.04]";
27 const folderTable = (measure: "notes" | "words") => (
28 <ChartTable
29 columns={["Folder", measure === "notes" ? "Notes" : "Words"]}
30 rows={folderRows.map((row) => ({ key: row.folderId, cells: [row.name, row[measure]] }))}
31 />
32 );
33​
34 return (
35 <div
36 data-spotlight="analytics"
37 className={cn("space-y-6", variant === "ios" ? "text-[15px]" : "text-[13px]")}
38 >
39 <div className={cn("grid gap-6", variant === "mac" && "@3xl/notes:grid-cols-2")}>
40 <ChartCard title="Notes per folder" className={card} table={folderTable("notes")}>
41 <LazyFolderBarChart rows={folderRows} measure="notes" />
42 </ChartCard>
43 <ChartCard title="Words per folder" className={card} table={folderTable("words")}>
44 <LazyFolderBarChart rows={folderRows} measure="words" />
45 </ChartCard>
46 </div>
47 <ChartCard
48 title="Notes by day of last edit"
49 className={card}
50 caption={`The last ${RECENT_DAYS} days. ${
51 days.earlier === 0
52 ? "No notes were last edited before that."
53 : `${days.earlier} ${days.earlier === 1 ? "note was" : "notes were"} last edited before that.`
54 }`}
55 table={
56 <ChartTable
57 columns={["Day", "Notes"]}
58 rows={[
59 ...days.rows.map((row): ChartTableRow => ({
60 key: row.day,
61 cells: [dayFormat.format(new Date(`${row.day}T12:00`)), row.notes],
62 })),
63 { key: "earlier", cells: ["Earlier", days.earlier] },
64 ]}
65 />
66 }
67 >
68 <LazyDayBarChart rows={days.rows} />
69 </ChartCard>
70 </div>
71 );
73​
74function ChartCard({
75 title,
76 caption,
77 className,
78 table,
79 children,
80}: {
81 title: string;
82 caption?: string;
83 className: string;
84 /** The chart's numbers as a table, behind "Show as table". */
85 table: ReactNode;
86 children: ReactNode;
87}) {
88 return (
89 <section className="min-w-0">
90 <h2 className="mb-1.5 px-4 text-[0.85em] font-semibold tracking-wide uppercase opacity-55">
91 {title}
92 </h2>
93 <div className={cn("analytics-chart", className)}>
94 {children}
95 <details className="group/table -mx-4 border-t border-current/10 px-4 pt-1.5">
96 <summary className="flex cursor-pointer list-none items-center gap-1 rounded py-1 text-[0.85em] font-medium opacity-70 outline-none hover:opacity-100 focus-visible:ring-2 focus-visible:ring-notes-accent/60 [&::-webkit-details-marker]:hidden">
97 <ChevronRight
98 aria-hidden
99 className="size-3.5 transition-transform group-open/table:rotate-90 motion-reduce:transition-none"
100 />
101 Show as table
102 </summary>
103 {table}
104 </details>
105 </div>
106 {caption && <p className="mt-1.5 px-4 text-[0.85em] opacity-55">{caption}</p>}
107 </section>
108 );
110​
111export interface ChartTableRow {
112 key: string;
113 cells: [label: string, value: number];
115​
116/** A chart's exact numbers: a label column, then numbers. */
117export function ChartTable({
118 columns,
119 rows,
120}: {
121 columns: [string, string];
122 rows: ChartTableRow[];
123}) {
124 return (
125 <table className="mb-2 w-full border-collapse">
126 <thead>
127 <tr className="text-left text-[0.85em] opacity-55">
128 <th scope="col" className="py-1.5 font-medium">
129 {columns[0]}
130 </th>
131 <th scope="col" className="py-1.5 text-right font-medium">
132 {columns[1]}
133 </th>
134 </tr>
135 </thead>
136 <tbody className="divide-y divide-current/10">
137 {rows.map(({ key, cells: [label, value] }) => (
138 <tr key={key}>
139 <th scope="row" className="py-1.5 text-left font-normal">
140 {label}
141 </th>
142 <td className="py-1.5 text-right tabular-nums">{numberFormat.format(value)}</td>
143 </tr>
144 ))}
145 </tbody>
146 </table>
147 );