@prenta/admin 2.12.0 → 2.13.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 (75) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/AdminRoot.d.ts.map +1 -1
  3. package/dist/AdminRoot.js +3 -3
  4. package/dist/AdminRoot.js.map +1 -1
  5. package/dist/__tests__/components/attribution-rows.test.js +17 -5
  6. package/dist/__tests__/components/attribution-rows.test.js.map +1 -1
  7. package/dist/__tests__/layout/shell-nav.render.test.js +3 -1
  8. package/dist/__tests__/layout/shell-nav.render.test.js.map +1 -1
  9. package/dist/__tests__/views/form-entries.render.test.js +5 -5
  10. package/dist/__tests__/views/form-entries.render.test.js.map +1 -1
  11. package/dist/__tests__/views/form-submissions.render.test.js +3 -3
  12. package/dist/__tests__/views/form-submissions.render.test.js.map +1 -1
  13. package/dist/__tests__/views/search-data-card.render.test.d.ts +2 -0
  14. package/dist/__tests__/views/search-data-card.render.test.d.ts.map +1 -0
  15. package/dist/__tests__/views/search-data-card.render.test.js +236 -0
  16. package/dist/__tests__/views/search-data-card.render.test.js.map +1 -0
  17. package/dist/__tests__/views/seo-settings.render.test.js +8 -4
  18. package/dist/__tests__/views/seo-settings.render.test.js.map +1 -1
  19. package/dist/components/forms/ExportMenu.d.ts +7 -1
  20. package/dist/components/forms/ExportMenu.d.ts.map +1 -1
  21. package/dist/components/forms/ExportMenu.js +17 -5
  22. package/dist/components/forms/ExportMenu.js.map +1 -1
  23. package/dist/layout/nav-config.d.ts.map +1 -1
  24. package/dist/layout/nav-config.js +8 -1
  25. package/dist/layout/nav-config.js.map +1 -1
  26. package/dist/lib/entry-attribution.d.ts +33 -0
  27. package/dist/lib/entry-attribution.d.ts.map +1 -1
  28. package/dist/lib/entry-attribution.js +115 -30
  29. package/dist/lib/entry-attribution.js.map +1 -1
  30. package/dist/lib/forms-service.d.ts +38 -0
  31. package/dist/lib/forms-service.d.ts.map +1 -1
  32. package/dist/lib/forms-service.js +10 -0
  33. package/dist/lib/forms-service.js.map +1 -1
  34. package/dist/prenta-admin.css +1 -1
  35. package/dist/views/FormEntries.d.ts +4 -1
  36. package/dist/views/FormEntries.d.ts.map +1 -1
  37. package/dist/views/FormEntries.js +2 -2
  38. package/dist/views/FormEntries.js.map +1 -1
  39. package/dist/views/FormSubmissions.d.ts +3 -1
  40. package/dist/views/FormSubmissions.d.ts.map +1 -1
  41. package/dist/views/FormSubmissions.js +2 -2
  42. package/dist/views/FormSubmissions.js.map +1 -1
  43. package/dist/views/SpamFolder.d.ts +6 -1
  44. package/dist/views/SpamFolder.d.ts.map +1 -1
  45. package/dist/views/SpamFolder.js +4 -11
  46. package/dist/views/SpamFolder.js.map +1 -1
  47. package/dist/views/entries/EntriesInbox.d.ts +12 -2
  48. package/dist/views/entries/EntriesInbox.d.ts.map +1 -1
  49. package/dist/views/entries/EntriesInbox.js +820 -81
  50. package/dist/views/entries/EntriesInbox.js.map +1 -1
  51. package/dist/views/entries/inbox-format.d.ts +0 -1
  52. package/dist/views/entries/inbox-format.d.ts.map +1 -1
  53. package/dist/views/entries/inbox-format.js +0 -4
  54. package/dist/views/entries/inbox-format.js.map +1 -1
  55. package/dist/views/settings/SearchDataCard.d.ts.map +1 -1
  56. package/dist/views/settings/SearchDataCard.js +162 -24
  57. package/dist/views/settings/SearchDataCard.js.map +1 -1
  58. package/package.json +7 -7
  59. package/src/AdminRoot.tsx +23 -3
  60. package/src/__tests__/components/attribution-rows.test.ts +19 -5
  61. package/src/__tests__/layout/shell-nav.render.test.tsx +3 -1
  62. package/src/__tests__/views/form-entries.render.test.tsx +5 -5
  63. package/src/__tests__/views/form-submissions.render.test.tsx +3 -3
  64. package/src/__tests__/views/search-data-card.render.test.tsx +275 -0
  65. package/src/__tests__/views/seo-settings.render.test.tsx +15 -5
  66. package/src/components/forms/ExportMenu.tsx +37 -13
  67. package/src/layout/nav-config.tsx +8 -0
  68. package/src/lib/entry-attribution.ts +155 -29
  69. package/src/lib/forms-service.ts +44 -0
  70. package/src/views/FormEntries.tsx +17 -2
  71. package/src/views/FormSubmissions.tsx +16 -2
  72. package/src/views/SpamFolder.tsx +14 -26
  73. package/src/views/entries/EntriesInbox.tsx +2119 -337
  74. package/src/views/entries/inbox-format.ts +0 -6
  75. package/src/views/settings/SearchDataCard.tsx +306 -45
@@ -120,9 +120,3 @@ export function entryIdLabel(entry: Pick<FormSubmission, 'entryNumber' | 'id'>):
120
120
  if (typeof entry.entryNumber === 'number') return `#${String(entry.entryNumber).padStart(5, '0')}`
121
121
  return entry.id
122
122
  }
123
-
124
- const LONG_KEYS = new Set(['description', 'message', 'services', 'platforms', 'goal'])
125
-
126
- export function fieldIsWide(key: string, value: string): boolean {
127
- return LONG_KEYS.has(key) || value.length > 70
128
- }
@@ -1,8 +1,9 @@
1
1
  'use client'
2
2
 
3
- import { useCallback, useEffect, useId, useState } from 'react'
3
+ import { useCallback, useEffect, useId, useRef, useState } from 'react'
4
4
  import { AlertTriangle, CheckCircle2, KeyRound, Loader2, PlugZap, RefreshCw, X } from 'lucide-react'
5
5
  import { toast } from 'sonner'
6
+ import { searchConsolePropertyError } from '@prenta/core/seo/search-console-property'
6
7
  import { cmsApi } from '../../lib/api.js'
7
8
  import { SettingsCard } from './components.js'
8
9
 
@@ -28,6 +29,19 @@ interface SearchConsoleStatus {
28
29
  suggestedProperty: string | null
29
30
  }
30
31
 
32
+ interface SearchConsoleProperty {
33
+ siteUrl: string
34
+ permissionLevel: string
35
+ }
36
+
37
+ /** `GET /seo/search-console/properties`: the properties the stored key can read. */
38
+ type PropertyList =
39
+ | { state: 'idle' | 'loading' }
40
+ | { state: 'ok'; properties: SearchConsoleProperty[]; serviceAccountEmail: string }
41
+ | { state: 'error'; message: string }
42
+
43
+ const FIELD_ERROR_CLASS = 'text-destructive mt-1 flex items-start gap-1 text-sm'
44
+
31
45
  function KeyStoredBadge() {
32
46
  return (
33
47
  <span className="text-muted-foreground border-border bg-muted inline-flex items-center gap-1 rounded-md border px-1.5 py-0.5 text-xs">
@@ -45,8 +59,11 @@ function KeyStoredBadge() {
45
59
  */
46
60
  export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
47
61
  const propertyId = useId()
62
+ const propertyHelpId = useId()
48
63
  const keyId = useId()
64
+ const keyErrorId = useId()
49
65
  const cruxId = useId()
66
+ const cruxErrorId = useId()
50
67
 
51
68
  const [status, setStatus] = useState<SearchConsoleStatus | null>(null)
52
69
  const [loading, setLoading] = useState(true)
@@ -59,6 +76,43 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
59
76
  const [testing, setTesting] = useState(false)
60
77
  const [syncing, setSyncing] = useState(false)
61
78
 
79
+ // Inline, per-field errors. The key, property and CrUX key save in separate
80
+ // requests, so a bad property never discards a good key (#1102).
81
+ const [propertyTouched, setPropertyTouched] = useState(false)
82
+ const [propertyServerError, setPropertyServerError] = useState<string | null>(null)
83
+ const [keyError, setKeyError] = useState<string | null>(null)
84
+ const [cruxError, setCruxError] = useState<string | null>(null)
85
+
86
+ const [propertyList, setPropertyList] = useState<PropertyList>({ state: 'idle' })
87
+ const [manualProperty, setManualProperty] = useState(false)
88
+
89
+ // Each lookup (and every key change) takes a new generation; a response is
90
+ // only applied if no newer one started, so a slow lookup for a replaced or
91
+ // removed key can't bring back its account's list.
92
+ const propertiesGeneration = useRef(0)
93
+
94
+ const loadProperties = useCallback(async () => {
95
+ const generation = ++propertiesGeneration.current
96
+ setPropertyList({ state: 'loading' })
97
+ const res = await cmsApi<
98
+ | { ok: true; properties: SearchConsoleProperty[]; serviceAccountEmail: string }
99
+ | { ok: false; code: string; message: string }
100
+ >('/seo/search-console/properties')
101
+ if (generation !== propertiesGeneration.current) return
102
+ if (res.data?.ok) {
103
+ setPropertyList({
104
+ state: 'ok',
105
+ properties: res.data.properties,
106
+ serviceAccountEmail: res.data.serviceAccountEmail,
107
+ })
108
+ } else {
109
+ setPropertyList({
110
+ state: 'error',
111
+ message: res.data?.message ?? res.error ?? 'Could not load your Search Console properties',
112
+ })
113
+ }
114
+ }, [])
115
+
62
116
  const load = useCallback(async () => {
63
117
  setLoading(true)
64
118
  setLoadError(null)
@@ -66,40 +120,111 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
66
120
  if (res.data) {
67
121
  setStatus(res.data)
68
122
  setProperty(res.data.propertyUrl ?? '')
123
+ if (res.data.hasServiceAccountKey) {
124
+ void loadProperties()
125
+ } else {
126
+ propertiesGeneration.current++
127
+ setPropertyList({ state: 'idle' })
128
+ }
69
129
  } else {
70
130
  setLoadError(res.error ?? 'Failed to load search data settings')
71
131
  }
72
132
  setLoading(false)
73
- }, [])
133
+ }, [loadProperties])
74
134
 
75
135
  useEffect(() => {
76
136
  void load()
77
137
  }, [load])
78
138
 
79
- const dirty =
80
- property.trim() !== (status?.propertyUrl ?? '') ||
81
- serviceKey.trim() !== '' ||
82
- cruxKey.trim() !== ''
139
+ const propertyChanged = property.trim() !== (status?.propertyUrl ?? '')
140
+ const dirty = propertyChanged || serviceKey.trim() !== '' || cruxKey.trim() !== ''
141
+ const propertyFormatError = searchConsolePropertyError(property)
142
+ const propertyError =
143
+ propertyServerError ?? (propertyTouched || propertyChanged ? propertyFormatError : null)
144
+
145
+ async function put(body: Record<string, string>): Promise<string | null> {
146
+ const res = await cmsApi('/seo/search-console', { method: 'PUT', body: JSON.stringify(body) })
147
+ return res.error ?? null
148
+ }
83
149
 
84
- async function save(extra?: { serviceAccountKey?: string; cruxApiKey?: string }) {
150
+ async function save() {
151
+ setPropertyTouched(true)
152
+ setPropertyServerError(null)
153
+ setKeyError(null)
154
+ setCruxError(null)
85
155
  setSaving(true)
86
- const body: Record<string, string> = { propertyUrl: property.trim(), ...extra }
87
- if (!extra) {
88
- if (serviceKey.trim()) body.serviceAccountKey = serviceKey.trim()
89
- if (cruxKey.trim()) body.cruxApiKey = cruxKey.trim()
156
+
157
+ const saved: string[] = []
158
+ let failed = false
159
+
160
+ const key = serviceKey.trim()
161
+ if (key) {
162
+ const err = await put({ serviceAccountKey: key })
163
+ if (err) {
164
+ setKeyError(err)
165
+ failed = true
166
+ } else {
167
+ // The old key's lookup is stale now; the reload below starts a new one.
168
+ propertiesGeneration.current++
169
+ setServiceKey('')
170
+ saved.push('service account key')
171
+ }
172
+ }
173
+
174
+ const crux = cruxKey.trim()
175
+ if (crux) {
176
+ const err = await put({ cruxApiKey: crux })
177
+ if (err) {
178
+ setCruxError(err)
179
+ failed = true
180
+ } else {
181
+ setCruxKey('')
182
+ saved.push('CrUX key')
183
+ }
90
184
  }
91
- const res = await cmsApi('/seo/search-console', {
92
- method: 'PUT',
93
- body: JSON.stringify(body),
94
- })
185
+
186
+ if (propertyChanged) {
187
+ if (propertyFormatError) {
188
+ failed = true
189
+ } else {
190
+ const err = await put({ propertyUrl: property.trim() })
191
+ if (err) {
192
+ setPropertyServerError(err)
193
+ failed = true
194
+ } else {
195
+ saved.push('property')
196
+ }
197
+ }
198
+ }
199
+
95
200
  setSaving(false)
96
- if (res.error) {
97
- toast.error(res.error)
201
+ if (saved.length > 0) {
202
+ toast.success(
203
+ failed
204
+ ? `Saved the ${saved.join(' and ')}. Fix the highlighted field to save the rest.`
205
+ : 'Search data settings saved',
206
+ )
207
+ const keepProperty = failed && propertyChanged ? property : null
208
+ await load()
209
+ if (keepProperty !== null) setProperty(keepProperty)
210
+ } else if (failed) {
211
+ toast.error('Nothing was saved. Fix the highlighted field.')
212
+ }
213
+ }
214
+
215
+ async function removeKey(field: 'serviceAccountKey' | 'cruxApiKey') {
216
+ if (field === 'serviceAccountKey') {
217
+ propertiesGeneration.current++
218
+ setPropertyList({ state: 'idle' })
219
+ }
220
+ setSaving(true)
221
+ const err = await put({ [field]: '' })
222
+ setSaving(false)
223
+ if (err) {
224
+ toast.error(err)
98
225
  return
99
226
  }
100
- toast.success('Search data settings saved')
101
- setServiceKey('')
102
- setCruxKey('')
227
+ toast.success('Key removed')
103
228
  await load()
104
229
  }
105
230
 
@@ -187,6 +312,13 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
187
312
  }
188
313
 
189
314
  const lastSync = status?.lastSyncAt ? new Date(status.lastSyncAt).toLocaleString() : 'Never'
315
+ const pickerProperties =
316
+ propertyList.state === 'ok' && propertyList.properties.length > 0
317
+ ? propertyList.properties
318
+ : null
319
+ // An invalid value (e.g. kept after a partial save) shows in the text field,
320
+ // where its error and the "Use sc-domain:…" fix are.
321
+ const showPicker = Boolean(pickerProperties) && !manualProperty && !propertyFormatError
190
322
 
191
323
  return (
192
324
  <SettingsCard
@@ -215,33 +347,138 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
215
347
  <label htmlFor={propertyId} className={LABEL_CLASS}>
216
348
  Search Console property
217
349
  </label>
218
- <input
219
- id={propertyId}
220
- type="text"
221
- autoComplete="off"
222
- spellCheck={false}
223
- value={property}
224
- disabled={!canEdit}
225
- placeholder={status?.suggestedProperty ?? 'sc-domain:example.com'}
226
- onChange={(e) => setProperty(e.target.value)}
227
- className={INPUT_CLASS}
228
- />
229
- <p className="text-muted-foreground mt-1 text-sm">
230
- Domain property (<code>sc-domain:example.com</code>) or URL prefix (
231
- <code>https://example.com/</code>) exactly as it appears in Search Console.
232
- {status?.suggestedProperty && !property.trim() && canEdit && (
233
- <>
234
- {' '}
350
+ {showPicker && pickerProperties ? (
351
+ <select
352
+ id={propertyId}
353
+ value={property}
354
+ disabled={!canEdit}
355
+ aria-invalid={propertyError ? true : undefined}
356
+ aria-describedby={propertyHelpId}
357
+ onChange={(e) => {
358
+ setProperty(e.target.value)
359
+ setPropertyServerError(null)
360
+ }}
361
+ className={INPUT_CLASS}
362
+ >
363
+ <option value="">Choose a property…</option>
364
+ {pickerProperties.map((p) => (
365
+ <option key={p.siteUrl} value={p.siteUrl}>
366
+ {p.siteUrl}
367
+ </option>
368
+ ))}
369
+ {property && !pickerProperties.some((p) => p.siteUrl === property) && (
370
+ <option value={property}>{property} (not granted to this key)</option>
371
+ )}
372
+ </select>
373
+ ) : (
374
+ <input
375
+ id={propertyId}
376
+ type="text"
377
+ autoComplete="off"
378
+ spellCheck={false}
379
+ value={property}
380
+ disabled={!canEdit}
381
+ aria-invalid={propertyError ? true : undefined}
382
+ aria-describedby={propertyHelpId}
383
+ placeholder={status?.suggestedProperty ?? 'sc-domain:example.com'}
384
+ onChange={(e) => {
385
+ setProperty(e.target.value)
386
+ setPropertyServerError(null)
387
+ }}
388
+ onBlur={() => setPropertyTouched(true)}
389
+ className={INPUT_CLASS}
390
+ />
391
+ )}
392
+ <div id={propertyHelpId}>
393
+ {propertyError && (
394
+ <p role="alert" className={FIELD_ERROR_CLASS}>
395
+ <AlertTriangle size={14} className="mt-0.5 shrink-0" aria-hidden="true" />
396
+ <span>{propertyError}</span>
397
+ </p>
398
+ )}
399
+ {status?.hasServiceAccountKey && propertyList.state === 'loading' && (
400
+ <p className="text-muted-foreground mt-1 inline-flex items-center gap-1 text-sm">
401
+ <Loader2
402
+ size={13}
403
+ className="animate-spin motion-reduce:animate-none"
404
+ aria-hidden="true"
405
+ />
406
+ Loading the properties this key can read…
407
+ </p>
408
+ )}
409
+ {propertyList.state === 'ok' && propertyList.properties.length === 0 && (
410
+ <p className="text-muted-foreground mt-1 text-sm">
411
+ No properties yet — add{' '}
412
+ <span className="text-foreground font-medium">
413
+ {propertyList.serviceAccountEmail}
414
+ </span>{' '}
415
+ in Search Console → Settings → Users and permissions, then{' '}
235
416
  <button
236
417
  type="button"
237
- onClick={() => setProperty(status.suggestedProperty ?? '')}
418
+ onClick={() => void loadProperties()}
238
419
  className="text-brand hover:underline"
239
420
  >
240
- Use {status.suggestedProperty}
421
+ refresh the list
241
422
  </button>
242
- </>
423
+ .
424
+ </p>
243
425
  )}
244
- </p>
426
+ {propertyList.state === 'error' && (
427
+ <p className="text-muted-foreground mt-1 text-sm">
428
+ Couldn’t list your properties: {propertyList.message}{' '}
429
+ <button
430
+ type="button"
431
+ onClick={() => void loadProperties()}
432
+ className="text-brand hover:underline"
433
+ >
434
+ Try again
435
+ </button>
436
+ </p>
437
+ )}
438
+ {!showPicker && (
439
+ <p className="text-muted-foreground mt-1 text-sm">
440
+ Domain property (<code>sc-domain:example.com</code>) or URL prefix (
441
+ <code>https://example.com/</code>) exactly as it appears in Search Console.
442
+ {status?.suggestedProperty &&
443
+ (!property.trim() || propertyFormatError) &&
444
+ property.trim() !== status.suggestedProperty &&
445
+ canEdit && (
446
+ <>
447
+ {' '}
448
+ <button
449
+ type="button"
450
+ onClick={() => {
451
+ setProperty(status.suggestedProperty ?? '')
452
+ setPropertyServerError(null)
453
+ }}
454
+ className="text-brand hover:underline"
455
+ >
456
+ Use {status.suggestedProperty}
457
+ </button>
458
+ </>
459
+ )}
460
+ </p>
461
+ )}
462
+ {pickerProperties && canEdit && (
463
+ <button
464
+ type="button"
465
+ onClick={() => {
466
+ if (showPicker) {
467
+ setManualProperty(true)
468
+ return
469
+ }
470
+ if (propertyFormatError) {
471
+ setProperty('')
472
+ setPropertyServerError(null)
473
+ }
474
+ setManualProperty(false)
475
+ }}
476
+ className="text-brand mt-1 text-sm hover:underline"
477
+ >
478
+ {showPicker ? 'Enter it manually' : 'Choose from your properties'}
479
+ </button>
480
+ )}
481
+ </div>
245
482
  </div>
246
483
 
247
484
  {/* Service account key */}
@@ -262,9 +499,20 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
262
499
  ? 'Paste a new key to replace the stored one'
263
500
  : '{ "type": "service_account", "client_email": … }'
264
501
  }
265
- onChange={(e) => setServiceKey(e.target.value)}
502
+ onChange={(e) => {
503
+ setServiceKey(e.target.value)
504
+ setKeyError(null)
505
+ }}
506
+ aria-invalid={keyError ? true : undefined}
507
+ aria-describedby={keyError ? keyErrorId : undefined}
266
508
  className={`${INPUT_CLASS} resize-y font-mono text-sm`}
267
509
  />
510
+ {keyError && (
511
+ <p id={keyErrorId} role="alert" className={FIELD_ERROR_CLASS}>
512
+ <AlertTriangle size={14} className="mt-0.5 shrink-0" aria-hidden="true" />
513
+ <span>{keyError}</span>
514
+ </p>
515
+ )}
268
516
  <p className="text-muted-foreground mt-1 text-sm">
269
517
  {status?.serviceAccountEmail ? (
270
518
  <>
@@ -279,7 +527,8 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
279
527
  {status?.hasServiceAccountKey && canEdit && (
280
528
  <button
281
529
  type="button"
282
- onClick={() => void save({ serviceAccountKey: '' })}
530
+ onClick={() => void removeKey('serviceAccountKey')}
531
+ disabled={saving}
283
532
  className="text-muted-foreground hover:text-destructive mt-1 inline-flex items-center gap-1 rounded-md text-sm transition-colors"
284
533
  >
285
534
  <X size={13} aria-hidden="true" />
@@ -303,16 +552,28 @@ export function SearchDataCard({ canEdit }: { canEdit: boolean }) {
303
552
  placeholder={
304
553
  status?.hasCruxApiKey ? 'Paste a new key to replace the stored one' : 'AIza…'
305
554
  }
306
- onChange={(e) => setCruxKey(e.target.value)}
555
+ onChange={(e) => {
556
+ setCruxKey(e.target.value)
557
+ setCruxError(null)
558
+ }}
559
+ aria-invalid={cruxError ? true : undefined}
560
+ aria-describedby={cruxError ? cruxErrorId : undefined}
307
561
  className={INPUT_CLASS}
308
562
  />
563
+ {cruxError && (
564
+ <p id={cruxErrorId} role="alert" className={FIELD_ERROR_CLASS}>
565
+ <AlertTriangle size={14} className="mt-0.5 shrink-0" aria-hidden="true" />
566
+ <span>{cruxError}</span>
567
+ </p>
568
+ )}
309
569
  <p className="text-muted-foreground mt-1 text-sm">
310
570
  Optional — collects real-user Core Web Vitals (LCP, INP, CLS) from the Chrome UX Report.
311
571
  </p>
312
572
  {status?.hasCruxApiKey && canEdit && (
313
573
  <button
314
574
  type="button"
315
- onClick={() => void save({ cruxApiKey: '' })}
575
+ onClick={() => void removeKey('cruxApiKey')}
576
+ disabled={saving}
316
577
  className="text-muted-foreground hover:text-destructive mt-1 inline-flex items-center gap-1 rounded-md text-sm transition-colors"
317
578
  >
318
579
  <X size={13} aria-hidden="true" />