@simmalugnt-se/payload-consent 0.6.6 → 0.6.8

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,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.8
4
+
5
+ - Add to service on a scan's finding gives the row the cookie's description from the Open Cookie
6
+ Database. Before, the description only became a new service's purpose, so a cookie added to an
7
+ existing service had none.
8
+ - The finding's texts go in English, the database's language, when the site has English, with the
9
+ duration in Swedish too ("1 år", "13 månader", "Session"); the Swedish description falls back
10
+ to English until someone writes it. Before, they went in Admin's language, so a Swedish Admin
11
+ stored "1 year" as the Swedish duration and left the English empty.
12
+ - A cookie the scan saw on another site's domain is added as third party.
13
+ - A Cookie service's collapsed rows say what they are: "_ga · Cookie · 2 år" for a cookie or key,
14
+ the host for a host, in place of "Cookie or key 01". A project regenerates its import map for
15
+ the two new row label components.
16
+
17
+ ## 0.6.7
18
+
19
+ - The help under Site address and Scan every month stays out of the site's generated types. It
20
+ names the site's own address and scan day, so `payload-types.ts` differed by environment and
21
+ site, and changed when the types were generated elsewhere. The help is now a function, which
22
+ Payload renders in Admin but leaves out of the types; Admin shows the same text.
23
+
3
24
  ## 0.6.6
4
25
 
5
26
  - A scan without a Site address scans `scanner.callbackBaseUrl`, then `serverURL`. Before, it took
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A collapsed cookie or storage row of a Cookie service, by what it is: "_ga · Cookie · 2 år".
3
+ * A row of text, since Payload's row labels take a component and none of its own fits.
4
+ */
5
+ export declare function StorageRowLabel(): import("react").JSX.Element;
6
+ /** A collapsed host row of a Cookie service, by its host. */
7
+ export declare function HostRowLabel(): import("react").JSX.Element;
@@ -0,0 +1,41 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useRowLabel, useTranslation } from "@payloadcms/ui";
4
+ const COPY = {
5
+ en: { storage: "Cookie or key", host: "Host", thirdParty: "third party" },
6
+ sv: { storage: "Cookie eller nyckel", host: "Värd", thirdParty: "tredje part" },
7
+ };
8
+ const TYPES = {
9
+ cookie: "Cookie",
10
+ localStorage: "localStorage",
11
+ sessionStorage: "sessionStorage",
12
+ };
13
+ function useLanguage() {
14
+ const { i18n } = useTranslation();
15
+ return i18n.language === "sv" ? "sv" : "en";
16
+ }
17
+ /** Payload's own label for a row with nothing in it yet, such as "Host 01". */
18
+ const numbered = (label, rowNumber) => `${label} ${String((rowNumber ?? 0) + 1).padStart(2, "0")}`;
19
+ /**
20
+ * A collapsed cookie or storage row of a Cookie service, by what it is: "_ga · Cookie · 2 år".
21
+ * A row of text, since Payload's row labels take a component and none of its own fits.
22
+ */
23
+ export function StorageRowLabel() {
24
+ const copy = COPY[useLanguage()];
25
+ const { data, rowNumber } = useRowLabel();
26
+ const name = data?.name?.trim();
27
+ if (!name)
28
+ return _jsx("span", { children: numbered(copy.storage, rowNumber) });
29
+ const details = [
30
+ data?.type ? (TYPES[data.type] ?? data.type) : null,
31
+ data?.duration?.trim() || null,
32
+ data?.thirdParty ? copy.thirdParty : null,
33
+ ].filter(Boolean);
34
+ return _jsx("span", { children: [name, ...details].join(" · ") });
35
+ }
36
+ /** A collapsed host row of a Cookie service, by its host. */
37
+ export function HostRowLabel() {
38
+ const copy = COPY[useLanguage()];
39
+ const { data, rowNumber } = useRowLabel();
40
+ return _jsx("span", { children: data?.host?.trim() || numbered(copy.host, rowNumber) });
41
+ }
@@ -507,9 +507,12 @@ function FindingRow({ api, copy, finding, language, locale, onAdded, }) {
507
507
  service,
508
508
  category,
509
509
  provider: suggestion?.provider,
510
+ // The finding's own description: the row's text, and a new service's purpose.
510
511
  purpose: suggestion?.description,
512
+ description: suggestion?.description,
511
513
  duration: suggestion?.retention,
512
514
  policyUrl: suggestion?.privacyUrl,
515
+ firstParty: finding.firstParty,
513
516
  locale,
514
517
  }),
515
518
  });
@@ -1,2 +1,3 @@
1
+ export { HostRowLabel, StorageRowLabel } from "../admin/RowLabels.tsx";
1
2
  export { ScanPanel } from "../admin/ScanPanel.tsx";
2
3
  export { ConsentLogPanel, ScanLinks, ServicesPanel } from "../admin/ServicesPanel.tsx";
@@ -1,2 +1,3 @@
1
+ export { HostRowLabel, StorageRowLabel } from "../admin/RowLabels.js";
1
2
  export { ScanPanel } from "../admin/ScanPanel.js";
2
3
  export { ConsentLogPanel, ScanLinks, ServicesPanel } from "../admin/ServicesPanel.js";
package/dist/global.js CHANGED
@@ -48,6 +48,12 @@ const necessaryOnly = {
48
48
  },
49
49
  condition: (data) => !hasOptional(data),
50
50
  };
51
+ /**
52
+ * A help text that depends on the site, such as its address or its scan day, as a function:
53
+ * Payload leaves functions out of the generated types, so `payload-types.ts` stays the same in
54
+ * every environment and on every site.
55
+ */
56
+ const siteDescription = (texts) => ({ i18n }) => i18n.language === "sv" ? texts.sv : texts.en;
51
57
  export function consentGlobal(options, { usersSlug, schedule }) {
52
58
  const time = `${String(schedule.hour).padStart(2, "0")}:${String(schedule.minute).padStart(2, "0")}`;
53
59
  const legal = options.legalPages;
@@ -202,7 +208,7 @@ export function consentGlobal(options, { usersSlug, schedule }) {
202
208
  label: { en: "Site address", sv: "Webbplatsens adress" },
203
209
  admin: {
204
210
  placeholder: fallbackAddress ?? "https://",
205
- description: fallbackAddress
211
+ description: siteDescription(fallbackAddress
206
212
  ? {
207
213
  en: `The published site's start page. Empty: ${fallbackAddress}, where the scanner sends its results.`,
208
214
  sv: `Den publicerade webbplatsens startsida. Tom: ${fallbackAddress}, dit scannern skickar resultaten.`,
@@ -210,7 +216,7 @@ export function consentGlobal(options, { usersSlug, schedule }) {
210
216
  : {
211
217
  en: "The published site's start page. Empty: Payload's serverURL.",
212
218
  sv: "Den publicerade webbplatsens startsida. Tom: Payloads serverURL.",
213
- },
219
+ }),
214
220
  },
215
221
  },
216
222
  {
@@ -219,10 +225,10 @@ export function consentGlobal(options, { usersSlug, schedule }) {
219
225
  defaultValue: true,
220
226
  label: { en: "Scan every month", sv: "Skanna varje månad" },
221
227
  admin: {
222
- description: {
228
+ description: siteDescription({
223
229
  en: `On day ${schedule.day} of each month, the first time the host's cron runs Payload's jobs after ${time} server time.`,
224
230
  sv: `Den ${schedule.day}:e varje månad, första gången värdens cron kör Payloads jobb efter kl. ${time} servertid.`,
225
- },
231
+ }),
226
232
  },
227
233
  },
228
234
  {
@@ -0,0 +1,3 @@
1
+ type Language = "en" | "sv";
2
+ export declare function retentionIn(retention: string | undefined, language: Language): string | undefined;
3
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A retention from the Open Cookie Database ("1 year", "13 months", "session") in the site's
3
+ * languages. The database writes English; Swedish is worded here. A retention in any other form
4
+ * comes back as it was in English and as nothing in Swedish, which then falls back to English.
5
+ */
6
+ const UNITS = {
7
+ second: { en: ["second", "seconds"], sv: ["sekund", "sekunder"] },
8
+ minute: { en: ["minute", "minutes"], sv: ["minut", "minuter"] },
9
+ hour: { en: ["hour", "hours"], sv: ["timme", "timmar"] },
10
+ day: { en: ["day", "days"], sv: ["dag", "dagar"] },
11
+ week: { en: ["week", "weeks"], sv: ["vecka", "veckor"] },
12
+ month: { en: ["month", "months"], sv: ["månad", "månader"] },
13
+ year: { en: ["year", "years"], sv: ["år", "år"] },
14
+ };
15
+ export function retentionIn(retention, language) {
16
+ const text = retention?.trim();
17
+ if (!text)
18
+ return undefined;
19
+ if (/^(end of )?session( \(browser\))?$/i.test(text))
20
+ return "Session";
21
+ const match = text.match(/^(\d+)\s*(second|minute|hour|day|week|month|year)s?$/i);
22
+ if (!match)
23
+ return language === "en" ? text : undefined;
24
+ const count = Number(match[1]);
25
+ const [one, many] = UNITS[match[2].toLowerCase()][language];
26
+ return `${count} ${count === 1 ? one : many}`;
27
+ }
package/dist/scans.js CHANGED
@@ -4,6 +4,7 @@ import { CATEGORIES } from "./defaults.js";
4
4
  import { evaluateScan, scanProblems, } from "./evaluate.js";
5
5
  import { evidenceEndpoint } from "./evidence.js";
6
6
  import { notifyScan } from "./notify.js";
7
+ import { retentionIn } from "./retention.js";
7
8
  import { readConsent, toConsentConfig, } from "./site-config.js";
8
9
  import { CONSENT_GLOBAL, CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, CONSENT_SERVICES, MAX_SCAN_PAGES, POLICY_COOKIE, SCAN_TASK, } from "./types.js";
9
10
  /**
@@ -407,13 +408,16 @@ function endpoints(options) {
407
408
  !serviceName ||
408
409
  (kind !== "cookie" && kind !== "localStorage" && kind !== "host"))
409
410
  return Response.json({ error: "Name the finding and the service" }, { status: 400 });
410
- // The site's own languages only; anything else writes the default one.
411
- const localization = req.payload.config.localization;
412
- const locale = localization &&
413
- localization.localeCodes.includes(String(body?.locale)) &&
414
- typeof body?.locale === "string"
415
- ? body.locale
416
- : undefined;
411
+ // The suggestion's texts are the Open Cookie Database's English, so they go in English when
412
+ // the site has it, else in the editor's language of the site, else in the default one.
413
+ const localeCodes = req.payload.config.localization
414
+ ? req.payload.config.localization.localeCodes
415
+ : [];
416
+ const locale = localeCodes.includes("en")
417
+ ? "en"
418
+ : localeCodes.includes(String(body?.locale)) && typeof body?.locale === "string"
419
+ ? body.locale
420
+ : undefined;
417
421
  const category = CATEGORIES.includes(body?.category)
418
422
  ? body?.category
419
423
  : "necessary";
@@ -432,7 +436,13 @@ function endpoints(options) {
432
436
  : {
433
437
  storage: [
434
438
  ...(existing?.storage ?? []),
435
- { name, type: kind, duration: body?.duration || undefined, thirdParty: false },
439
+ {
440
+ name,
441
+ type: kind,
442
+ duration: retentionIn(body?.duration, "en"),
443
+ description: body?.description?.trim() || undefined,
444
+ thirdParty: body?.firstParty === false,
445
+ },
436
446
  ],
437
447
  };
438
448
  const service = existing
@@ -457,6 +467,29 @@ function endpoints(options) {
457
467
  locale: locale,
458
468
  ...asEditor,
459
469
  });
470
+ // The duration in Swedish too, on the row just added; its description falls back to English.
471
+ const swedish = retentionIn(body?.duration, "sv");
472
+ if (kind !== "host" && swedish && locale === "en" && localeCodes.includes("sv")) {
473
+ const inSwedish = (await req.payload.findByID({
474
+ collection: CONSENT_SERVICES,
475
+ id: service.id,
476
+ depth: 0,
477
+ locale: "sv",
478
+ ...asEditor,
479
+ }));
480
+ const rows = inSwedish.storage ?? [];
481
+ // The last row of that name: the one just added, should the service list it twice.
482
+ const added = rows.reduce((last, row, index) => (row.name === name && row.type === kind ? index : last), -1);
483
+ await req.payload.update({
484
+ collection: CONSENT_SERVICES,
485
+ id: service.id,
486
+ data: {
487
+ storage: rows.map((row, index) => index === added ? { ...row, duration: swedish } : row),
488
+ },
489
+ locale: "sv",
490
+ ...asEditor,
491
+ });
492
+ }
460
493
  const latest = await latestDone(req);
461
494
  if (latest) {
462
495
  const previous = await latestDone(req, latest.id);
package/dist/services.js CHANGED
@@ -187,6 +187,7 @@ export function servicesCollection(options) {
187
187
  },
188
188
  admin: {
189
189
  initCollapsed: true,
190
+ components: { RowLabel: "@simmalugnt-se/payload-consent/admin#StorageRowLabel" },
190
191
  description: {
191
192
  en: "What the service stores in the browser. A service that stores nothing, such as an embedded font, has none.",
192
193
  sv: "Det tjänsten sparar i webbläsaren. En tjänst som inte sparar något, till exempel ett inbäddat typsnitt, har inga.",
@@ -247,7 +248,10 @@ export function servicesCollection(options) {
247
248
  singular: { en: "Host", sv: "Värd" },
248
249
  plural: { en: "Hosts", sv: "Värdar" },
249
250
  },
250
- admin: { initCollapsed: true },
251
+ admin: {
252
+ initCollapsed: true,
253
+ components: { RowLabel: "@simmalugnt-se/payload-consent/admin#HostRowLabel" },
254
+ },
251
255
  fields: [
252
256
  {
253
257
  name: "host",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simmalugnt-se/payload-consent",
3
- "version": "0.6.6",
3
+ "version": "0.6.8",
4
4
  "description": "Cookie consent for Payload sites: banner texts, services and scans in Admin",
5
5
  "keywords": [
6
6
  "payload",