@kelpie/ui 0.17.0 → 0.18.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 (120) hide show
  1. package/dist/api/resources/ai.d.ts +5 -0
  2. package/dist/api/resources/ai.d.ts.map +1 -1
  3. package/dist/api/resources/ai.js +9 -1
  4. package/dist/api/resources/ai.js.map +1 -1
  5. package/dist/api/resources/forms.d.ts +8 -0
  6. package/dist/api/resources/forms.d.ts.map +1 -1
  7. package/dist/api/resources/forms.js +39 -0
  8. package/dist/api/resources/forms.js.map +1 -1
  9. package/dist/api/resources/personIntake.d.ts +12 -0
  10. package/dist/api/resources/personIntake.d.ts.map +1 -0
  11. package/dist/api/resources/personIntake.js +54 -0
  12. package/dist/api/resources/personIntake.js.map +1 -0
  13. package/dist/app/KelpieApp.js +2 -2
  14. package/dist/app/KelpieApp.js.map +1 -1
  15. package/dist/components/AgentTasks.js +2 -2
  16. package/dist/components/AgentTasks.js.map +1 -1
  17. package/dist/components/PersonIntakeDialog.d.ts +19 -0
  18. package/dist/components/PersonIntakeDialog.d.ts.map +1 -0
  19. package/dist/components/PersonIntakeDialog.js +87 -0
  20. package/dist/components/PersonIntakeDialog.js.map +1 -0
  21. package/dist/components/Shell.js +1 -1
  22. package/dist/components/Shell.js.map +1 -1
  23. package/dist/index.d.ts +3 -3
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +2 -2
  26. package/dist/index.js.map +1 -1
  27. package/dist/modules/PersonIntakeWizard.d.ts +3 -0
  28. package/dist/modules/PersonIntakeWizard.d.ts.map +1 -0
  29. package/dist/modules/PersonIntakeWizard.js +223 -0
  30. package/dist/modules/PersonIntakeWizard.js.map +1 -0
  31. package/dist/modules/ai.d.ts +4 -3
  32. package/dist/modules/ai.d.ts.map +1 -1
  33. package/dist/modules/ai.js +10 -6
  34. package/dist/modules/ai.js.map +1 -1
  35. package/dist/modules/aiRunner.d.ts.map +1 -1
  36. package/dist/modules/aiRunner.js +7 -5
  37. package/dist/modules/aiRunner.js.map +1 -1
  38. package/dist/modules/personIntakeSelection.d.ts +7 -0
  39. package/dist/modules/personIntakeSelection.d.ts.map +1 -0
  40. package/dist/modules/personIntakeSelection.js +21 -0
  41. package/dist/modules/personIntakeSelection.js.map +1 -0
  42. package/dist/pages/FormsPage.js +4 -4
  43. package/dist/pages/FormsPage.js.map +1 -1
  44. package/dist/pages/PeoplePage.d.ts.map +1 -1
  45. package/dist/pages/PeoplePage.js +7 -1
  46. package/dist/pages/PeoplePage.js.map +1 -1
  47. package/dist/pages/admin/AiAdminPage.d.ts +2 -0
  48. package/dist/pages/admin/AiAdminPage.d.ts.map +1 -0
  49. package/dist/pages/admin/AiAdminPage.js +19 -0
  50. package/dist/pages/admin/AiAdminPage.js.map +1 -0
  51. package/dist/pages/admin/AiPage.d.ts +4 -1
  52. package/dist/pages/admin/AiPage.d.ts.map +1 -1
  53. package/dist/pages/admin/AiPage.js +77 -15
  54. package/dist/pages/admin/AiPage.js.map +1 -1
  55. package/dist/pages/admin/McpPanel.d.ts +3 -0
  56. package/dist/pages/admin/McpPanel.d.ts.map +1 -0
  57. package/dist/pages/admin/McpPanel.js +123 -0
  58. package/dist/pages/admin/McpPanel.js.map +1 -0
  59. package/dist/pages/admin/PrivacyPage.js +19 -3
  60. package/dist/pages/admin/PrivacyPage.js.map +1 -1
  61. package/dist/pages/forms/EmbedPanel.js +5 -2
  62. package/dist/pages/forms/EmbedPanel.js.map +1 -1
  63. package/dist/pages/forms/FieldsEditor.d.ts.map +1 -1
  64. package/dist/pages/forms/FieldsEditor.js +90 -8
  65. package/dist/pages/forms/FieldsEditor.js.map +1 -1
  66. package/dist/pages/forms/FormSettings.d.ts.map +1 -1
  67. package/dist/pages/forms/FormSettings.js +73 -6
  68. package/dist/pages/forms/FormSettings.js.map +1 -1
  69. package/dist/pages/forms/SubmissionDetail.d.ts.map +1 -1
  70. package/dist/pages/forms/SubmissionDetail.js +2 -1
  71. package/dist/pages/forms/SubmissionDetail.js.map +1 -1
  72. package/dist/pages/forms/SubmissionsTable.d.ts.map +1 -1
  73. package/dist/pages/forms/SubmissionsTable.js +2 -1
  74. package/dist/pages/forms/SubmissionsTable.js.map +1 -1
  75. package/dist/pages/forms/fieldList.d.ts +2 -2
  76. package/dist/pages/forms/fieldList.d.ts.map +1 -1
  77. package/dist/pages/forms/fieldList.js +38 -10
  78. package/dist/pages/forms/fieldList.js.map +1 -1
  79. package/dist/pages/forms/template.d.ts.map +1 -1
  80. package/dist/pages/forms/template.js +13 -0
  81. package/dist/pages/forms/template.js.map +1 -1
  82. package/dist/registry/context.d.ts +5 -1
  83. package/dist/registry/context.d.ts.map +1 -1
  84. package/dist/registry/context.js +8 -0
  85. package/dist/registry/context.js.map +1 -1
  86. package/dist/registry/contributions.d.ts +32 -1
  87. package/dist/registry/contributions.d.ts.map +1 -1
  88. package/dist/registry/registry.d.ts +8 -1
  89. package/dist/registry/registry.d.ts.map +1 -1
  90. package/dist/registry/registry.js +13 -0
  91. package/dist/registry/registry.js.map +1 -1
  92. package/package.json +2 -2
  93. package/src/api/resources/ai.ts +11 -1
  94. package/src/api/resources/forms.ts +56 -0
  95. package/src/api/resources/personIntake.ts +95 -0
  96. package/src/app/KelpieApp.tsx +4 -2
  97. package/src/components/AgentTasks.tsx +2 -2
  98. package/src/components/PersonIntakeDialog.tsx +226 -0
  99. package/src/components/Shell.tsx +1 -1
  100. package/src/index.ts +7 -1
  101. package/src/modules/PersonIntakeWizard.tsx +633 -0
  102. package/src/modules/ai.tsx +10 -6
  103. package/src/modules/aiRunner.ts +8 -5
  104. package/src/modules/personIntakeSelection.ts +24 -0
  105. package/src/pages/FormsPage.tsx +4 -4
  106. package/src/pages/PeoplePage.tsx +21 -5
  107. package/src/pages/admin/AiAdminPage.tsx +52 -0
  108. package/src/pages/admin/AiPage.tsx +136 -32
  109. package/src/pages/admin/{McpPage.tsx → McpPanel.tsx} +4 -6
  110. package/src/pages/admin/PrivacyPage.tsx +42 -0
  111. package/src/pages/forms/EmbedPanel.tsx +5 -1
  112. package/src/pages/forms/FieldsEditor.tsx +204 -15
  113. package/src/pages/forms/FormSettings.tsx +156 -9
  114. package/src/pages/forms/SubmissionDetail.tsx +2 -1
  115. package/src/pages/forms/SubmissionsTable.tsx +2 -1
  116. package/src/pages/forms/fieldList.ts +39 -10
  117. package/src/pages/forms/template.ts +13 -0
  118. package/src/registry/context.ts +13 -0
  119. package/src/registry/contributions.ts +34 -1
  120. package/src/registry/registry.ts +30 -0
@@ -9,7 +9,7 @@ import {
9
9
  import type { DragEndEvent } from '@dnd-kit/core'
10
10
  import { SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'
11
11
  import { CSS } from '@dnd-kit/utilities'
12
- import { FORM_OPTION_VALUE_TYPES } from '@kelpie/schemas'
12
+ import { FORM_OPTION_VALUE_TYPES, consentCheckboxText, formFieldDisplayLabel } from '@kelpie/schemas'
13
13
  import type {
14
14
  Form,
15
15
  FormFieldInput,
@@ -20,8 +20,10 @@ import { useEffect, useMemo, useRef, useState } from 'react'
20
20
 
21
21
  import { useConsentPurposes } from '../../api/resources/consentPurposes.ts'
22
22
  import { useCustomFields } from '../../api/resources/customFields.ts'
23
- import type { ConsentPurpose } from '@kelpie/schemas'
23
+ import type { ConsentPurpose, List } from '@kelpie/schemas'
24
24
  import { useUpdateFormFields } from '../../api/resources/forms.ts'
25
+ import { useLists } from '../../api/resources/lists.ts'
26
+ import { useWorkspace } from '../../api/resources/workspace.ts'
25
27
  import { ErrorPanel } from '../../components/QueryState.tsx'
26
28
  import { AddFieldMenu } from './AddFieldMenu.tsx'
27
29
  import { MapTargetSearch } from './MapTargetSearch.tsx'
@@ -281,8 +283,8 @@ function PreviewField({ field, selected, problem, onSelect }: PreviewFieldProps)
281
283
  >
282
284
  <button
283
285
  type="button"
284
- className="mt-8 shrink-0 cursor-grab touch-none rounded-md px-1 py-1 text-[10px] leading-none text-ink-faint opacity-0 transition hover:text-ink focus-visible:opacity-100 active:cursor-grabbing group-hover:opacity-100"
285
- aria-label={`Drag ${field.label} to reorder`}
286
+ className="shrink-0 self-center cursor-grab touch-none rounded-md px-1 py-1 text-[10px] leading-none text-ink-faint opacity-0 transition hover:text-ink focus-visible:opacity-100 active:cursor-grabbing group-hover:opacity-100"
287
+ aria-label={`Drag ${formFieldDisplayLabel(field)} to reorder`}
286
288
  {...attributes}
287
289
  {...listeners}
288
290
  >
@@ -302,16 +304,19 @@ function PreviewField({ field, selected, problem, onSelect }: PreviewFieldProps)
302
304
  <button
303
305
  type="button"
304
306
  onClick={onSelect}
305
- aria-label={`Edit ${field.label}`}
307
+ aria-label={`Edit ${formFieldDisplayLabel(field)}`}
306
308
  aria-pressed={selected}
307
309
  className="absolute inset-0 z-10 cursor-pointer rounded-md"
308
310
  />
309
311
 
310
312
  <div className="pointer-events-none">
311
- <span className="mb-1 block text-[13px] font-medium text-ink">
312
- {field.label}
313
- {(field.required ?? false) && <span className="ml-0.5 text-danger">*</span>}
314
- </span>
313
+ {/* No label, no heading: the same as the embed. */}
314
+ {field.label.trim().length > 0 && (
315
+ <span className="mb-1 block text-[13px] font-medium text-ink">
316
+ {field.label}
317
+ {(field.required ?? false) && <span className="ml-0.5 text-danger">*</span>}
318
+ </span>
319
+ )}
315
320
  <PreviewControl field={field} />
316
321
  {problem !== undefined && <p className="mt-1.5 text-[12px] text-danger">{problem}</p>}
317
322
  </div>
@@ -368,6 +373,10 @@ function PreviewControl({ field }: { readonly field: EditableField }): React.JSX
368
373
  return <NoticePreview field={field} />
369
374
  }
370
375
 
376
+ if (field.type === 'list') {
377
+ return <ListPreview field={field} />
378
+ }
379
+
371
380
  return (
372
381
  <input
373
382
  readOnly
@@ -405,10 +414,15 @@ function NoticePreview({ field }: { readonly field: EditableField }): React.JSX.
405
414
  )
406
415
  }
407
416
 
417
+ /** Until the workspace loads, the preview shows the token rather than a guess. */
418
+ const WORKSPACE_TOKEN = '{{workspace}}'
419
+
408
420
  function ConsentPreview({ field }: { readonly field: EditableField }): React.JSX.Element {
409
421
  const purposes = useConsentPurposes({ sort: 'sort_order', limit: 200 })
410
- const labelById = new Map<string, string>(
411
- purposes.records.map((purpose: ConsentPurpose) => [purpose.id, purpose.label]),
422
+ const { workspace } = useWorkspace()
423
+ const workspaceName = workspace?.name ?? WORKSPACE_TOKEN
424
+ const purposeById = new Map<string, ConsentPurpose>(
425
+ purposes.records.map((purpose: ConsentPurpose) => [purpose.id, purpose]),
412
426
  )
413
427
  const statement = field.statement ?? field.label
414
428
  const ids = field.consentPurposeIds ?? []
@@ -426,7 +440,11 @@ function ConsentPreview({ field }: { readonly field: EditableField }): React.JSX
426
440
  <div key={id} className="flex items-start gap-2">
427
441
  <input readOnly tabIndex={-1} type="checkbox" className="mt-0.5" />
428
442
  <span className="text-[13px] text-ink">
429
- {overrides[id] ?? labelById.get(id) ?? id}
443
+ {consentCheckboxText(
444
+ overrides[id],
445
+ purposeById.get(id) ?? { label: id, statement: '' },
446
+ workspaceName,
447
+ )}
430
448
  </span>
431
449
  </div>
432
450
  ))
@@ -435,6 +453,51 @@ function ConsentPreview({ field }: { readonly field: EditableField }): React.JSX
435
453
  )
436
454
  }
437
455
 
456
+ /**
457
+ * The lists a form can feed: person lists get the submitter, company lists the
458
+ * resolved company. The same two the form's Actions tab offers.
459
+ */
460
+ function useFormLists(): { readonly isLoading: boolean; readonly records: readonly List[] } {
461
+ const personLists = useLists({ targetType: 'person' })
462
+ const companyLists = useLists({ targetType: 'company' })
463
+ const records = useMemo(
464
+ () => [...personLists.records, ...companyLists.records],
465
+ [personLists.records, companyLists.records],
466
+ )
467
+
468
+ return { isLoading: personLists.isLoading || companyLists.isLoading, records }
469
+ }
470
+
471
+ /**
472
+ * The Add to list preview — what the visitor sees: the statement, then one
473
+ * checkbox per list, each with the field's own text or else the list's name.
474
+ */
475
+ function ListPreview({ field }: { readonly field: EditableField }): React.JSX.Element {
476
+ const lists = useFormLists()
477
+ const nameById = new Map(lists.records.map((list) => [list.id, list.name]))
478
+ const statement = (field.statement ?? '').trim()
479
+ const ids = field.listIds ?? []
480
+ const overrides = field.listLabels ?? {}
481
+
482
+ return (
483
+ <div className="space-y-1.5">
484
+ {statement.length > 0 && <p className="text-[12px] text-ink-muted">{statement}</p>}
485
+ {ids.length === 0 ? (
486
+ <p className="text-[11px] italic text-ink-faint">
487
+ Pick at least one list in the settings panel.
488
+ </p>
489
+ ) : (
490
+ ids.map((id) => (
491
+ <div key={id} className="flex items-start gap-2">
492
+ <input readOnly tabIndex={-1} type="checkbox" className="mt-0.5" />
493
+ <span className="text-[13px] text-ink">{overrides[id] ?? nameById.get(id) ?? id}</span>
494
+ </div>
495
+ ))
496
+ )}
497
+ </div>
498
+ )
499
+ }
500
+
438
501
  interface FieldSettingsProps {
439
502
  readonly field: EditableField
440
503
  readonly fields: readonly EditableField[]
@@ -590,6 +653,33 @@ function FieldSettings({
590
653
  </>
591
654
  )}
592
655
 
656
+ {field.type === 'list' && (
657
+ <>
658
+ <label className="block">
659
+ <span className="mb-1 block text-[11px] font-medium text-ink-faint">
660
+ Description (text above the checkboxes)
661
+ </span>
662
+ <textarea
663
+ className={inputClass}
664
+ rows={3}
665
+ value={field.statement ?? ''}
666
+ onChange={(event) => {
667
+ const value = event.target.value
668
+ onChange({ statement: value.length === 0 ? null : value })
669
+ }}
670
+ placeholder="We send one email a month."
671
+ />
672
+ </label>
673
+ <FormListPicker
674
+ value={field.listIds ?? []}
675
+ labels={field.listLabels ?? {}}
676
+ onChange={(listIds, listLabels) => {
677
+ onChange({ listIds, listLabels })
678
+ }}
679
+ />
680
+ </>
681
+ )}
682
+
593
683
  {problem !== undefined && <p className="text-[12px] text-danger">{problem}</p>}
594
684
 
595
685
  <button
@@ -701,7 +791,7 @@ function OptionsEditor({
701
791
  * beside each selected purpose so the admin can override the checkbox text.
702
792
  * The override is bound to the same purpose the checkbox grants — the wording
703
793
  * changes, the mapping never does. An empty override falls back to the
704
- * workspace purpose's own label at render time.
794
+ * workspace purpose's consent statement, then its label, at render time.
705
795
  */
706
796
  function ConsentPurposePicker({
707
797
  mode,
@@ -790,7 +880,7 @@ function ConsentPurposePicker({
790
880
  {isChosen && mode === 'checkbox' && (
791
881
  <label className="mt-1.5 block">
792
882
  <span className="mb-1 block text-[11px] font-medium text-ink-faint">
793
- Checkbox text (defaults to the purpose label)
883
+ Checkbox text (defaults to the purpose's consent statement)
794
884
  </span>
795
885
  <input
796
886
  className={inputClass}
@@ -798,7 +888,106 @@ function ConsentPurposePicker({
798
888
  onChange={(event) => {
799
889
  setLabel(purpose.id, event.target.value)
800
890
  }}
801
- placeholder={purpose.label}
891
+ placeholder={
892
+ purpose.statement.trim().length > 0 ? purpose.statement : purpose.label
893
+ }
894
+ />
895
+ </label>
896
+ )}
897
+ </div>
898
+ )
899
+ })}
900
+ </div>
901
+ </fieldset>
902
+ )
903
+ }
904
+
905
+ /**
906
+ * Picks the lists an Add to list field offers, with a text input beside each
907
+ * chosen list to override its checkbox text. An empty override falls back to
908
+ * the list's name. The workspace's list order is the order the visitor sees.
909
+ */
910
+ function FormListPicker({
911
+ value,
912
+ labels,
913
+ onChange,
914
+ }: {
915
+ readonly value: readonly string[]
916
+ readonly labels: Readonly<Record<string, string>>
917
+ readonly onChange: (ids: readonly string[], labels: Readonly<Record<string, string>>) => void
918
+ }): React.JSX.Element {
919
+ const lists = useFormLists()
920
+
921
+ if (lists.isLoading) {
922
+ return <p className="text-[12px] text-ink-muted">Loading lists…</p>
923
+ }
924
+
925
+ if (lists.records.length === 0) {
926
+ return (
927
+ <p className="text-[12px] text-ink-muted">
928
+ No person or company lists yet. Add one on the Lists page, then pick it here.
929
+ </p>
930
+ )
931
+ }
932
+
933
+ const chosen = new Set(value)
934
+
935
+ function toggle(id: string, checked: boolean): void {
936
+ const nextIds = lists.records
937
+ .map((list) => list.id)
938
+ .filter((listId) => (listId === id ? checked : chosen.has(listId)))
939
+ // A list taken off the field drops its text too, so it does not come
940
+ // back if the list is picked again later.
941
+ const nextLabels: Record<string, string> = {}
942
+ for (const [key, text] of Object.entries(labels)) {
943
+ if (nextIds.includes(key)) nextLabels[key] = text
944
+ }
945
+ onChange(nextIds, nextLabels)
946
+ }
947
+
948
+ function setLabel(id: string, text: string): void {
949
+ const nextLabels: Record<string, string> = { ...labels }
950
+ if (text.length === 0) {
951
+ delete nextLabels[id]
952
+ } else {
953
+ nextLabels[id] = text
954
+ }
955
+ onChange(value, nextLabels)
956
+ }
957
+
958
+ return (
959
+ <fieldset className="block">
960
+ <legend className="mb-1 block text-[11px] font-medium text-ink-faint">
961
+ Lists (each selected one becomes a checkbox)
962
+ </legend>
963
+ <div className="flex flex-col gap-2">
964
+ {lists.records.map((list) => {
965
+ const isChosen = chosen.has(list.id)
966
+ return (
967
+ <div key={list.id} className="rounded-md border border-border p-2">
968
+ <label className="flex items-center gap-2 text-[13px] text-ink">
969
+ <input
970
+ type="checkbox"
971
+ checked={isChosen}
972
+ onChange={(event) => {
973
+ toggle(list.id, event.target.checked)
974
+ }}
975
+ />
976
+ {list.name}
977
+ <span className="text-[11px] text-ink-faint">{list.targetType}</span>
978
+ </label>
979
+ {isChosen && (
980
+ <label className="mt-1.5 block">
981
+ <span className="mb-1 block text-[11px] font-medium text-ink-faint">
982
+ Checkbox text (defaults to the list's name)
983
+ </span>
984
+ <input
985
+ className={inputClass}
986
+ value={labels[list.id] ?? ''}
987
+ onChange={(event) => {
988
+ setLabel(list.id, event.target.value)
989
+ }}
990
+ placeholder={list.name}
802
991
  />
803
992
  </label>
804
993
  )}
@@ -1,17 +1,23 @@
1
- import { FORM_STATUSES } from '@kelpie/schemas'
1
+ import { useState } from 'react'
2
+ import { FORM_SLUG_PATTERN, FORM_STATUSES } from '@kelpie/schemas'
2
3
  import type { Form, FormStatus } from '@kelpie/schemas'
3
4
 
4
- import { useUpdateForm } from '../../api/resources/forms.ts'
5
+ import { ApiError } from '../../api/client.ts'
6
+ import {
7
+ useRegenerateFormSlug,
8
+ useUpdateForm,
9
+ useUpdateFormSlug,
10
+ } from '../../api/resources/forms.ts'
5
11
  import { ErrorPanel } from '../../components/QueryState.tsx'
6
12
  import { SectionHeader } from '../../components/SectionHeader.tsx'
7
13
 
8
14
  /**
9
- * The form's identity: status, public heading, thank-you copy, and the public key.
15
+ * The form's identity: status, public heading, thank-you copy, and the slug.
10
16
  *
11
17
  * Every control commits on change, because each one is a single field and the
12
18
  * optimistic update in `createResourceHooks` puts the old value back if the
13
19
  * PATCH is refused. The field list is the exception and has its own Save; see
14
- * `FieldsEditor`. What the form *does* on submit — create records, tag them,
20
+ * `FieldsEditor`, and so is the slug, which asks first; see `SlugSetting`. What the form *does* on submit — create records, tag them,
15
21
  * add to lists, attach the submitter — lives on the Actions tab.
16
22
  */
17
23
 
@@ -32,7 +38,7 @@ export function FormSettings({ form }: FormSettingsProps): React.JSX.Element {
32
38
  <div className="max-w-xl space-y-4">
33
39
  <SectionHeader
34
40
  title="Settings"
35
- description="Status, the public form title, thank-you copy, and the public key."
41
+ description="Status, the public form title, thank-you copy, and the slug in its submit URL."
36
42
  />
37
43
 
38
44
  {updateForm.error !== null && <ErrorPanel error={updateForm.error} />}
@@ -90,14 +96,155 @@ export function FormSettings({ form }: FormSettingsProps): React.JSX.Element {
90
96
  <strong className="font-medium text-ink">Actions</strong> tab.
91
97
  </p>
92
98
 
93
- <Labelled label="Public key">
94
- <input className={`${inputClass} font-mono text-[12px]`} value={form.publicKey} readOnly />
95
- <Hint>The only credential the public submit takes. It never changes.</Hint>
96
- </Labelled>
99
+ {/* Keyed on the slug so the draft starts over whenever the stored slug moves. */}
100
+ <SlugSetting key={form.slug} form={form} />
97
101
  </div>
98
102
  )
99
103
  }
100
104
 
105
+ type SlugAction = 'change' | 'regenerate'
106
+
107
+ /**
108
+ * The form's slug, with an edit and a Regenerate button.
109
+ *
110
+ * The slug is only in the submit URL. Embed snippets use the form id, and the
111
+ * embed page carries the current submit URL, so embeds follow a change on their
112
+ * own. Anything that saved the old submit URL does not, which is the point of
113
+ * Regenerate. So, unlike the other settings, a change here does not commit on
114
+ * blur: both actions say what moves and wait for a confirm.
115
+ */
116
+ function SlugSetting({ form }: { readonly form: Form }): React.JSX.Element {
117
+ const updateSlug = useUpdateFormSlug()
118
+ const regenerateSlug = useRegenerateFormSlug()
119
+ const [draft, setDraft] = useState(form.slug)
120
+ const [confirming, setConfirming] = useState<SlugAction | null>(null)
121
+ const [lastError, setLastError] = useState<Error | null>(null)
122
+
123
+ const trimmed = draft.trim()
124
+ const edited = trimmed !== form.slug
125
+ const valid = FORM_SLUG_PATTERN.test(trimmed)
126
+ const isPending = updateSlug.isPending || regenerateSlug.isPending
127
+ const pendingAction: SlugAction | null = edited ? 'change' : confirming
128
+
129
+ const reset = (): void => {
130
+ setDraft(form.slug)
131
+ setConfirming(null)
132
+ setLastError(null)
133
+ }
134
+
135
+ const commit = async (): Promise<void> => {
136
+ setLastError(null)
137
+ try {
138
+ // Success moves `form.slug`, and the key on this component then resets it.
139
+ await (pendingAction === 'regenerate'
140
+ ? regenerateSlug.runAsync(form.id)
141
+ : updateSlug.runAsync({ id: form.id, slug: trimmed }))
142
+ } catch (error) {
143
+ setLastError(error instanceof Error ? error : new Error(String(error)))
144
+ }
145
+ }
146
+
147
+ return (
148
+ <div>
149
+ <label htmlFor="form-slug" className="mb-1.5 block text-[12px] font-medium text-ink">
150
+ Slug
151
+ </label>
152
+ <div className="flex gap-2">
153
+ <input
154
+ id="form-slug"
155
+ className={`${inputClass} font-mono text-[12px]`}
156
+ value={draft}
157
+ spellCheck={false}
158
+ autoComplete="off"
159
+ disabled={isPending}
160
+ aria-invalid={edited && !valid}
161
+ onChange={(event) => {
162
+ setDraft(event.target.value)
163
+ setConfirming(null)
164
+ setLastError(null)
165
+ }}
166
+ onKeyDown={(event) => {
167
+ if (event.key === 'Escape') {
168
+ reset()
169
+ }
170
+ }}
171
+ />
172
+ <button
173
+ type="button"
174
+ disabled={isPending || edited}
175
+ onClick={() => {
176
+ setConfirming('regenerate')
177
+ setLastError(null)
178
+ }}
179
+ className="shrink-0 rounded-md border border-border bg-surface-raised px-3 py-2 text-[12px] font-medium text-ink transition hover:bg-surface disabled:opacity-50"
180
+ >
181
+ Regenerate
182
+ </button>
183
+ </div>
184
+
185
+ {edited && !valid ? (
186
+ <p className="mt-1 text-[11px] text-danger">
187
+ Use 3 to 64 characters: letters, digits, hyphens (-) or underscores (_).
188
+ </p>
189
+ ) : (
190
+ <Hint>
191
+ The name of this form in its submit URL. It is not a secret: every page that embeds
192
+ the form shows it. If a bot sends spam to the form, regenerate the slug. Your
193
+ embeds keep working and use the new slug.
194
+ </Hint>
195
+ )}
196
+
197
+ {pendingAction !== null && (edited ? valid : true) && (
198
+ <div className="mt-2 rounded-md border border-border bg-surface px-3 py-2">
199
+ <p className="text-[12px] text-ink">
200
+ {pendingAction === 'regenerate'
201
+ ? 'Replace the slug with a new random one?'
202
+ : `Change the slug to "${trimmed}"?`}{' '}
203
+ Embeds on your sites change to the new slug in less than a minute. You do not
204
+ need to paste new code. Anything that sends to the old submit URL directly gets
205
+ an error.
206
+ </p>
207
+ <div className="mt-2 flex justify-end gap-2">
208
+ <button
209
+ type="button"
210
+ disabled={isPending}
211
+ onClick={reset}
212
+ className="rounded-md px-2 py-1 text-[12px] font-medium text-ink-muted hover:text-ink"
213
+ >
214
+ Cancel
215
+ </button>
216
+ <button
217
+ type="button"
218
+ disabled={isPending}
219
+ onClick={() => {
220
+ void commit()
221
+ }}
222
+ className="rounded-md bg-accent px-2.5 py-1 text-[12px] font-semibold text-accent-fg transition hover:bg-accent-hover disabled:opacity-50"
223
+ >
224
+ {isPending
225
+ ? 'Saving…'
226
+ : pendingAction === 'regenerate'
227
+ ? 'Regenerate slug'
228
+ : 'Change slug'}
229
+ </button>
230
+ </div>
231
+ </div>
232
+ )}
233
+
234
+ {lastError !== null && <p className="mt-1 text-[11px] text-danger">{slugErrorText(lastError)}</p>}
235
+ </div>
236
+ )
237
+ }
238
+
239
+ /** The field-level message for a refused slug write, when the API gives one. */
240
+ function slugErrorText(error: Error): string {
241
+ if (error instanceof ApiError && error.status === 409) {
242
+ return 'Another form in this workspace uses this slug. Choose a different one.'
243
+ }
244
+
245
+ return error.message
246
+ }
247
+
101
248
  function Labelled({
102
249
  label,
103
250
  children,
@@ -1,3 +1,4 @@
1
+ import { formFieldDisplayLabel } from '@kelpie/schemas'
1
2
  import type { Form, FormField, FormSubmission, FormSubmissionActionEntry } from '@kelpie/schemas'
2
3
  import { useMemo } from 'react'
3
4
  import { Link, useParams } from 'react-router'
@@ -112,7 +113,7 @@ function SubmissionDetailView({
112
113
  className="grid gap-0.5 px-3 py-2.5 sm:grid-cols-[10rem_1fr] sm:gap-3"
113
114
  >
114
115
  <dt className="text-[12px] font-medium text-ink-muted">
115
- {field.label}
116
+ {formFieldDisplayLabel(field)}
116
117
  {field.required && <span className="ml-0.5 text-danger">*</span>}
117
118
  </dt>
118
119
  <dd className="whitespace-pre-wrap text-[13px] text-ink">
@@ -1,3 +1,4 @@
1
+ import { formFieldDisplayLabel } from '@kelpie/schemas'
1
2
  import type { Form, FormSubmission, FormSubmissionActionEntry } from '@kelpie/schemas'
2
3
  import { useMemo } from 'react'
3
4
  import { Link, useNavigate } from 'react-router'
@@ -251,7 +252,7 @@ function summarise(form: Form, submission: FormSubmission): string {
251
252
  ? (field.options.find((option) => option.key === answer)?.value ?? answer)
252
253
  : answer
253
254
 
254
- return `${field.label}: ${shown}`
255
+ return `${formFieldDisplayLabel(field)}: ${shown}`
255
256
  })
256
257
  .filter((part): part is string => part !== undefined)
257
258
  .slice(0, 2)
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  FORM_FIELD_TYPES,
3
+ FORM_LIST_TARGET,
3
4
  isCompatibleFormFieldType,
4
5
  isKnownMapTarget,
5
6
  isRepeatableMapTarget,
@@ -35,7 +36,13 @@ export interface EditableField extends FormFieldInput {
35
36
  readonly id: string
36
37
  }
37
38
 
38
- export const FIELD_TYPE_OPTIONS = FORM_FIELD_TYPES.map((type) => ({ value: type, label: type }))
39
+ /** The type picker's text. Each type shows as its own name, except `list`. */
40
+ const FIELD_TYPE_LABELS: Partial<Record<FormFieldType, string>> = { list: 'add to list' }
41
+
42
+ export const FIELD_TYPE_OPTIONS = FORM_FIELD_TYPES.map((type) => ({
43
+ value: type,
44
+ label: FIELD_TYPE_LABELS[type] ?? type,
45
+ }))
39
46
 
40
47
  export interface FindProblemsOptions {
41
48
  readonly customFieldDefinitions?: readonly CustomFieldDefinitionRef[]
@@ -54,6 +61,8 @@ export function toEditableFields(form: Form): EditableField[] {
54
61
  statement: field.statement,
55
62
  consentPurposeIds: [...field.consentPurposeIds],
56
63
  consentPurposeLabels: { ...field.consentPurposeLabels },
64
+ listIds: [...field.listIds],
65
+ listLabels: { ...field.listLabels },
57
66
  }))
58
67
  }
59
68
 
@@ -96,9 +105,9 @@ export function editField(
96
105
  return fields.map((field) => {
97
106
  if (field.id !== id) {
98
107
  // Only one field may carry a CRM target, so taking one releases it
99
- // wherever it was. `submission` and `person.consent` are the targets
100
- // that may repeat — consent's uniqueness is per purpose, not per
101
- // target, and is enforced when the purpose is picked.
108
+ // wherever it was. `submission`, `person.consent` and `lists` are the
109
+ // targets that may repeat — consent's uniqueness is per purpose, not
110
+ // per target, and is enforced when the purpose is picked.
102
111
  return change.mapTo !== undefined &&
103
112
  !isRepeatableMapTarget(change.mapTo) &&
104
113
  field.mapTo === change.mapTo
@@ -106,19 +115,32 @@ export function editField(
106
115
  : field
107
116
  }
108
117
 
109
- const next: EditableField = { ...field, ...change }
110
-
111
- // A consent or notice field must map to person.consent; anything else
112
- // clears any lingering purpose list.
118
+ let next: EditableField = { ...field, ...change }
113
119
  const isConsentLike = next.type === 'consent' || next.type === 'notice'
120
+ const isList = next.type === 'list'
121
+
122
+ // Only a consent or notice field carries purposes, and only a list field
123
+ // carries lists. A field that changes type drops the ones it left behind.
124
+ if (!isConsentLike && (next.consentPurposeIds ?? []).length > 0) {
125
+ next = { ...next, consentPurposeIds: [], consentPurposeLabels: {} }
126
+ }
127
+ if (!isList && (next.listIds ?? []).length > 0) {
128
+ next = { ...next, listIds: [], listLabels: {} }
129
+ }
130
+
131
+ // A list field maps to `lists`, and a consent or notice field to
132
+ // person.consent. Each of those targets takes only its own types.
133
+ if (isList && next.mapTo !== FORM_LIST_TARGET) {
134
+ return { ...withoutOptions(next), mapTo: FORM_LIST_TARGET }
135
+ }
114
136
  if (isConsentLike && next.mapTo !== PERSON_CONSENT_TARGET) {
115
137
  return { ...withoutOptions(next), mapTo: PERSON_CONSENT_TARGET }
116
138
  }
117
139
  if (next.mapTo === PERSON_CONSENT_TARGET && !isConsentLike) {
118
140
  return { ...withoutOptions(next), type: 'consent' }
119
141
  }
120
- if (!isConsentLike && (next.consentPurposeIds ?? []).length > 0) {
121
- return withoutOptions({ ...next, consentPurposeIds: [] })
142
+ if (next.mapTo === FORM_LIST_TARGET && !isList) {
143
+ return { ...withoutOptions(next), type: 'list' }
122
144
  }
123
145
 
124
146
  if (next.type !== 'select') {
@@ -242,6 +264,13 @@ export function findProblems(
242
264
  }
243
265
  }
244
266
  }
267
+ } else if (field.mapTo === FORM_LIST_TARGET) {
268
+ const lists = field.listIds ?? []
269
+ if (lists.length === 0) {
270
+ byField.set(field.id, 'Pick at least one list this field offers.')
271
+ } else if (new Set(lists).size !== lists.length) {
272
+ byField.set(field.id, 'This field lists a list twice.')
273
+ }
245
274
  } else if (!isRepeatableMapTarget(field.mapTo)) {
246
275
  if (seen.has(field.mapTo)) {
247
276
  byField.set(field.id, `Another field already maps to ${field.mapTo}.`)
@@ -130,6 +130,19 @@ export const SUBMISSION_FIELD_PRESETS: readonly SubmissionFieldPreset[] = [
130
130
  consentPurposeLabels: {},
131
131
  },
132
132
  },
133
+ {
134
+ menuLabel: 'Add to list',
135
+ field: {
136
+ label: 'Add to list',
137
+ type: 'list',
138
+ required: false,
139
+ mapTo: 'lists',
140
+ placeholder: null,
141
+ statement: null,
142
+ listIds: [],
143
+ listLabels: {},
144
+ },
145
+ },
133
146
  {
134
147
  menuLabel: 'Notice',
135
148
  field: {