Open History to see the last commit
1import type { ReactNode } from "react";
2
3import { useEditsPerDay, useFolderStats, useFolders } from "@/db/queries";
4import { cn } from "@/lib/utils";
5
6const dayFormat = new Intl.DateTimeFormat("en-GB", {
7 weekday: "short",
8 day: "numeric",
9 month: "short",
10 year: "numeric",
11});
12const numberFormat = new Intl.NumberFormat("en-GB");
13
14/**
15 * The analytics page, as plain tables for now (phase 4 draws them as
16 * charts). Both are live `groupBy` queries, so they change as you type.
17 */
18export function AnalyticsTables({ variant }: { variant: "mac" | "ios" }) {
19 const folders = useFolders();
20 const perFolder = useFolderStats();
21 const perDay = useEditsPerDay();
22 const statsByFolder = new Map(perFolder.map((row) => [row.folderId, row]));
23
24 const card =
25 variant === "ios"
26 ? "rounded-[26px] bg-ios-card"
27 : "rounded-xl border border-mac-separator bg-black/[0.025] dark:bg-white/[0.04]";
28
29 return (
30 <div className={cn("space-y-6", variant === "ios" ? "text-[17px]" : "text-[13px]")}>
31 <StatsTable
32 title="Notes and words per folder"
33 className={card}
34 columns={["Folder", "Notes", "Words"]}
35 rows={folders.map((folder) => {
36 const stats = statsByFolder.get(folder.id);
37 return {
38 key: folder.id,
39 cells: [folder.name, stats?.notes ?? 0, stats?.words ?? 0],
40 };
41 })}
42 />
43 <StatsTable
44 title="Notes by day of last edit"
45 className={card}
46 columns={["Day", "Notes"]}
47 rows={perDay.map((row) => ({
48 key: row.day,
49 cells: [dayFormat.format(new Date(`${row.day}T00:00`)), row.notes],
50 }))}
51 />
52 </div>
53 );
54}
55
56function StatsTable({
57 title,
58 className,
59 columns,
60 rows,
61}: {
62 title: string;
63 className: string;
64 columns: string[];
65 rows: { key: string; cells: (ReactNode | number)[] }[];
66}) {
67 return (
68 <section>
69 <h2 className="mb-1.5 px-4 text-[0.85em] font-semibold tracking-wide uppercase opacity-55">
70 {title}
71 </h2>
72 <div className={cn(className, "overflow-hidden")}>
73 <table className="w-full border-collapse">
74 <thead>
75 <tr className="text-left text-[0.85em] opacity-55">
76 {columns.map((column, index) => (
77 <th
78 key={column}
79 scope="col"
80 className={cn("px-4 pt-2.5 pb-1.5 font-medium", index > 0 && "text-right")}
81 >
82 {column}
83 </th>
84 ))}
85 </tr>
86 </thead>
87 <tbody className="divide-y divide-mac-separator dark:divide-white/10">
88 {rows.map((row) => (
89 <tr key={row.key}>
90 {row.cells.map((cell, index) =>
91 index === 0 ? (
92 <th key={index} scope="row" className="px-4 py-2 text-left font-normal">
93 {cell}
94 </th>
95 ) : (
96 <td key={index} className="px-4 py-2 text-right tabular-nums">
97 {typeof cell === "number" ? numberFormat.format(cell) : cell}
98 </td>
99 ),
100 )}
101 </tr>
102 ))}
103 </tbody>
104 </table>
105 </div>
106 </section>
107 );
108}