@simmalugnt-se/payload-consent 0.6.3 → 0.6.4

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.4
4
+
5
+ - The Scan tab's time left counts down. It grew while a page took its time, since it was worked
6
+ out each second from the scan's start, start-up included, and the pages already done; it now
7
+ takes the pace between pages as the scanner begins them, counts down until the next one and
8
+ says "almost done" once the pace is spent. The card asks every two seconds instead of four.
9
+ - A scan that ends no longer flashes "Waiting for the scanner": while the result is on its way
10
+ from the scanner, the card says it is saving the result, with a full bar. The result then fades
11
+ in with a "Just finished" pill, so it does not look like the scan before it.
12
+ - The README says that `consent-scanner` binds each token to its site's hosts and refuses scans
13
+ and callbacks for other hosts, which hosts to give it, and that it treats a site with and
14
+ without `www.` as one, so a Site address without `www.` still finds the pages on `www.`
15
+ (`consent-scanner` at `0b7aba1` or later).
16
+
3
17
  ## 0.6.3
4
18
 
5
19
  - The Scan tab's findings follow the card above them. Each group (Needs action, Not in the Cookie
package/README.md CHANGED
@@ -130,6 +130,13 @@ consentPlugin({
130
130
  })
131
131
  ```
132
132
 
133
+ The scanner binds each token to its site's hosts (`token@example.se|example.vercel.app` in its
134
+ `SCANNER_TOKENS`), and refuses a scan whose address or callback is on another host: give it the
135
+ host of the scanned address and, when results come back elsewhere (`serverURL` or
136
+ `callbackBaseUrl`), that host too. A host covers itself with and without `www.`, and the scanner
137
+ treats a site with and without `www.` as one, so a Site address of `https://example.se` still finds
138
+ the pages on `https://www.example.se`. Both need `consent-scanner` at `0b7aba1` or later.
139
+
133
140
  The scanner picks the pages to visit from the site's sitemaps. A site with search indexing off
134
141
  often serves empty ones, and then only the start page is scanned. With `scanner.pages`, a scan
135
142
  sends the published documents of those collections instead: read as a visitor would, in the
@@ -1,10 +1,10 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { Button, Collapsible, Pill, SelectInput, TextInput, useConfig, useFormFields, useLocale, useTranslation, } from "@payloadcms/ui";
4
- import { useCallback, useEffect, useState } from "react";
4
+ import { useCallback, useEffect, useRef, useState, } from "react";
5
5
  import { CATEGORIES } from "../defaults.js";
6
6
  import { CONSENT_SCANS } from "../types.js";
7
- import { describeProblem, estimateLeft, findingHeading, finishedPages, formatElapsed, pagePath, scanVerdict, } from "./scan-view.js";
7
+ import { describeProblem, estimateLeft, findingHeading, finishedPages, formatElapsed, markPace, pagePath, scanVerdict, } from "./scan-view.js";
8
8
  import "../styles/admin.css";
9
9
  const COPY = {
10
10
  en: {
@@ -16,6 +16,7 @@ const COPY = {
16
16
  none: "Not scanned",
17
17
  queued: "Waiting",
18
18
  running: "Scanning",
19
+ finishing: "Saving",
19
20
  failed: "Failed",
20
21
  action: "Needs action",
21
22
  unknown: "Not explained",
@@ -24,10 +25,12 @@ const COPY = {
24
25
  clean: "In order",
25
26
  },
26
27
  newPill: (n) => `${n} new`,
28
+ justFinished: "Just finished",
27
29
  title: {
28
30
  none: "The site has not been scanned yet",
29
31
  queued: "Waiting for the scanner",
30
32
  running: "Scanning the site",
33
+ finishing: "Saving the result",
31
34
  failed: "The scan failed",
32
35
  action: (n) => `${n} ${n === 1 ? "needs" : "need"} action`,
33
36
  unknown: (n) => `${n} not in the Cookie services`,
@@ -39,6 +42,8 @@ const COPY = {
39
42
  none: "Scan now to see which cookies, storage and other sites the published site uses, before and after the visitor's choice.",
40
43
  queued: (position) => position ? `Number ${position} in line.` : "It starts when the scanner is free.",
41
44
  choosing: "Choosing the pages to visit…",
45
+ finishing: "The scan is done. Its result is on its way to this site.",
46
+ late: "The result has not arrived. If the scanner could not reach this site, the scan is marked as failed within a day.",
42
47
  action: "Services load where the visitor has not allowed their category.",
43
48
  unknown: "The scan found storage or other sites that no Cookie service explains.",
44
49
  incomplete: "It may have missed something. Scan again once this is fixed:",
@@ -51,6 +56,7 @@ const COPY = {
51
56
  ...(stage.states > 1 ? [`state ${stage.stateNumber} of ${stage.states}`] : []),
52
57
  ].join(" · "),
53
58
  elapsed: (time, left) => (left ? `${time} · about ${left} left` : time),
59
+ almostDone: (time) => `${time} · almost done`,
54
60
  progress: (finished, total) => `${finished} of ${total} page visits done`,
55
61
  stats: {
56
62
  pages: (n, of) => n === of ? (n === 1 ? "page" : "pages") : `of ${of} ${of === 1 ? "page" : "pages"}`,
@@ -128,6 +134,7 @@ const COPY = {
128
134
  none: "Inte skannad",
129
135
  queued: "Väntar",
130
136
  running: "Skannar",
137
+ finishing: "Sparar",
131
138
  failed: "Misslyckades",
132
139
  action: "Åtgärd krävs",
133
140
  unknown: "Okänt",
@@ -136,10 +143,12 @@ const COPY = {
136
143
  clean: "I ordning",
137
144
  },
138
145
  newPill: (n) => `${n} nya`,
146
+ justFinished: "Nyss klar",
139
147
  title: {
140
148
  none: "Webbplatsen har inte skannats än",
141
149
  queued: "Väntar på skannern",
142
150
  running: "Skannar webbplatsen",
151
+ finishing: "Sparar resultatet",
143
152
  failed: "Skanningen misslyckades",
144
153
  action: (n) => `${n} kräver åtgärd`,
145
154
  unknown: (n) => `${n} finns inte bland Cookietjänsterna`,
@@ -151,6 +160,8 @@ const COPY = {
151
160
  none: "Skanna nu för att se vilka cookies, vilken lagring och vilka andra webbplatser den publicerade webbplatsen använder, före och efter besökarens val.",
152
161
  queued: (position) => position ? `Nummer ${position} i kön.` : "Den startar när skannern är ledig.",
153
162
  choosing: "Väljer vilka sidor som ska besökas…",
163
+ finishing: "Skanningen är klar. Resultatet är på väg till webbplatsen.",
164
+ late: "Resultatet har inte kommit. Om skannern inte nådde webbplatsen markeras skanningen som misslyckad inom ett dygn.",
154
165
  action: "Tjänster laddas där besökaren inte har tillåtit kategorin.",
155
166
  unknown: "Skanningen hittade lagring eller andra webbplatser som ingen Cookietjänst förklarar.",
156
167
  incomplete: "Den kan ha missat något. Skanna igen när det här är åtgärdat:",
@@ -163,6 +174,7 @@ const COPY = {
163
174
  ...(stage.states > 1 ? [`läge ${stage.stateNumber} av ${stage.states}`] : []),
164
175
  ].join(" · "),
165
176
  elapsed: (time, left) => (left ? `${time} · ungefär ${left} kvar` : time),
177
+ almostDone: (time) => `${time} · snart klar`,
166
178
  progress: (finished, total) => `${finished} av ${total} sidbesök klara`,
167
179
  stats: {
168
180
  pages: (n, of) => n === of ? (n === 1 ? "sida" : "sidor") : `av ${of} ${of === 1 ? "sida" : "sidor"}`,
@@ -260,6 +272,7 @@ const CARD_PILL = {
260
272
  none: "light-gray",
261
273
  queued: "light-gray",
262
274
  running: "light-gray",
275
+ finishing: "light-gray",
263
276
  failed: "error",
264
277
  action: "error",
265
278
  unknown: "warning",
@@ -277,13 +290,17 @@ const GROUP_PILL = {
277
290
  * The scan at a glance: where it stands or what it found, with Scan now. A panel of our own like
278
291
  * the dashboard widget's: Payload's `Card` is a link, not a status.
279
292
  */
280
- function ScanCard({ active, busy, copy, current, error, language, live, now, onStart, scan, scansUrl, }) {
293
+ function ScanCard({ active, busy, copy, current, error, finishingSince, fresh, language, live, now, onStart, pace, scan, scansUrl, }) {
281
294
  const card = copy.card;
282
295
  const summary = scan?.status === "done" ? scan.summary : undefined;
296
+ // The scanner says a scan is done before the site has saved its result: until then the scan is
297
+ // finishing, not back in the queue.
283
298
  const state = active
284
299
  ? current === "running"
285
300
  ? "running"
286
- : "queued"
301
+ : current === "done" || current === "failed"
302
+ ? "finishing"
303
+ : "queued"
287
304
  : !scan
288
305
  ? "none"
289
306
  : scan.status === "failed"
@@ -297,7 +314,7 @@ function ScanCard({ active, busy, copy, current, error, language, live, now, onS
297
314
  ? card.title.unknown(summary?.unknown ?? 0)
298
315
  : card.title[state];
299
316
  const elapsed = live.startedAt ? now - Date.parse(live.startedAt) : undefined;
300
- const left = live.stage && elapsed !== undefined ? estimateLeft(elapsed, live.stage) : undefined;
317
+ const left = live.stage ? estimateLeft(pace, live.stage.total, now) : undefined;
301
318
  const findings = scan?.findings ?? [];
302
319
  const count = (kind) => findings.filter((f) => f.kind === kind).length;
303
320
  const pages = scan?.pages?.selected ?? [];
@@ -317,17 +334,21 @@ function ScanCard({ active, busy, copy, current, error, language, live, now, onS
317
334
  const took = scan?.startedAt && scan.finishedAt
318
335
  ? formatElapsed(Date.parse(scan.finishedAt) - Date.parse(scan.startedAt))
319
336
  : undefined;
320
- return (_jsxs("section", { className: `consent-scan__card consent-scan__card--${CARD_PILL[state]}`, children: [_jsxs("div", { className: "consent-scan__head", children: [_jsxs("div", { className: "consent-scan__pills", children: [_jsx(Pill, { pillStyle: CARD_PILL[state], size: "small", children: card.pills[state] }), summary?.new ? (_jsx(Pill, { pillStyle: "light", size: "small", children: card.newPill(summary.new) })) : null] }), _jsx(Button, { buttonStyle: state === "none" ? "primary" : "secondary", disabled: busy || active, margin: false, onClick: onStart, size: "small", children: busy ? copy.starting : copy.scan })] }), _jsxs("div", { "aria-live": "polite", className: "consent-scan__body", children: [_jsx("h3", { className: "consent-scan__title", children: title }), state === "none" ? _jsx("p", { children: card.text.none }) : null, state === "queued" ? _jsx("p", { children: card.text.queued(live.position) }) : null, state === "running" ? (_jsx("p", { children: live.stage
337
+ return (_jsxs("section", { className: `consent-scan__card consent-scan__card--${CARD_PILL[state]}${summary && fresh ? " consent-scan__card--fresh" : ""}`, children: [_jsxs("div", { className: "consent-scan__head", children: [_jsxs("div", { className: "consent-scan__pills", children: [_jsx(Pill, { pillStyle: CARD_PILL[state], size: "small", children: card.pills[state] }), summary && fresh ? (_jsx(Pill, { pillStyle: "light", size: "small", children: card.justFinished })) : null, summary?.new ? (_jsx(Pill, { pillStyle: "light", size: "small", children: card.newPill(summary.new) })) : null] }), _jsx(Button, { buttonStyle: state === "none" ? "primary" : "secondary", disabled: busy || active, margin: false, onClick: onStart, size: "small", children: busy ? copy.starting : copy.scan })] }), _jsxs("div", { "aria-live": "polite", className: "consent-scan__body", children: [_jsx("h3", { className: "consent-scan__title", children: title }), state === "none" ? _jsx("p", { children: card.text.none }) : null, state === "queued" ? _jsx("p", { children: card.text.queued(live.position) }) : null, state === "running" ? (_jsx("p", { children: live.stage
321
338
  ? card.stage(capitalize(stateLabel(copy, live.stage.state)), live.stage)
322
339
  : live.progress
323
340
  ? progressLabel(copy, live.progress)
324
- : card.text.choosing })) : null, state === "failed" ? _jsx("p", { children: scan?.error }) : null, state === "action" ||
341
+ : card.text.choosing })) : null, state === "finishing" ? (_jsx("p", { children: finishingSince !== undefined && now - finishingSince > 60_000
342
+ ? card.text.late
343
+ : card.text.finishing })) : null, state === "failed" ? _jsx("p", { children: scan?.error }) : null, state === "action" ||
325
344
  state === "unknown" ||
326
345
  state === "incomplete" ||
327
346
  state === "ok" ||
328
- state === "clean" ? (_jsx("p", { children: card.text[state] })) : null] }), state === "running" && live.stage ? (
347
+ state === "clean" ? (_jsx("p", { children: card.text[state] })) : null] }), (state === "running" || state === "finishing") && live.stage ? (
329
348
  // Payload has no progress bar for content (its ProgressBar follows route changes).
330
- _jsx("progress", { "aria-label": card.progress(finishedPages(live.stage), live.stage.total), className: "consent-scan__progress", max: live.stage.total, value: finishedPages(live.stage) })) : null, state === "running" && elapsed !== undefined ? (_jsx("p", { className: "consent-scan__meta", children: card.elapsed(formatElapsed(elapsed), left === undefined ? undefined : formatElapsed(left)) })) : null, summary?.problems?.length ? (_jsx("ul", { className: "consent-scan__problems", children: summary.problems.map((raw) => {
349
+ _jsx("progress", { "aria-label": card.progress(state === "finishing" ? live.stage.total : finishedPages(live.stage), live.stage.total), className: "consent-scan__progress", max: live.stage.total, value: state === "finishing" ? live.stage.total : finishedPages(live.stage) })) : null, state === "running" && elapsed !== undefined ? (_jsx("p", { className: "consent-scan__meta", children: left !== undefined && left < 1000
350
+ ? card.almostDone(formatElapsed(elapsed))
351
+ : card.elapsed(formatElapsed(elapsed), left === undefined ? undefined : formatElapsed(left)) })) : null, summary?.problems?.length ? (_jsx("ul", { className: "consent-scan__problems", children: summary.problems.map((raw) => {
331
352
  const problem = describeProblem(raw, stateIds);
332
353
  const label = problem.state ? stateLabel(copy, problem.state) : "";
333
354
  const text = problem.kind === "sitemap"
@@ -363,6 +384,10 @@ export function ScanPanel() {
363
384
  const [busy, setBusy] = useState(false);
364
385
  const [error, setError] = useState();
365
386
  const [now, setNow] = useState(() => Date.now());
387
+ // The pace of the scan being watched, from when each page was first seen begun.
388
+ const [pace, setPace] = useState({ marks: [] });
389
+ // The scan the card has seen running or waiting, so its result shows as just finished.
390
+ const watched = useRef(undefined);
366
391
  const load = useCallback(async () => {
367
392
  const response = await fetch(`${api}?sort=-createdAt&limit=1&depth=0`, {
368
393
  credentials: "include",
@@ -373,35 +398,54 @@ export function ScanPanel() {
373
398
  useEffect(() => {
374
399
  load();
375
400
  }, [load]);
376
- // While a scan waits or runs, ask for its progress; reload it when it ends.
401
+ // While a scan waits or runs, ask for its progress; reload it until its result has arrived.
377
402
  useEffect(() => {
378
403
  if (!scan || (scan.status !== "queued" && scan.status !== "running"))
379
404
  return;
405
+ watched.current = scan.id;
380
406
  let stopped = false;
381
407
  const poll = async () => {
382
408
  const response = await fetch(`${api}/${scan.id}/status`, { credentials: "include" });
383
409
  const next = (await response.json().catch(() => ({})));
384
410
  if (stopped)
385
411
  return;
386
- setLive(next);
412
+ const at = Date.now();
413
+ // A finished scan no longer reports its stage: keep the last one for the full bar.
414
+ setLive((previous) => ({
415
+ ...next,
416
+ stage: next.stage ?? previous.stage,
417
+ startedAt: next.startedAt ?? previous.startedAt,
418
+ }));
419
+ setPace((previous) => ({
420
+ id: scan.id,
421
+ marks: markPace(previous.id === scan.id ? previous.marks : [], next.stage, at),
422
+ }));
387
423
  if (next.status === "done" || next.status === "failed")
388
424
  await load();
389
425
  };
390
426
  poll();
391
- const timer = setInterval(poll, 4000);
427
+ const timer = setInterval(poll, 2000);
392
428
  return () => {
393
429
  stopped = true;
394
430
  clearInterval(timer);
395
431
  };
396
432
  }, [api, load, scan]);
397
- // A running scan's clock ticks each second between the scanner's answers.
398
- const running = scan?.status === "running" || live.status === "running";
433
+ const active = scan?.status === "queued" || scan?.status === "running";
434
+ // The scanner knows first when a queued scan starts running, and when it is done.
435
+ const current = active ? (live.status ?? scan?.status) : scan?.status;
436
+ const finishing = active && (current === "done" || current === "failed");
437
+ const [finishingSince, setFinishingSince] = useState();
438
+ useEffect(() => {
439
+ setFinishingSince((since) => (finishing ? (since ?? Date.now()) : undefined));
440
+ }, [finishing]);
441
+ // A waiting or running scan's clock ticks each second between the scanner's answers.
442
+ const ticking = scan?.status === "queued" || scan?.status === "running";
399
443
  useEffect(() => {
400
- if (!running)
444
+ if (!ticking)
401
445
  return;
402
446
  const timer = setInterval(() => setNow(Date.now()), 1000);
403
447
  return () => clearInterval(timer);
404
- }, [running]);
448
+ }, [ticking]);
405
449
  async function start() {
406
450
  setBusy(true);
407
451
  setError(undefined);
@@ -422,11 +466,8 @@ export function ScanPanel() {
422
466
  }
423
467
  if (!hasScanner)
424
468
  return _jsx("p", { className: "consent-scan__note", children: copy.noScanner });
425
- const active = scan?.status === "queued" || scan?.status === "running";
426
- // The scanner knows first when a queued scan starts running.
427
- const current = active ? (live.status ?? scan?.status) : scan?.status;
428
469
  const findings = scan?.findings ?? [];
429
- return (_jsxs("div", { className: "consent-scan", children: [_jsx(ScanCard, { active: active, busy: busy, copy: copy, current: current, error: error, language: language, live: live, now: now, onStart: start, scan: scan ?? null, scansUrl: `${config.routes.admin}/collections/${CONSENT_SCANS}` }), scan?.status === "done"
470
+ return (_jsxs("div", { className: "consent-scan", children: [_jsx(ScanCard, { active: active, busy: busy, copy: copy, current: current, error: error, finishingSince: finishingSince, fresh: Boolean(scan) && watched.current === scan?.id, language: language, live: live, now: now, onStart: start, pace: pace.id === scan?.id ? pace.marks : [], scan: scan ?? null, scansUrl: `${config.routes.admin}/collections/${CONSENT_SCANS}` }), scan?.status === "done"
430
471
  ? ["action", "unknown", "ok"].map((status) => {
431
472
  const rows = findings.filter((finding) => finding.status === status);
432
473
  if (!rows.length)
@@ -28,10 +28,19 @@ export type ScanProblem = {
28
28
  detail: string;
29
29
  };
30
30
  export declare function describeProblem(problem: string, stateIds: string[]): ScanProblem;
31
+ /** When the card first saw each count of pages begun (`stage.visited`), for the pace. */
32
+ export type PaceMark = {
33
+ visited: number;
34
+ at: number;
35
+ };
36
+ /** The marks with the scanner's latest answer, when it has begun a page the card had not seen. */
37
+ export declare function markPace(marks: PaceMark[], stage: ScanStage | undefined, now: number): PaceMark[];
31
38
  /**
32
- * Roughly how long a running scan has left, from the pace so far; `undefined` until a page is
33
- * done, when there is no pace to go by.
39
+ * Roughly how long a running scan has left. The pace is the time between the first and the latest
40
+ * page begun, so the scan's start-up is left out, and the estimate made when the latest page began
41
+ * counts down until the next one rather than grow while a page takes its time. Undefined until two
42
+ * pages have begun; 0 once the pace says the scan should be done, as it ends with WEC's report.
34
43
  */
35
- export declare function estimateLeft(elapsedMs: number, stage: ScanStage): number | undefined;
44
+ export declare function estimateLeft(marks: PaceMark[], total: number, now: number): number | undefined;
36
45
  /** `/about?x=1` for a page on the scanned site: the address without its origin. */
37
46
  export declare function pagePath(url: string): string;
@@ -44,15 +44,27 @@ export function describeProblem(problem, stateIds) {
44
44
  return { kind: "outOfTime", state, detail };
45
45
  return { kind: "pagesFailed", state, detail };
46
46
  }
47
+ /** The marks with the scanner's latest answer, when it has begun a page the card had not seen. */
48
+ export function markPace(marks, stage, now) {
49
+ const last = marks.at(-1);
50
+ if (!stage || stage.visited < 1 || (last && last.visited >= stage.visited))
51
+ return marks;
52
+ return [...marks, { visited: stage.visited, at: now }];
53
+ }
47
54
  /**
48
- * Roughly how long a running scan has left, from the pace so far; `undefined` until a page is
49
- * done, when there is no pace to go by.
55
+ * Roughly how long a running scan has left. The pace is the time between the first and the latest
56
+ * page begun, so the scan's start-up is left out, and the estimate made when the latest page began
57
+ * counts down until the next one rather than grow while a page takes its time. Undefined until two
58
+ * pages have begun; 0 once the pace says the scan should be done, as it ends with WEC's report.
50
59
  */
51
- export function estimateLeft(elapsedMs, stage) {
52
- const finished = finishedPages(stage);
53
- if (finished < 1 || elapsedMs <= 0)
60
+ export function estimateLeft(marks, total, now) {
61
+ const first = marks[0];
62
+ const last = marks.at(-1);
63
+ if (!first || !last || last.visited <= first.visited)
54
64
  return undefined;
55
- return (elapsedMs / finished) * Math.max(0, stage.total - finished);
65
+ const pace = (last.at - first.at) / (last.visited - first.visited);
66
+ // The latest page has only begun: it is left too, with every page after it.
67
+ return Math.max(0, pace * (total - last.visited + 1) - (now - last.at));
56
68
  }
57
69
  /** `/about?x=1` for a page on the scanned site: the address without its origin. */
58
70
  export function pagePath(url) {
@@ -24,6 +24,21 @@
24
24
  .consent-scan__card--warning {
25
25
  border-color: var(--theme-warning-500);
26
26
  }
27
+ /* A result that arrived while the card watched the scan: it fades in rather than swap in place. */
28
+ .consent-scan__card--fresh {
29
+ animation: consent-scan-fresh 400ms ease-out;
30
+ }
31
+ @keyframes consent-scan-fresh {
32
+ from {
33
+ opacity: 0.35;
34
+ transform: translateY(4px);
35
+ }
36
+ }
37
+ @media (prefers-reduced-motion: reduce) {
38
+ .consent-scan__card--fresh {
39
+ animation: none;
40
+ }
41
+ }
27
42
  .consent-scan__head {
28
43
  display: flex;
29
44
  align-items: center;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simmalugnt-se/payload-consent",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Cookie consent for Payload sites: banner texts, services and scans in Admin",
5
5
  "keywords": [
6
6
  "payload",