@simmalugnt-se/payload-consent 0.0.0-stage → 0.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 (46) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +21 -0
  3. package/README.md +242 -2
  4. package/dist/admin/ScanPanel.d.ts +3 -0
  5. package/dist/admin/ScanPanel.js +232 -0
  6. package/dist/admin/ServicesPanel.d.ts +11 -0
  7. package/dist/admin/ServicesPanel.js +136 -0
  8. package/dist/block.d.ts +6 -0
  9. package/dist/block.js +27 -0
  10. package/dist/defaults.d.ts +56 -0
  11. package/dist/defaults.js +137 -0
  12. package/dist/evaluate.d.ts +106 -0
  13. package/dist/evaluate.js +101 -0
  14. package/dist/evidence.d.ts +12 -0
  15. package/dist/evidence.js +114 -0
  16. package/dist/exports/admin.d.ts +2 -0
  17. package/dist/exports/admin.js +2 -0
  18. package/dist/exports/client.d.ts +37 -0
  19. package/dist/exports/client.js +180 -0
  20. package/dist/exports/rsc.d.ts +1 -0
  21. package/dist/exports/rsc.js +1 -0
  22. package/dist/global.d.ts +14 -0
  23. package/dist/global.js +317 -0
  24. package/dist/index.d.ts +13 -0
  25. package/dist/index.js +75 -0
  26. package/dist/integrations.d.ts +40 -0
  27. package/dist/integrations.js +32 -0
  28. package/dist/notify.d.ts +15 -0
  29. package/dist/notify.js +86 -0
  30. package/dist/records.d.ts +15 -0
  31. package/dist/records.js +198 -0
  32. package/dist/scans.d.ts +37 -0
  33. package/dist/scans.js +424 -0
  34. package/dist/schedule.d.ts +17 -0
  35. package/dist/schedule.js +18 -0
  36. package/dist/services.d.ts +8 -0
  37. package/dist/services.js +258 -0
  38. package/dist/site-config.d.ts +128 -0
  39. package/dist/site-config.js +204 -0
  40. package/dist/styles/admin.css +202 -0
  41. package/dist/styles/consent.css +301 -0
  42. package/dist/types.d.ts +65 -0
  43. package/dist/types.js +17 -0
  44. package/dist/widget/ConsentScanWidget.d.ts +8 -0
  45. package/dist/widget/ConsentScanWidget.js +99 -0
  46. package/package.json +76 -3
@@ -0,0 +1,202 @@
1
+ /* The Scan tab in the Cookies global; Payload's own variables, so both themes work. */
2
+ .consent-scan {
3
+ display: grid;
4
+ gap: calc(var(--base) * 0.75);
5
+ margin-block: var(--base);
6
+ }
7
+ .consent-scan__bar {
8
+ display: flex;
9
+ flex-wrap: wrap;
10
+ align-items: center;
11
+ gap: calc(var(--base) * 0.75);
12
+ }
13
+ .consent-scan__status,
14
+ .consent-scan__summary {
15
+ color: var(--theme-elevation-800);
16
+ }
17
+ .consent-scan__summary {
18
+ margin: 0;
19
+ font-weight: 600;
20
+ }
21
+ .consent-scan__note {
22
+ margin: 0;
23
+ color: var(--theme-elevation-500);
24
+ }
25
+ .consent-scan__error {
26
+ margin: 0;
27
+ color: var(--theme-error-500);
28
+ }
29
+ .consent-scan__group {
30
+ border-top: 1px solid var(--theme-elevation-100);
31
+ padding-top: calc(var(--base) * 0.75);
32
+ }
33
+ .consent-scan__group h4 {
34
+ margin: 0 0 calc(var(--base) * 0.25);
35
+ display: inline;
36
+ }
37
+ .consent-scan__group--action h4 {
38
+ color: var(--theme-error-500);
39
+ }
40
+ .consent-scan__group summary {
41
+ cursor: pointer;
42
+ }
43
+ .consent-scan__list {
44
+ list-style: none;
45
+ margin: calc(var(--base) * 0.5) 0 0;
46
+ padding: 0;
47
+ display: grid;
48
+ gap: calc(var(--base) * 0.5);
49
+ }
50
+ .consent-scan__row {
51
+ display: grid;
52
+ gap: 4px;
53
+ padding: calc(var(--base) * 0.5);
54
+ border: 1px solid var(--theme-elevation-100);
55
+ border-radius: var(--style-radius-s);
56
+ }
57
+ .consent-scan__name {
58
+ display: flex;
59
+ flex-wrap: wrap;
60
+ align-items: center;
61
+ gap: 8px;
62
+ }
63
+ .consent-scan__kind {
64
+ color: var(--theme-elevation-500);
65
+ font-size: 0.85em;
66
+ }
67
+ .consent-scan__where {
68
+ color: var(--theme-elevation-500);
69
+ }
70
+ .consent-scan__detail {
71
+ margin: 0;
72
+ color: var(--theme-elevation-600);
73
+ font-size: 0.9em;
74
+ overflow-wrap: anywhere;
75
+ }
76
+ .consent-scan__classify {
77
+ display: flex;
78
+ flex-wrap: wrap;
79
+ align-items: flex-end;
80
+ gap: 8px;
81
+ margin-top: 4px;
82
+ }
83
+ .consent-scan__classify label {
84
+ display: grid;
85
+ gap: 2px;
86
+ font-size: 0.85em;
87
+ color: var(--theme-elevation-600);
88
+ }
89
+ .consent-scan__classify input,
90
+ .consent-scan__classify select {
91
+ font: inherit;
92
+ color: var(--theme-text);
93
+ background: var(--theme-input-bg);
94
+ border: 1px solid var(--theme-elevation-150);
95
+ border-radius: var(--style-radius-s);
96
+ padding: 6px 8px;
97
+ min-height: 32px;
98
+ }
99
+
100
+ /* The dashboard widget, laid out like Payload's collection cards and content health's widget. */
101
+ .consent-widget {
102
+ display: flex;
103
+ flex-direction: column;
104
+ gap: var(--base);
105
+ width: 100%;
106
+ padding-top: calc(var(--base) - 12px);
107
+ }
108
+
109
+ .consent-widget__heading {
110
+ margin: 0;
111
+ }
112
+
113
+ .consent-widget__panel {
114
+ display: flex;
115
+ flex-direction: column;
116
+ gap: calc(var(--base) * 0.5);
117
+ padding: calc(var(--base) * 1.25);
118
+ border: 1px solid var(--theme-elevation-150);
119
+ border-radius: var(--style-radius-m);
120
+ background: var(--theme-elevation-0);
121
+ }
122
+
123
+ .consent-widget__panel--red {
124
+ border-color: var(--theme-error-500);
125
+ }
126
+
127
+ .consent-widget__top {
128
+ display: flex;
129
+ align-items: center;
130
+ justify-content: space-between;
131
+ gap: var(--base);
132
+ }
133
+
134
+ .consent-widget__lines {
135
+ margin: 0;
136
+ padding: 0;
137
+ list-style: none;
138
+ font-size: 1.05rem;
139
+ }
140
+
141
+ .consent-widget__meta {
142
+ margin: 0;
143
+ color: var(--theme-elevation-600);
144
+ }
145
+
146
+ /* The Services and Consent log tabs on the Cookies page. */
147
+ .consent-panel {
148
+ display: flex;
149
+ flex-direction: column;
150
+ gap: var(--base);
151
+ }
152
+
153
+ .consent-panel__link,
154
+ .consent-panel__more a,
155
+ .consent-panel__links a {
156
+ color: var(--theme-text);
157
+ text-decoration: underline;
158
+ text-underline-offset: 2px;
159
+ }
160
+
161
+ .consent-panel__more {
162
+ margin: 0;
163
+ }
164
+
165
+ .consent-panel__table {
166
+ width: 100%;
167
+ border-collapse: collapse;
168
+ }
169
+
170
+ .consent-panel__table th,
171
+ .consent-panel__table td {
172
+ padding: calc(var(--base) * 0.5) calc(var(--base) * 0.75);
173
+ border-bottom: 1px solid var(--theme-elevation-100);
174
+ text-align: left;
175
+ vertical-align: middle;
176
+ }
177
+
178
+ .consent-panel__table th {
179
+ color: var(--theme-elevation-500);
180
+ font-weight: 500;
181
+ }
182
+
183
+ .consent-panel__table td a {
184
+ color: var(--theme-text);
185
+ font-weight: 500;
186
+ }
187
+
188
+ .consent-panel__links {
189
+ display: flex;
190
+ flex-direction: column;
191
+ gap: calc(var(--base) * 0.75);
192
+ margin: 0;
193
+ padding: 0;
194
+ list-style: none;
195
+ }
196
+
197
+ .consent-panel__links li {
198
+ display: flex;
199
+ flex-wrap: wrap;
200
+ gap: calc(var(--base) * 0.75);
201
+ align-items: baseline;
202
+ }
@@ -0,0 +1,301 @@
1
+ /* Banner and dialog styles over c15t's unstyled components, copied from @simmalugnt-se/consent-ui 0.2.1.
2
+ Sites customize them with the --consent-* variables on :root. */
3
+ /* Theme variables belong on :root because c15t renders through portals. */
4
+ [data-testid="consent-banner-root"],
5
+ [data-testid="consent-dialog-root"] {
6
+ font: 400 14px / 1.5 var(--consent-font, system-ui, sans-serif);
7
+ color: var(--consent-text, #202522);
8
+ text-align: left;
9
+ letter-spacing: normal;
10
+ }
11
+ [data-testid="consent-banner-root"] *,
12
+ [data-testid="consent-dialog-root"] * {
13
+ box-sizing: border-box;
14
+ }
15
+ [data-testid="consent-banner-root"] {
16
+ position: fixed;
17
+ z-index: 999999998;
18
+ left: 20px;
19
+ bottom: 20px;
20
+ width: min(440px, calc(100% - 40px));
21
+ pointer-events: none;
22
+ }
23
+ .sc-card,
24
+ .sc-dialog-card {
25
+ background: var(--consent-surface, #fff);
26
+ border: 1px solid var(--consent-border, #dededb);
27
+ border-radius: var(--consent-radius, 14px);
28
+ box-shadow: 0 8px 40px #0002;
29
+ pointer-events: auto;
30
+ overflow: hidden;
31
+ }
32
+ .sc-header {
33
+ padding: 24px;
34
+ display: grid;
35
+ gap: 10px;
36
+ }
37
+ .sc-title {
38
+ font: 600 18px / 1.35 var(--consent-font, system-ui, sans-serif);
39
+ margin: 0;
40
+ }
41
+ .sc-description {
42
+ font: inherit;
43
+ color: var(--consent-muted, #606660);
44
+ margin: 0;
45
+ }
46
+ .sc-actions {
47
+ display: flex;
48
+ flex-wrap: wrap;
49
+ gap: 10px;
50
+ justify-content: space-between;
51
+ padding: 16px 24px;
52
+ border-top: 1px solid var(--consent-border, #dededb);
53
+ }
54
+ .sc-group {
55
+ display: flex;
56
+ flex-wrap: wrap;
57
+ gap: 10px;
58
+ }
59
+ :is(.sc-actions button) {
60
+ appearance: none;
61
+ background: var(--consent-surface, #fff);
62
+ color: var(--consent-text, #202522);
63
+ border: 1px solid var(--consent-border, #dededb);
64
+ border-radius: 7px;
65
+ padding: 10px 12px;
66
+ min-height: 42px;
67
+ font: 500 14px / 1.4 var(--consent-font, system-ui, sans-serif);
68
+ cursor: pointer;
69
+ text-align: center;
70
+ }
71
+ :is(.sc-actions button):hover {
72
+ background: var(--consent-hover, #f3f5f3);
73
+ }
74
+ :is(.sc-actions button):disabled {
75
+ opacity: 0.5;
76
+ cursor: default;
77
+ }
78
+ [data-testid="consent-banner-root"] button:focus-visible,
79
+ [data-testid="consent-dialog-root"] button:focus-visible,
80
+ [data-testid="consent-banner-root"] a:focus-visible,
81
+ [data-testid="consent-dialog-root"] a:focus-visible {
82
+ outline: 2px solid var(--consent-accent, #006747);
83
+ outline-offset: 3px;
84
+ }
85
+ [data-testid="consent-dialog-overlay"] {
86
+ position: fixed;
87
+ inset: 0;
88
+ z-index: 999999999;
89
+ background: #0006;
90
+ }
91
+ [data-testid="consent-dialog-root"] {
92
+ position: fixed;
93
+ inset: 0;
94
+ z-index: 1000000000;
95
+ display: grid;
96
+ place-items: center;
97
+ padding: 24px;
98
+ overflow-y: auto;
99
+ }
100
+ .sc-dialog-card {
101
+ width: 100%;
102
+ max-width: 500px;
103
+ max-height: calc(100dvh - 48px);
104
+ overflow-y: auto;
105
+ }
106
+ .sc-content {
107
+ padding: 0;
108
+ }
109
+ .sc-categories {
110
+ padding: 0 24px 24px;
111
+ display: grid;
112
+ gap: 12px;
113
+ }
114
+ .sc-categories h3 {
115
+ font: 500 15px / 1.4 var(--consent-font, system-ui, sans-serif);
116
+ margin: 0;
117
+ }
118
+ .sc-categories button {
119
+ font-family: inherit;
120
+ cursor: pointer;
121
+ }
122
+ .sc-categories svg {
123
+ width: 18px;
124
+ height: 18px;
125
+ }
126
+ :is(.sc-categories [role="switch"]) {
127
+ flex: none;
128
+ width: 36px;
129
+ height: 22px;
130
+ border: 0;
131
+ border-radius: 20px;
132
+ padding: 3px;
133
+ background: #c9ceca;
134
+ position: relative;
135
+ }
136
+ :is(.sc-categories [role="switch"])::after {
137
+ content: "";
138
+ display: block;
139
+ width: 16px;
140
+ height: 16px;
141
+ border-radius: 50%;
142
+ background: white;
143
+ transition: transform 0.15s;
144
+ }
145
+ :is(.sc-categories [role="switch"])[aria-checked="true"] {
146
+ background: var(--consent-accent, #006747);
147
+ }
148
+ :is(.sc-categories [role="switch"])[aria-checked="true"]::after {
149
+ transform: translateX(14px);
150
+ }
151
+ :is(.sc-categories [role="switch"]):disabled {
152
+ opacity: 0.5;
153
+ cursor: default;
154
+ }
155
+ :is(.sc-categories [role="switch"]) > * {
156
+ display: none;
157
+ }
158
+ .sc-branding {
159
+ display: flex;
160
+ justify-content: flex-end;
161
+ font: 11px / 1.4 system-ui;
162
+ padding: 6px;
163
+ pointer-events: auto;
164
+ }
165
+ .sc-branding svg {
166
+ height: 14px;
167
+ width: auto;
168
+ }
169
+ @media (max-width: 520px) {
170
+ [data-testid="consent-dialog-root"] {
171
+ padding: 12px;
172
+ }
173
+ .sc-dialog-card {
174
+ max-height: calc(100dvh - 24px);
175
+ }
176
+ .sc-actions,
177
+ .sc-group {
178
+ display: grid;
179
+ grid-template-columns: 1fr;
180
+ }
181
+ .sc-actions > * {
182
+ width: 100%;
183
+ }
184
+ }
185
+ @media (prefers-reduced-motion: reduce) {
186
+ :is(.sc-categories [role="switch"])::after {
187
+ transition: none;
188
+ }
189
+ }
190
+ /* Category structure exposed by c15t 2.2.1. */
191
+ .sc-categories [data-testid^="consent-widget-accordion-item-"] {
192
+ border: 1px solid var(--consent-border, #dededb);
193
+ border-radius: 8px;
194
+ overflow: hidden;
195
+ }
196
+ .sc-categories
197
+ [data-testid^="consent-widget-accordion-trigger-"]:not([data-testid*="trigger-inner"]) {
198
+ display: flex;
199
+ align-items: center;
200
+ gap: 12px;
201
+ padding: 14px;
202
+ }
203
+ .sc-categories [data-testid^="consent-widget-accordion-trigger-inner-"] {
204
+ display: flex;
205
+ align-items: center;
206
+ flex: 1;
207
+ gap: 10px;
208
+ border: 0;
209
+ background: none;
210
+ color: inherit;
211
+ padding: 0;
212
+ text-align: left;
213
+ }
214
+ .sc-categories [data-testid^="consent-widget-accordion-content-"] {
215
+ color: var(--consent-muted, #606660);
216
+ font: inherit;
217
+ }
218
+ .sc-categories [data-testid^="consent-widget-accordion-content-"] > * {
219
+ padding: 0 14px 14px;
220
+ }
221
+
222
+ [data-testid="consent-dialog-root"] {
223
+ margin: auto;
224
+ border: 0;
225
+ background: transparent;
226
+ width: 100%;
227
+ max-width: none;
228
+ height: 100%;
229
+ max-height: none;
230
+ outline: none;
231
+ }
232
+ [data-testid="consent-dialog-root"]::backdrop {
233
+ background: #0006;
234
+ }
235
+ .sc-categories [data-state="closed"][data-testid^="consent-widget-accordion-content-"] {
236
+ display: none;
237
+ }
238
+
239
+ /* The card receives initial focus; interactive controls keep their focus rings. */
240
+ [data-testid="consent-banner-card"]:focus {
241
+ outline: none;
242
+ }
243
+
244
+ /* Embedded content waiting for consent (ConsentFrame). */
245
+ .sc-frame {
246
+ display: grid;
247
+ place-items: center;
248
+ gap: 12px;
249
+ padding: 32px 24px;
250
+ min-height: 200px;
251
+ text-align: center;
252
+ font: 400 14px / 1.5 var(--consent-font, system-ui, sans-serif);
253
+ color: var(--consent-text, #202522);
254
+ background: var(--consent-hover, #f3f5f3);
255
+ border: 1px solid var(--consent-border, #dededb);
256
+ border-radius: var(--consent-radius, 14px);
257
+ }
258
+ .sc-frame p {
259
+ margin: 0;
260
+ max-width: 40ch;
261
+ }
262
+ .sc-frame button {
263
+ appearance: none;
264
+ background: var(--consent-surface, #fff);
265
+ color: var(--consent-text, #202522);
266
+ border: 1px solid var(--consent-border, #dededb);
267
+ border-radius: 7px;
268
+ padding: 10px 12px;
269
+ min-height: 42px;
270
+ font: 500 14px / 1.4 var(--consent-font, system-ui, sans-serif);
271
+ cursor: pointer;
272
+ }
273
+ .sc-frame button:focus-visible {
274
+ outline: 2px solid var(--consent-accent, #006747);
275
+ outline-offset: 3px;
276
+ }
277
+
278
+ /* The cookie table (CookieTable); sites style headings and text with their own styles. */
279
+ .sc-cookie-service {
280
+ margin-block: 16px;
281
+ }
282
+ .sc-cookie-table table {
283
+ width: 100%;
284
+ border-collapse: collapse;
285
+ font-size: 0.9em;
286
+ }
287
+ .sc-cookie-table th,
288
+ .sc-cookie-table td {
289
+ text-align: left;
290
+ padding: 6px 8px;
291
+ border-bottom: 1px solid var(--consent-border, #dededb);
292
+ vertical-align: top;
293
+ }
294
+ .sc-cookie-service:has(table) {
295
+ overflow-x: auto;
296
+ }
297
+
298
+ /* With only necessary storage there is nothing to reject. */
299
+ .sc-dialog--necessary-only [data-testid="consent-widget-reject-button"] {
300
+ display: none;
301
+ }
@@ -0,0 +1,65 @@
1
+ import type { Access, CollectionConfig, CollectionSlug, GlobalConfig } from "payload";
2
+ export type ConsentPluginOptions = {
3
+ disabled?: boolean;
4
+ /** Admin group of the Cookies global. Defaults to Settings. */
5
+ group?: string | Record<string, string>;
6
+ /**
7
+ * Where the privacy and cookie policies live. The global gets a field per policy that picks a
8
+ * document from `collection`; `url` turns the picked document into the link the banner shows.
9
+ */
10
+ legalPages?: {
11
+ collection: CollectionSlug;
12
+ url: (doc: Record<string, unknown>, locale: string | undefined) => string;
13
+ };
14
+ /** Read access from outside Admin, such as the site's own API. Defaults to public. */
15
+ access?: {
16
+ read?: Access;
17
+ update?: Access;
18
+ };
19
+ /** Change the global, for example to add hooks that revalidate the site's cache. */
20
+ overrideGlobal?: (global: GlobalConfig) => GlobalConfig;
21
+ /**
22
+ * Keeps a record of every choice visitors save, with a snapshot of the policy they were shown,
23
+ * to show that consent was given. On by default; `false` turns it off.
24
+ */
25
+ records?: false | {
26
+ retentionDays?: number;
27
+ };
28
+ /**
29
+ * The consent scanner (`consent-scanner`) that scans the published site from Admin. Without it,
30
+ * the Scan tab explains how to set one up.
31
+ */
32
+ scanner?: {
33
+ url: string;
34
+ token: string;
35
+ /**
36
+ * Where the scanner posts results, when it cannot reach the site at `serverURL`; for example
37
+ * `http://host.docker.internal:3000` when it runs in Docker beside a local dev server.
38
+ */
39
+ callbackBaseUrl?: string;
40
+ /**
41
+ * WEC's PDF report and network traffic (HAR) are kept for this many of the latest scans, in
42
+ * `consent-scan-evidence`. Defaults to 12; `0` asks the scanner for none.
43
+ */
44
+ keepEvidence?: number;
45
+ };
46
+ /** Change the services collection, for example to add hooks that revalidate the site's cache. */
47
+ overrideServices?: (collection: CollectionConfig) => CollectionConfig;
48
+ };
49
+ export declare const CONSENT_GLOBAL = "consent";
50
+ export declare const CONSENT_SERVICES = "consent-services";
51
+ export declare const CONSENT_RECORDS = "consent-records";
52
+ export declare const CONSENT_POLICIES = "consent-policies";
53
+ export declare const CONSENT_SCANS = "consent-scans";
54
+ export declare const CONSENT_SCAN_EVIDENCE = "consent-scan-evidence";
55
+ /** The jobs task that runs the monthly scan. */
56
+ export declare const SCAN_TASK = "consentScan";
57
+ export declare const SCAN_WIDGET = "consent-scan";
58
+ /**
59
+ * The cookie with the policy version the visitor last chose under, beside c15t's own: a scanner can
60
+ * set cookies before the first page loads, but not localStorage.
61
+ */
62
+ export declare const POLICY_COOKIE = "c15t-policy";
63
+ /** Three years, unless the host sets `records.retentionDays`. */
64
+ export declare const DEFAULT_RETENTION_DAYS: number;
65
+ export declare const DEFAULT_KEEP_EVIDENCE = 12;
package/dist/types.js ADDED
@@ -0,0 +1,17 @@
1
+ export const CONSENT_GLOBAL = "consent";
2
+ export const CONSENT_SERVICES = "consent-services";
3
+ export const CONSENT_RECORDS = "consent-records";
4
+ export const CONSENT_POLICIES = "consent-policies";
5
+ export const CONSENT_SCANS = "consent-scans";
6
+ export const CONSENT_SCAN_EVIDENCE = "consent-scan-evidence";
7
+ /** The jobs task that runs the monthly scan. */
8
+ export const SCAN_TASK = "consentScan";
9
+ export const SCAN_WIDGET = "consent-scan";
10
+ /**
11
+ * The cookie with the policy version the visitor last chose under, beside c15t's own: a scanner can
12
+ * set cookies before the first page loads, but not localStorage.
13
+ */
14
+ export const POLICY_COOKIE = "c15t-policy";
15
+ /** Three years, unless the host sets `records.retentionDays`. */
16
+ export const DEFAULT_RETENTION_DAYS = 3 * 365;
17
+ export const DEFAULT_KEEP_EVIDENCE = 12;
@@ -0,0 +1,8 @@
1
+ import type { WidgetServerProps } from "payload";
2
+ import "../styles/admin.css";
3
+ /**
4
+ * Dashboard widget: the latest scan's status, red when something needs handling, with a link to
5
+ * the Cookies global. A panel of our own like content health's: Payload's `Card` is a link to one
6
+ * collection, not a status.
7
+ */
8
+ export declare function ConsentScanWidget({ req }: WidgetServerProps): Promise<import("react").JSX.Element>;
@@ -0,0 +1,99 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Button, Pill } from "@payloadcms/ui";
3
+ import { nextScan } from "../schedule.js";
4
+ import { CONSENT_GLOBAL, CONSENT_SCANS } from "../types.js";
5
+ import "../styles/admin.css";
6
+ const COPY = {
7
+ en: {
8
+ title: "Cookie scan",
9
+ none: "The site has not been scanned yet.",
10
+ waiting: "A scan is waiting or running.",
11
+ failed: "The latest scan failed",
12
+ action: (n) => `${n} need${n === 1 ? "s" : ""} action`,
13
+ unknown: (n) => `${n} not in the Cookie services`,
14
+ allGood: "Everything found is in order",
15
+ isNew: (n) => `${n} new since the scan before`,
16
+ scanned: (date) => `Scanned ${date}`,
17
+ next: (date) => `Next scan ${date}`,
18
+ off: "Monthly scans are off",
19
+ open: "Open",
20
+ pills: { red: "Needs action", green: "In order", grey: "No result" },
21
+ },
22
+ sv: {
23
+ title: "Cookieskanning",
24
+ none: "Webbplatsen har inte skannats än.",
25
+ waiting: "En skanning väntar eller pågår.",
26
+ failed: "Senaste skanningen misslyckades",
27
+ action: (n) => `${n} kräver åtgärd`,
28
+ unknown: (n) => `${n} finns inte bland Cookietjänsterna`,
29
+ allGood: "Allt som hittades är i ordning",
30
+ isNew: (n) => `${n} nya sedan skanningen innan`,
31
+ scanned: (date) => `Skannad ${date}`,
32
+ next: (date) => `Nästa skanning ${date}`,
33
+ off: "Månatliga skanningar är avstängda",
34
+ open: "Öppna",
35
+ pills: { red: "Åtgärd krävs", green: "I ordning", grey: "Inget resultat" },
36
+ },
37
+ };
38
+ /**
39
+ * Dashboard widget: the latest scan's status, red when something needs handling, with a link to
40
+ * the Cookies global. A panel of our own like content health's: Payload's `Card` is a link to one
41
+ * collection, not a status.
42
+ */
43
+ export async function ConsentScanWidget({ req }) {
44
+ const language = req.i18n?.language === "sv" ? "sv" : "en";
45
+ const copy = COPY[language];
46
+ const [{ docs }, global] = await Promise.all([
47
+ req.payload.find({
48
+ collection: CONSENT_SCANS,
49
+ sort: "-createdAt",
50
+ limit: 5,
51
+ depth: 0,
52
+ overrideAccess: false,
53
+ user: req.user,
54
+ select: {
55
+ status: true,
56
+ error: true,
57
+ summary: true,
58
+ finishedAt: true,
59
+ createdAt: true,
60
+ },
61
+ }),
62
+ req.payload.findGlobal({ slug: CONSENT_GLOBAL, depth: 0, overrideAccess: true }),
63
+ ]);
64
+ const scans = docs;
65
+ // A queued or running scan says nothing yet: show the last one with an outcome under it.
66
+ const pending = scans[0]?.status === "queued" || scans[0]?.status === "running";
67
+ const latest = scans.find((scan) => scan.status === "done" || scan.status === "failed");
68
+ const summary = latest?.status === "done" ? latest.summary : undefined;
69
+ const tone = summary ? (summary.action || summary.unknown ? "red" : "green") : "grey";
70
+ const format = (value) => value
71
+ ? new Date(value).toLocaleDateString(language === "sv" ? "sv-SE" : "en-GB", {
72
+ day: "numeric",
73
+ month: "long",
74
+ year: "numeric",
75
+ })
76
+ : "";
77
+ const schedule = req.payload.config.custom?.consentSchedule;
78
+ const monthly = global.scheduledScans !== false;
79
+ const lines = !latest
80
+ ? [copy.none]
81
+ : latest.status === "failed"
82
+ ? [`${copy.failed}: ${latest.error ?? ""}`]
83
+ : summary
84
+ ? [
85
+ ...(summary.action ? [copy.action(summary.action)] : []),
86
+ ...(summary.unknown ? [copy.unknown(summary.unknown)] : []),
87
+ ...(summary.action || summary.unknown ? [] : [copy.allGood]),
88
+ ...(summary.new ? [copy.isNew(summary.new)] : []),
89
+ ]
90
+ : [];
91
+ const adminRoute = req.payload.config.routes.admin;
92
+ return (_jsxs("section", { className: "consent-widget", children: [_jsx("h2", { className: "consent-widget__heading", children: copy.title }), _jsxs("div", { className: `consent-widget__panel consent-widget__panel--${tone}`, children: [_jsxs("div", { className: "consent-widget__top", children: [_jsx(Pill, { pillStyle: tone === "red" ? "error" : tone === "green" ? "success" : "light-gray", children: copy.pills[tone] }), _jsx(Button, { buttonStyle: "secondary", el: "link", margin: false, size: "small", url: `${adminRoute}/globals/${CONSENT_GLOBAL}`, children: copy.open })] }), _jsx("ul", { className: "consent-widget__lines", children: lines.map((line) => (_jsx("li", { children: line }, line))) }), _jsx("p", { className: "consent-widget__meta", children: [
93
+ latest ? copy.scanned(format(latest.finishedAt ?? latest.createdAt)) : undefined,
94
+ pending ? copy.waiting : undefined,
95
+ schedule ? (monthly ? copy.next(format(nextScan(schedule))) : copy.off) : undefined,
96
+ ]
97
+ .filter(Boolean)
98
+ .join(" · ") })] })] }));
99
+ }