Open History to see the last commit
1import { useMemo } from "react";
2
3import { localDay, useEditsPerDay, useFolderStats, useFolders } from "@/db/queries";
4import type { Folder } from "@/db/schema";
5
6/*
7 * Turns the analytics live queries' rows (`folderStatsQuery`,
8 * `editsPerDayQuery` in `db/queries.ts`) into what the charts draw: folder
9 * names, days without edits, the chart's date range. The counting happens in
10 * the queries, incrementally.
11 */
12
13export interface FolderChartRow {
14 folderId: string;
15 name: string;
16 notes: number;
17 words: number;
18}
19
20/** One row per folder, in sidebar order; folders without notes count zero. */
21export function folderChartRows(
22 folders: readonly Pick<Folder, "id" | "name">[],
23 stats: readonly { folderId: string; notes: number; words: number | null | undefined }[],
24): FolderChartRow[] {
25 const byFolder = new Map(stats.map((row) => [row.folderId, row]));
26 return folders.map((folder) => {
27 const row = byFolder.get(folder.id);
28 return {
29 folderId: folder.id,
30 name: folder.name,
31 notes: row?.notes ?? 0,
32 words: row?.words ?? 0,
33 };
34 });
35}
36
37export interface DayChartRow {
38 /** `YYYY-MM-DD`, local time. */
39 day: string;
40 /** Short label for the axis, e.g. "3 Nov". */
41 label: string;
42 notes: number;
43}
44
45/** How many days the chart shows, ending today. */
46export const RECENT_DAYS = 14;
47
48const dayLabel = new Intl.DateTimeFormat("en-GB", { day: "numeric", month: "short" });
49
50/**
51 * Notes by day of last edit, for the last `days` days (oldest first, days
52 * without edits at zero), and how many notes were last edited before that.
53 * `today` is a local `YYYY-MM-DD`.
54 */
55export function recentDayRows(
56 perDay: readonly { day: string; notes: number }[],
57 today: string,
58 days = RECENT_DAYS,
59): { rows: DayChartRow[]; earlier: number } {
60 const counts = new Map(perDay.map((row) => [row.day, row.notes]));
61 const end = new Date(`${today}T12:00`);
62 const rows: DayChartRow[] = [];
63 for (let offset = days - 1; offset >= 0; offset--) {
64 const date = new Date(end);
65 date.setDate(end.getDate() - offset);
66 const day = localDay(date.toISOString());
67 rows.push({ day, label: dayLabel.format(date), notes: counts.get(day) ?? 0 });
68 }
69 const first = rows[0]!.day;
70 const earlier = perDay.filter((row) => row.day < first).reduce((sum, row) => sum + row.notes, 0);
71 return { rows, earlier };
72}
73
74/**
75 * The live analytics rows: notes and words per folder, and notes by day of
76 * last edit. Both come from `groupBy` live queries, so they change as you
77 * type.
78 */
79export function useAnalyticsRows() {
80 const folders = useFolders();
81 const perFolder = useFolderStats();
82 const perDay = useEditsPerDay();
83 const folderRows = useMemo(() => folderChartRows(folders, perFolder), [folders, perFolder]);
84 const days = useMemo(() => recentDayRows(perDay, localDay(new Date().toISOString())), [perDay]);
85 return { folderRows, days };
86}
87
88/** Chart heights, shared by the charts and their loading placeholders. */
89export function folderChartHeight(rowCount: number): number {
90 return rowCount * 30 + 12;
91}
92
93export const DAY_CHART_HEIGHT = 180;