@jant/core 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/bin/lib/site-snapshot.js +10 -0
  2. package/dist/{app-Gt7_skt6.js → app-CbOZ2Irh.js} +8704 -5180
  3. package/dist/client/.vite/manifest.json +125 -85
  4. package/dist/client/_assets/chunks/{confirm-D7GrwsCN.js → confirm-CMe01lne.js} +1 -1
  5. package/dist/client/_assets/chunks/{create-editor-5Y49x7h5.js → create-editor-hWapNE40.js} +26 -26
  6. package/dist/client/_assets/chunks/footnote-rail-9zwl6jtV.js +2 -0
  7. package/dist/client/_assets/chunks/{jant-locale-picker-DL6mIOT5.js → jant-locale-picker-2L4rkDaz.js} +1 -1
  8. package/dist/client/_assets/chunks/lit-Bw_voPoW.js +2 -0
  9. package/dist/client/_assets/chunks/preload-helper-Czpn1I53.js +1 -0
  10. package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +1 -0
  11. package/dist/client/_assets/chunks/{settings-paths-h6RjIe3C.js → settings-paths-BBVT8zKj.js} +1 -1
  12. package/dist/client/_assets/chunks/slug-format-BuCPjSrB.js +1 -0
  13. package/dist/client/_assets/chunks/sortable-list-DJXwkaOv.js +2 -0
  14. package/dist/client/_assets/chunks/types-DrYFqJH2.js +1 -0
  15. package/dist/client/_assets/chunks/{unsafe-html-B5ZEXaxg.js → unsafe-html-CWos3v6N.js} +1 -1
  16. package/dist/client/_assets/chunks/unsafe-svg-CUrgo4Xg.js +1 -0
  17. package/dist/client/_assets/client-BO8GOFfT.js +354 -0
  18. package/dist/client/_assets/client-D8Rrsd73.css +2 -0
  19. package/dist/client/_assets/client-auth-CsUGM718.js +859 -0
  20. package/dist/client/_assets/client-author-QPm1whbO.css +2 -0
  21. package/dist/client/_assets/client-compose-BB4U7Wz0.js +2004 -0
  22. package/dist/client/_assets/{client-manage-C1eDB1Rc.js → client-manage-CI9-eh-8.js} +176 -176
  23. package/dist/client/_assets/{client-settings-DIM44Zqh.js → client-settings-BnGPuqwc.js} +166 -166
  24. package/dist/client/_assets/client-share-0DICJjAc.css +1 -0
  25. package/dist/client/_assets/client-share-C59mOLJ1.js +149 -0
  26. package/dist/index.js +1 -1
  27. package/dist/node.js +3 -2
  28. package/package.json +2 -1
  29. package/src/app.tsx +9 -0
  30. package/src/client/components/compose-types.ts +1 -0
  31. package/src/client/components/jant-compose-dialog.ts +1 -0
  32. package/src/client/components/jant-compose-editor.ts +30 -0
  33. package/src/client/components/jant-compose-fullscreen.ts +1 -0
  34. package/src/client/components/jant-post-menu.ts +80 -13
  35. package/src/client/components/jant-share-dialog.ts +422 -0
  36. package/src/client/components/jant-share-menu.ts +188 -0
  37. package/src/client/compose-bridge.ts +61 -1
  38. package/src/client/feed-video-player.ts +6 -0
  39. package/src/client/lazy-entries.ts +21 -1
  40. package/src/client/post-menu-shared.ts +100 -0
  41. package/src/client/share-capture.ts +443 -0
  42. package/src/client/share-launch.ts +133 -0
  43. package/src/client/tiptap/bubble-menu.ts +25 -2
  44. package/src/client/tiptap/create-editor.ts +2 -0
  45. package/src/client/tiptap/extensions.ts +4 -0
  46. package/src/client/video-processor.ts +106 -52
  47. package/src/client/view-confirm.ts +108 -0
  48. package/src/client-share.ts +8 -0
  49. package/src/client.ts +2 -0
  50. package/src/db/migrations/0035_brainy_next_avengers.sql +9 -0
  51. package/src/db/migrations/0036_gorgeous_smasher.sql +7 -0
  52. package/src/db/migrations/meta/0035_snapshot.json +2877 -0
  53. package/src/db/migrations/meta/0036_snapshot.json +2924 -0
  54. package/src/db/migrations/meta/_journal.json +14 -0
  55. package/src/db/migrations/pg/0033_cute_magdalene.sql +10 -0
  56. package/src/db/migrations/pg/0034_material_agent_brand.sql +8 -0
  57. package/src/db/migrations/pg/meta/0033_snapshot.json +3719 -0
  58. package/src/db/migrations/pg/meta/0034_snapshot.json +3766 -0
  59. package/src/db/migrations/pg/meta/_journal.json +14 -0
  60. package/src/db/pg/schema.ts +40 -0
  61. package/src/db/schema.ts +61 -0
  62. package/src/i18n/locales/public/en.po +5 -0
  63. package/src/i18n/locales/public/en.ts +1 -1
  64. package/src/i18n/locales/public/zh-Hans.po +5 -0
  65. package/src/i18n/locales/public/zh-Hans.ts +1 -1
  66. package/src/i18n/locales/public/zh-Hant.po +5 -0
  67. package/src/i18n/locales/public/zh-Hant.ts +1 -1
  68. package/src/i18n/locales/settings/en.po +149 -0
  69. package/src/i18n/locales/settings/en.ts +1 -1
  70. package/src/i18n/locales/settings/zh-Hans.po +149 -0
  71. package/src/i18n/locales/settings/zh-Hans.ts +1 -1
  72. package/src/i18n/locales/settings/zh-Hant.po +149 -0
  73. package/src/i18n/locales/settings/zh-Hant.ts +1 -1
  74. package/src/lib/discover.ts +31 -0
  75. package/src/lib/env.ts +20 -0
  76. package/src/lib/github-app-state.ts +1 -1
  77. package/src/lib/github-app.ts +183 -1
  78. package/src/lib/markdown-manager.ts +129 -1
  79. package/src/lib/post-meta.ts +13 -3
  80. package/src/lib/referrer-aliases.ts +311 -0
  81. package/src/lib/render.tsx +10 -0
  82. package/src/lib/resolve-config.ts +14 -0
  83. package/src/lib/share-card.ts +653 -0
  84. package/src/lib/summary.ts +152 -54
  85. package/src/lib/tiptap-render.ts +1 -0
  86. package/src/lib/upload.ts +21 -1
  87. package/src/lib/version.ts +2 -0
  88. package/src/lib/video-playback.ts +55 -0
  89. package/src/lib/view-buffer.ts +201 -0
  90. package/src/lib/view-counting.ts +539 -0
  91. package/src/lib/view-token.ts +177 -0
  92. package/src/lib/view.ts +13 -1
  93. package/src/middleware/view-counter.ts +140 -0
  94. package/src/node/runtime.ts +4 -0
  95. package/src/preset.css +31 -14
  96. package/src/routes/api/media.ts +21 -3
  97. package/src/routes/auth/setup.tsx +37 -1
  98. package/src/routes/dash/settings.tsx +433 -168
  99. package/src/routes/pages/archive.tsx +1 -0
  100. package/src/routes/pages/collection.tsx +6 -0
  101. package/src/routes/pages/collections.tsx +1 -0
  102. package/src/routes/pages/featured.tsx +1 -0
  103. package/src/routes/pages/home.tsx +1 -0
  104. package/src/routes/pages/page.tsx +3 -0
  105. package/src/routes/pages/partials.tsx +23 -0
  106. package/src/routes/pages/smart-collection.tsx +2 -0
  107. package/src/routes/pages/subscribe.tsx +1 -0
  108. package/src/routes/view-confirm.ts +63 -0
  109. package/src/services/export-theme/assets/client-site.js +1 -1
  110. package/src/services/export-theme/styles/main.css +39 -21
  111. package/src/services/export.ts +2 -0
  112. package/src/services/index.ts +17 -0
  113. package/src/services/mcp.ts +31 -1
  114. package/src/services/media.ts +63 -0
  115. package/src/services/stats.ts +1130 -0
  116. package/src/style-share.css +16 -0
  117. package/src/styles/components-author.css +177 -0
  118. package/src/styles/components.css +25 -3
  119. package/src/styles/share.css +1103 -0
  120. package/src/styles/tokens.css +65 -30
  121. package/src/styles/ui.css +62 -13
  122. package/src/types/app-context.ts +9 -0
  123. package/src/types/bindings.ts +5 -0
  124. package/src/types/config.ts +54 -0
  125. package/src/types/views.ts +6 -0
  126. package/src/ui/compose/ComposeDialog.tsx +6 -0
  127. package/src/ui/dash/settings/SettingsRootContent.tsx +24 -6
  128. package/src/ui/dash/settings/StatsContent.tsx +855 -0
  129. package/src/ui/font-themes.ts +39 -6
  130. package/src/ui/layouts/BaseLayout.tsx +11 -2
  131. package/src/ui/layouts/SiteLayout.tsx +9 -3
  132. package/src/ui/share/ShareCard.tsx +298 -0
  133. package/src/ui/shared/Icon.tsx +23 -3
  134. package/src/ui/shared/MediaGallery.tsx +31 -4
  135. package/src/ui/shared/PostFooter.tsx +20 -4
  136. package/src/ui/shared/icon-collector.ts +28 -0
  137. package/src/ui/shared/post-article-attributes.ts +3 -0
  138. package/dist/client/_assets/chunks/footnote-rail-BJwswtVQ.js +0 -1
  139. package/dist/client/_assets/chunks/lit-DW8VmJAT.js +0 -2
  140. package/dist/client/_assets/chunks/slug-format-Dsi0e9Xn.js +0 -1
  141. package/dist/client/_assets/chunks/sortable-list-DWNKv782.js +0 -2
  142. package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
  143. package/dist/client/_assets/chunks/unsafe-svg-DTZZEvLU.js +0 -2
  144. package/dist/client/_assets/client-BaqmpPQ-.css +0 -2
  145. package/dist/client/_assets/client-C6melOtG.js +0 -296
  146. package/dist/client/_assets/client-auth-Ccq8kSqq.js +0 -828
  147. package/dist/client/_assets/client-author-CGVvrNZ8.css +0 -2
  148. package/dist/client/_assets/client-compose-oxzPb3SS.js +0 -2004
@@ -0,0 +1,855 @@
1
+ /**
2
+ * Stats: views by hour or by day, the most-viewed pages, and where readers
3
+ * came from.
4
+ *
5
+ * One quiet page of aggregates for the author. It shows a window of time and
6
+ * nothing beyond it — no lifetime total, no comparison with the window before
7
+ * — because a number to beat is the one social mechanic this page must not
8
+ * grow into. Today is the default and is drawn by the hour; a longer window
9
+ * by the day. When counting began inside the window, the page says so and the
10
+ * chart starts on that day: the days before were not counted, which is not
11
+ * the same as a month of zeros. Posts, collections and the site's own pages
12
+ * share one ranked list, since the question is what was read, not what kind
13
+ * of thing it was; a badge says the kind, and a bar behind each row says its
14
+ * share. Every page and every referrer in the lists opens on its own: a
15
+ * page with where its readers came from, a referrer with the pages it sent
16
+ * them to. Each hop drops the other dimension rather than stacking filters,
17
+ * since today's hours are kept for the site as a whole and a filtered chart
18
+ * would have nothing to draw. `services/stats.ts` has the policy behind the
19
+ * numbers.
20
+ */
21
+
22
+ import type { I18n } from "@lingui/core";
23
+ import { msg } from "@lingui/core/macro";
24
+ import { useLingui } from "../../../i18n/context.js";
25
+ import { toPublicPath } from "../../../lib/url.js";
26
+ import { VIEW_INTERNAL_REFERRER } from "../../../lib/view-counting.js";
27
+ import {
28
+ STATS_WINDOWS,
29
+ VIEW_COUNT_RETENTION_DAYS,
30
+ type StatsOverview,
31
+ type StatsPage,
32
+ type StatsPageDetail,
33
+ type StatsPageRow,
34
+ type StatsReferrerDetail,
35
+ type StatsReferrers,
36
+ type StatsSeries,
37
+ type StatsWindow,
38
+ } from "../../../services/stats.js";
39
+ import { FormatBadge } from "../FormatBadge.js";
40
+
41
+ export interface StatsContentProps {
42
+ /** Whether views are being counted; off means no numbers to show. */
43
+ enabled: boolean;
44
+ days: StatsWindow;
45
+ /** The site over the window; `null` when stats are off or a detail is shown. */
46
+ overview: StatsOverview | null;
47
+ /** One page over the window, when the page is about one page. */
48
+ page: StatsPageDetail | null;
49
+ /** One referrer over the window, when the page is about one referrer. */
50
+ referrer: StatsReferrerDetail | null;
51
+ sitePathPrefix: string;
52
+ demoMode: boolean;
53
+ }
54
+
55
+ /** What a stats address can be about besides the site: one page, or one referrer. */
56
+ type StatsTarget = { page: string } | { referrer: string };
57
+
58
+ /**
59
+ * The address of the stats page for a window, and optionally one page or
60
+ * one referrer. A referrer key goes in verbatim, so Direct is `referrer=`
61
+ * and Internal `referrer=%2F`: these addresses live in the settings and
62
+ * are never shown.
63
+ */
64
+ function statsHref(
65
+ sitePathPrefix: string,
66
+ days: StatsWindow,
67
+ target?: StatsTarget,
68
+ ): string {
69
+ const params = new URLSearchParams();
70
+ if (target && "page" in target) params.set("page", target.page);
71
+ if (target && "referrer" in target) params.set("referrer", target.referrer);
72
+ params.set("days", String(days));
73
+ return `${toPublicPath("/settings/stats", sitePathPrefix)}?${params}`;
74
+ }
75
+
76
+ /** `Nov 8` for a day key, in the dashboard's locale. */
77
+ function formatDayLabel(day: string, locale: string): string {
78
+ const [year, month, date] = day.split("-").map(Number);
79
+ return new Intl.DateTimeFormat(locale, {
80
+ month: "short",
81
+ day: "numeric",
82
+ timeZone: "UTC",
83
+ }).format(new Date(Date.UTC(year ?? 1970, (month ?? 1) - 1, date ?? 1)));
84
+ }
85
+
86
+ /** `14:00` for an hour of the day, in the dashboard's locale. */
87
+ function formatHourLabel(hour: number, locale: string): string {
88
+ return new Intl.DateTimeFormat(locale, {
89
+ hour: "2-digit",
90
+ minute: "2-digit",
91
+ hourCycle: "h23",
92
+ timeZone: "UTC",
93
+ }).format(new Date(Date.UTC(2000, 0, 1, hour)));
94
+ }
95
+
96
+ /** The hour of the day an hour key names. */
97
+ function hourOf(hourKey: string): number {
98
+ return Number(hourKey.slice(11, 13));
99
+ }
100
+
101
+ /** What one point of a series is called under the chart and in its tooltips. */
102
+ function pointLabel(series: StatsSeries, at: string, locale: string): string {
103
+ return series.unit === "hour"
104
+ ? formatHourLabel(hourOf(at), locale)
105
+ : formatDayLabel(at, locale);
106
+ }
107
+
108
+ /** The share of a total one row holds, as a CSS percentage for its bar. */
109
+ function shareOf(views: number, total: number): string {
110
+ if (total <= 0) return "0%";
111
+ return `${Math.round((views / total) * 1000) / 10}%`;
112
+ }
113
+
114
+ /** The site's own pages by name; any other path as itself. */
115
+ function pathName(path: string, i18n: I18n): string {
116
+ switch (path) {
117
+ case "/":
118
+ return i18n._(
119
+ msg({
120
+ message: "Home",
121
+ comment:
122
+ "@context: Row for the home page in the pages list on the stats page",
123
+ }),
124
+ );
125
+ case "/archive":
126
+ return i18n._(
127
+ msg({
128
+ message: "Archive",
129
+ comment:
130
+ "@context: Row for the archive page in the pages list on the stats page",
131
+ }),
132
+ );
133
+ case "/collections":
134
+ return i18n._(
135
+ msg({
136
+ message: "Collections",
137
+ comment:
138
+ "@context: Row for the collections directory in the pages list on the stats page",
139
+ }),
140
+ );
141
+ case "/featured":
142
+ return i18n._(
143
+ msg({
144
+ message: "Featured",
145
+ comment:
146
+ "@context: Row for the Featured page in the pages list on the stats page",
147
+ }),
148
+ );
149
+ case "/subscribe":
150
+ return i18n._(
151
+ msg({
152
+ message: "Subscribe",
153
+ comment:
154
+ "@context: Row for the subscribe page in the pages list on the stats page",
155
+ }),
156
+ );
157
+ default:
158
+ return path;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * What a page is called on the stats page: a post by the name the rest of
164
+ * Jant gives it, or its address when nothing gives one; a collection by its
165
+ * title; the site's own pages by name; any other path as itself. A deleted
166
+ * post or collection is said to be one.
167
+ *
168
+ * @param page - The page
169
+ * @param i18n - The dashboard's locale
170
+ * @returns The name
171
+ * @example
172
+ * statsPageName({ kind: "path", key: "/" }, i18n); // "Home"
173
+ */
174
+ export function statsPageName(page: StatsPage, i18n: I18n): string {
175
+ switch (page.kind) {
176
+ case "post":
177
+ if (!page.post) {
178
+ return i18n._(
179
+ msg({
180
+ message: "Deleted post",
181
+ comment:
182
+ "@context: Name on the stats page of a post whose views remain after it was deleted",
183
+ }),
184
+ );
185
+ }
186
+ return page.post.title || page.post.slug;
187
+ case "collection":
188
+ if (!page.collection) {
189
+ return i18n._(
190
+ msg({
191
+ message: "Deleted collection",
192
+ comment:
193
+ "@context: Name on the stats page of a collection whose views remain after it was deleted",
194
+ }),
195
+ );
196
+ }
197
+ return page.collection.title;
198
+ case "path":
199
+ return pathName(page.key, i18n);
200
+ }
201
+ }
202
+
203
+ /** The public address of a page, while it still has one. */
204
+ function pagePublicPath(page: StatsPage): string | null {
205
+ switch (page.kind) {
206
+ case "post":
207
+ return page.post ? `/${page.post.slug}` : null;
208
+ case "collection":
209
+ return page.collection ? `/${page.collection.slug}` : null;
210
+ case "path":
211
+ return page.key;
212
+ }
213
+ }
214
+
215
+ /**
216
+ * What a referrer is called on the stats page: a host or a tag as itself,
217
+ * the two keys that are not hosts by name.
218
+ *
219
+ * @param key - The referrer's counter key
220
+ * @param i18n - The dashboard's locale
221
+ * @returns The name
222
+ * @example
223
+ * statsReferrerName("", i18n); // "Direct"
224
+ */
225
+ export function statsReferrerName(key: string, i18n: I18n): string {
226
+ if (key === "") {
227
+ return i18n._(
228
+ msg({
229
+ message: "Direct",
230
+ comment:
231
+ "@context: Name on the stats page of the views with no referrer: typed addresses, apps, feed readers; a row of the referrer list and the heading of their own numbers",
232
+ }),
233
+ );
234
+ }
235
+ if (key === VIEW_INTERNAL_REFERRER) {
236
+ return i18n._(
237
+ msg({
238
+ message: "Internal",
239
+ comment:
240
+ "@context: Name on the stats page of the views that followed a link from another page of the site; a row of the referrer list and the heading of their own numbers",
241
+ }),
242
+ );
243
+ }
244
+ return key;
245
+ }
246
+
247
+ /** The kind of a page, where the name alone does not say: a post's format, or that it is a collection. */
248
+ function PageBadge({ page }: { page: StatsPage }) {
249
+ const { i18n } = useLingui();
250
+ if (page.kind === "post" && page.post) {
251
+ return <FormatBadge type={page.post.format} />;
252
+ }
253
+ if (page.kind === "collection") {
254
+ return (
255
+ <span class="badge-outline">
256
+ {i18n._(
257
+ msg({
258
+ message: "Collection",
259
+ comment: "@context: Badge on a collection's row on the stats page",
260
+ }),
261
+ )}
262
+ </span>
263
+ );
264
+ }
265
+ return null;
266
+ }
267
+
268
+ /** The translator the components get from the context; it takes values. */
269
+ type Translator = ReturnType<typeof useLingui>["i18n"];
270
+
271
+ /** What a window is called on the switch. */
272
+ function windowLabel(window: StatsWindow, i18n: Translator): string {
273
+ if (window === 1) {
274
+ return i18n._(
275
+ msg({
276
+ message: "Today",
277
+ comment:
278
+ "@context: The one-day option of the window switch on the stats page",
279
+ }),
280
+ );
281
+ }
282
+ return i18n._(
283
+ msg({
284
+ message: "{window} days",
285
+ comment:
286
+ "@context: One option of the window switch on the stats page; {window} is 7 or 30",
287
+ }),
288
+ { window },
289
+ );
290
+ }
291
+
292
+ function WindowSwitch({
293
+ days,
294
+ sitePathPrefix,
295
+ target,
296
+ }: {
297
+ days: StatsWindow;
298
+ sitePathPrefix: string;
299
+ target?: StatsTarget;
300
+ }) {
301
+ const { i18n } = useLingui();
302
+ return (
303
+ <nav
304
+ class="settings-stats-windows"
305
+ aria-label={i18n._(
306
+ msg({
307
+ message: "Window",
308
+ comment:
309
+ "@context: Accessible name of the today/7/30-day switch on the stats page",
310
+ }),
311
+ )}
312
+ >
313
+ {STATS_WINDOWS.map((window) => (
314
+ <a
315
+ key={window}
316
+ href={statsHref(sitePathPrefix, window, target)}
317
+ class="settings-stats-window"
318
+ aria-current={window === days ? "page" : undefined}
319
+ >
320
+ {windowLabel(window, i18n)}
321
+ </a>
322
+ ))}
323
+ </nav>
324
+ );
325
+ }
326
+
327
+ /**
328
+ * One bar per point over the whole window, so a bar is the same width
329
+ * whatever the record covers. Today has 24 slots and bars up to the current
330
+ * hour. A longer window has one slot per day; the days before the record
331
+ * have no bar and no baseline, since the baseline starts where counting did.
332
+ * The total line has then already named that day, so the axis carries
333
+ * today's date alone; it names both ends only when every day was counted.
334
+ */
335
+ function ViewsChart({
336
+ series,
337
+ start,
338
+ locale,
339
+ }: {
340
+ series: StatsSeries;
341
+ start: string | null;
342
+ locale: string;
343
+ }) {
344
+ const { i18n } = useLingui();
345
+ const { unit, points } = series;
346
+ const slots = unit === "hour" ? 24 : points.length;
347
+ const peak = Math.max(1, ...points.map((point) => point.views));
348
+ const startIndex =
349
+ unit === "day" && start
350
+ ? Math.max(
351
+ 0,
352
+ points.findIndex((point) => point.at === start),
353
+ )
354
+ : 0;
355
+ const single = unit === "day" && start !== null;
356
+ const firstLabel =
357
+ unit === "hour"
358
+ ? formatHourLabel(0, locale)
359
+ : points[startIndex] && formatDayLabel(points[startIndex].at, locale);
360
+ const last = points.at(-1);
361
+ const lastLabel =
362
+ unit === "hour"
363
+ ? formatHourLabel(23, locale)
364
+ : last && formatDayLabel(last.at, locale);
365
+
366
+ return (
367
+ <figure class="settings-stats-figure">
368
+ <svg
369
+ class="settings-stats-chart"
370
+ viewBox={`0 0 ${slots} 100`}
371
+ preserveAspectRatio="none"
372
+ role="img"
373
+ aria-label={
374
+ unit === "hour"
375
+ ? i18n._(
376
+ msg({
377
+ message: "Views per hour",
378
+ comment:
379
+ "@context: Accessible name of today's bar chart on the stats page",
380
+ }),
381
+ )
382
+ : i18n._(
383
+ msg({
384
+ message: "Views per day",
385
+ comment:
386
+ "@context: Accessible name of the bar chart on the stats page",
387
+ }),
388
+ )
389
+ }
390
+ >
391
+ {points.map((point, index) => {
392
+ if (index < startIndex) return null;
393
+ const height = (point.views / peak) * 100;
394
+ return (
395
+ <rect
396
+ key={point.at}
397
+ class="settings-stats-bar"
398
+ x={index + 0.15}
399
+ y={100 - height}
400
+ width={0.7}
401
+ height={height}
402
+ >
403
+ <title>
404
+ {i18n._(
405
+ msg({
406
+ message:
407
+ "{day}: {count, plural, one {# view} other {# views}}",
408
+ comment:
409
+ "@context: Tooltip on one bar of the stats chart; {day} is a date like Nov 8 or an hour like 14:00",
410
+ }),
411
+ {
412
+ day: pointLabel(series, point.at, locale),
413
+ count: point.views,
414
+ },
415
+ )}
416
+ </title>
417
+ </rect>
418
+ );
419
+ })}
420
+ </svg>
421
+ {firstLabel && lastLabel && (
422
+ <figcaption
423
+ class={`settings-stats-axis${single ? " settings-stats-axis-single" : ""}`}
424
+ style={`--stats-start:${(startIndex / slots) * 100}%`}
425
+ aria-hidden="true"
426
+ >
427
+ {!single && <span>{firstLabel}</span>}
428
+ <span>{lastLabel}</span>
429
+ </figcaption>
430
+ )}
431
+ </figure>
432
+ );
433
+ }
434
+
435
+ /** One ranked row: its place, its name, its kind, its count, and a bar for its share. */
436
+ function RankedRow({
437
+ rank,
438
+ share,
439
+ views,
440
+ children,
441
+ }: {
442
+ rank: number;
443
+ share: string;
444
+ views: number;
445
+ children: unknown;
446
+ }) {
447
+ return (
448
+ <li class="settings-stats-row" style={`--stats-share:${share}`}>
449
+ <span class="settings-stats-rank">{rank}</span>
450
+ {children}
451
+ <span class="settings-stats-count">{views}</span>
452
+ </li>
453
+ );
454
+ }
455
+
456
+ /**
457
+ * Where the views came from, each referrer a link to the pages it sent
458
+ * readers to. Internal and Direct take their place among the hosts by
459
+ * size; Other is last and opens nothing, being a remainder.
460
+ */
461
+ function ReferrerList({
462
+ referrers,
463
+ total,
464
+ days,
465
+ sitePathPrefix,
466
+ }: {
467
+ referrers: StatsReferrers;
468
+ total: number;
469
+ days: StatsWindow;
470
+ sitePathPrefix: string;
471
+ }) {
472
+ const { i18n } = useLingui();
473
+ const rows: { key: string | null; label: string; views: number }[] = [
474
+ ...referrers.top.map((row) => ({
475
+ key: row.host,
476
+ label: row.host,
477
+ views: row.views,
478
+ })),
479
+ {
480
+ key: VIEW_INTERNAL_REFERRER,
481
+ label: statsReferrerName(VIEW_INTERNAL_REFERRER, i18n),
482
+ views: referrers.internal,
483
+ },
484
+ { key: "", label: statsReferrerName("", i18n), views: referrers.direct },
485
+ ]
486
+ .filter((row) => row.views > 0)
487
+ .sort((a, b) => b.views - a.views);
488
+ if (referrers.other > 0) {
489
+ rows.push({
490
+ key: null,
491
+ label: i18n._(
492
+ msg({
493
+ message: "Other",
494
+ comment:
495
+ "@context: Row on the stats page for every referrer past the ten listed",
496
+ }),
497
+ ),
498
+ views: referrers.other,
499
+ });
500
+ }
501
+
502
+ return (
503
+ <section class="settings-stats-section">
504
+ <h2 class="settings-stats-heading">
505
+ {i18n._(
506
+ msg({
507
+ message: "Referrers",
508
+ comment: "@context: Heading of the referrer list on the stats page",
509
+ }),
510
+ )}
511
+ </h2>
512
+ <ol class="settings-stats-list">
513
+ {rows.map((row, index) => (
514
+ <RankedRow
515
+ key={row.label}
516
+ rank={index + 1}
517
+ share={shareOf(row.views, total)}
518
+ views={row.views}
519
+ >
520
+ {row.key === null ? (
521
+ <span class="settings-stats-label">{row.label}</span>
522
+ ) : (
523
+ <a
524
+ href={statsHref(sitePathPrefix, days, { referrer: row.key })}
525
+ class="settings-stats-label settings-stats-link"
526
+ >
527
+ {row.label}
528
+ </a>
529
+ )}
530
+ </RankedRow>
531
+ ))}
532
+ </ol>
533
+ </section>
534
+ );
535
+ }
536
+
537
+ /** The most-viewed pages of the window, each a link to its own numbers. */
538
+ function PageList({
539
+ pages,
540
+ total,
541
+ days,
542
+ sitePathPrefix,
543
+ }: {
544
+ pages: StatsPageRow[];
545
+ total: number;
546
+ days: StatsWindow;
547
+ sitePathPrefix: string;
548
+ }) {
549
+ const { i18n } = useLingui();
550
+
551
+ return (
552
+ <section class="settings-stats-section">
553
+ <h2 class="settings-stats-heading">
554
+ {i18n._(
555
+ msg({
556
+ message: "Pages",
557
+ comment:
558
+ "@context: Heading of the most-viewed pages on the stats page",
559
+ }),
560
+ )}
561
+ </h2>
562
+ <ol class="settings-stats-list">
563
+ {pages.map((row, index) => (
564
+ <RankedRow
565
+ key={row.key}
566
+ rank={index + 1}
567
+ share={shareOf(row.views, total)}
568
+ views={row.views}
569
+ >
570
+ <a
571
+ href={statsHref(sitePathPrefix, days, { page: row.key })}
572
+ class="settings-stats-label settings-stats-link"
573
+ >
574
+ {statsPageName(row, i18n)}
575
+ </a>
576
+ <PageBadge page={row} />
577
+ </RankedRow>
578
+ ))}
579
+ </ol>
580
+ </section>
581
+ );
582
+ }
583
+
584
+ function Toggle({
585
+ enabled,
586
+ sitePathPrefix,
587
+ demoMode,
588
+ }: {
589
+ enabled: boolean;
590
+ sitePathPrefix: string;
591
+ demoMode: boolean;
592
+ }) {
593
+ const { i18n } = useLingui();
594
+ const endpoint = toPublicPath("/settings/stats", sitePathPrefix);
595
+ const confirmOptions = JSON.stringify({
596
+ message: i18n._(
597
+ msg({
598
+ message: "Turn off stats and delete every count? This can't be undone.",
599
+ comment:
600
+ "@context: Confirmation before turning off stats, which deletes the counts",
601
+ }),
602
+ ),
603
+ confirmLabel: i18n._(
604
+ msg({
605
+ message: "Turn Off",
606
+ comment: "@context: Confirm button for turning off stats",
607
+ }),
608
+ ),
609
+ cancelLabel: i18n._(
610
+ msg({
611
+ message: "Cancel",
612
+ comment: "@context: Cancel button in the confirm dialog",
613
+ }),
614
+ ),
615
+ tone: "danger",
616
+ });
617
+ // Turning on saves at once. Turning off asks first, since it deletes; a
618
+ // cancelled dialog puts the box back.
619
+ const onChange = `(async () => { if ($enabled) { @post('${endpoint}'); return; } if (await jantConfirm(${confirmOptions})) { @post('${endpoint}') } else { $enabled = true } })()`;
620
+
621
+ return (
622
+ <section
623
+ class="settings-stats-section"
624
+ data-signals={JSON.stringify({ enabled })}
625
+ >
626
+ <label class="flex items-center gap-2 cursor-pointer">
627
+ <input
628
+ type="checkbox"
629
+ class="checkbox"
630
+ checked={enabled}
631
+ disabled={demoMode}
632
+ data-bind="enabled"
633
+ data-on:change={onChange}
634
+ />
635
+ <span>
636
+ {i18n._(
637
+ msg({
638
+ message: "Count page views",
639
+ comment: "@context: Checkbox label on the stats page",
640
+ }),
641
+ )}
642
+ </span>
643
+ </label>
644
+ <p class="text-sm text-muted-foreground">
645
+ {demoMode
646
+ ? i18n._(
647
+ msg({
648
+ message: "Stats settings are locked in demo mode.",
649
+ comment:
650
+ "@context: Note under the stats checkbox when demo restrictions apply",
651
+ }),
652
+ )
653
+ : i18n._(
654
+ msg({
655
+ message:
656
+ "Counts are kept for {days} days and store nothing about any reader. Turning this off deletes them.",
657
+ comment:
658
+ "@context: Help text under the stats checkbox; {days} is the retention window",
659
+ }),
660
+ { days: VIEW_COUNT_RETENTION_DAYS },
661
+ )}
662
+ </p>
663
+ </section>
664
+ );
665
+ }
666
+
667
+ /**
668
+ * The total line. Today says so. While the record began inside a longer
669
+ * window it names the day, since "the last 30 days" would claim days that
670
+ * were never counted.
671
+ */
672
+ function TotalLine({
673
+ shown,
674
+ locale,
675
+ }: {
676
+ shown: Pick<StatsOverview, "days" | "total" | "start">;
677
+ locale: string;
678
+ }) {
679
+ const { i18n } = useLingui();
680
+ if (shown.days === 1) {
681
+ return (
682
+ <p class="settings-stats-total">
683
+ {i18n._(
684
+ msg({
685
+ message: "{count, plural, one {# view} other {# views}} today",
686
+ comment: "@context: Total line on the stats page for today",
687
+ }),
688
+ { count: shown.total },
689
+ )}
690
+ </p>
691
+ );
692
+ }
693
+ return (
694
+ <p class="settings-stats-total">
695
+ {shown.start
696
+ ? i18n._(
697
+ msg({
698
+ message:
699
+ "{count, plural, one {# view} other {# views}} since {date}",
700
+ comment:
701
+ "@context: Total line on the stats page while counting began inside the window; {date} is a day like Nov 9",
702
+ }),
703
+ { count: shown.total, date: formatDayLabel(shown.start, locale) },
704
+ )
705
+ : i18n._(
706
+ msg({
707
+ message:
708
+ "{count, plural, one {# view} other {# views}} in the last {days} days",
709
+ comment:
710
+ "@context: Total line on the stats page; {days} is 7 or 30",
711
+ }),
712
+ { count: shown.total, days: shown.days },
713
+ )}
714
+ </p>
715
+ );
716
+ }
717
+
718
+ /** The heading of one page's numbers: its name, linked to it while it exists, and its kind. */
719
+ function PageHeading({
720
+ page,
721
+ sitePathPrefix,
722
+ }: {
723
+ page: StatsPage;
724
+ sitePathPrefix: string;
725
+ }) {
726
+ const { i18n } = useLingui();
727
+ const name = statsPageName(page, i18n);
728
+ const path = pagePublicPath(page);
729
+ return (
730
+ <h1 class="settings-stats-title">
731
+ {path ? (
732
+ <a
733
+ href={toPublicPath(path, sitePathPrefix)}
734
+ class="settings-stats-title-name settings-stats-link"
735
+ >
736
+ {name}
737
+ </a>
738
+ ) : (
739
+ <span class="settings-stats-title-name">{name}</span>
740
+ )}
741
+ <PageBadge page={page} />
742
+ </h1>
743
+ );
744
+ }
745
+
746
+ /** The heading of one referrer's numbers: its name, as the list wrote it. */
747
+ function ReferrerHeading({ referrer }: { referrer: string }) {
748
+ const { i18n } = useLingui();
749
+ return (
750
+ <h1 class="settings-stats-title">
751
+ <span class="settings-stats-title-name">
752
+ {statsReferrerName(referrer, i18n)}
753
+ </span>
754
+ </h1>
755
+ );
756
+ }
757
+
758
+ export function StatsContent({
759
+ enabled,
760
+ days,
761
+ overview,
762
+ page,
763
+ referrer,
764
+ sitePathPrefix,
765
+ demoMode,
766
+ }: StatsContentProps) {
767
+ const { i18n } = useLingui();
768
+ const locale = i18n.locale;
769
+ const shown = page ?? referrer ?? overview;
770
+ // The site and a referrer list pages; the site and a page list referrers.
771
+ const pages = overview?.pages ?? referrer?.pages ?? null;
772
+ const referrers = overview?.referrers ?? page?.referrers ?? null;
773
+ const target: StatsTarget | undefined = page
774
+ ? { page: page.page.key }
775
+ : referrer
776
+ ? { referrer: referrer.referrer }
777
+ : undefined;
778
+
779
+ return (
780
+ <div class="settings-stats">
781
+ <header class="settings-stats-header">
782
+ {page ? (
783
+ <PageHeading page={page.page} sitePathPrefix={sitePathPrefix} />
784
+ ) : referrer ? (
785
+ <ReferrerHeading referrer={referrer.referrer} />
786
+ ) : (
787
+ <h1 class="settings-stats-title">
788
+ {i18n._(
789
+ msg({
790
+ message: "Stats",
791
+ comment: "@context: Stats page title",
792
+ }),
793
+ )}
794
+ </h1>
795
+ )}
796
+ {enabled && (
797
+ <WindowSwitch
798
+ days={days}
799
+ sitePathPrefix={sitePathPrefix}
800
+ target={target}
801
+ />
802
+ )}
803
+ </header>
804
+
805
+ {!enabled ? (
806
+ <p class="text-sm text-muted-foreground">
807
+ {i18n._(
808
+ msg({
809
+ message: "Stats are off. Turn them on below to start counting.",
810
+ comment: "@context: Stats page body when counting is turned off",
811
+ }),
812
+ )}
813
+ </p>
814
+ ) : (
815
+ shown && (
816
+ <>
817
+ <TotalLine shown={shown} locale={locale} />
818
+ {shown.series && (
819
+ <ViewsChart
820
+ series={shown.series}
821
+ start={shown.start}
822
+ locale={locale}
823
+ />
824
+ )}
825
+ {/* The total line has already said there is nothing to list. */}
826
+ {shown.total > 0 && pages && (
827
+ <PageList
828
+ pages={pages}
829
+ total={shown.total}
830
+ days={days}
831
+ sitePathPrefix={sitePathPrefix}
832
+ />
833
+ )}
834
+ {shown.total > 0 && referrers && (
835
+ <ReferrerList
836
+ referrers={referrers}
837
+ total={shown.total}
838
+ days={days}
839
+ sitePathPrefix={sitePathPrefix}
840
+ />
841
+ )}
842
+ </>
843
+ )
844
+ )}
845
+
846
+ {!target && (
847
+ <Toggle
848
+ enabled={enabled}
849
+ sitePathPrefix={sitePathPrefix}
850
+ demoMode={demoMode}
851
+ />
852
+ )}
853
+ </div>
854
+ );
855
+ }