@kelpie/ui 0.19.0 → 0.20.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/dist/api/resources/forms.d.ts +21 -2
  2. package/dist/api/resources/forms.d.ts.map +1 -1
  3. package/dist/api/resources/forms.js +51 -4
  4. package/dist/api/resources/forms.js.map +1 -1
  5. package/dist/pages/FormDetail.d.ts.map +1 -1
  6. package/dist/pages/FormDetail.js +2 -1
  7. package/dist/pages/FormDetail.js.map +1 -1
  8. package/dist/pages/FormsPage.d.ts.map +1 -1
  9. package/dist/pages/FormsPage.js +5 -1
  10. package/dist/pages/FormsPage.js.map +1 -1
  11. package/dist/pages/forms/EmbedPanel.d.ts +5 -0
  12. package/dist/pages/forms/EmbedPanel.d.ts.map +1 -1
  13. package/dist/pages/forms/EmbedPanel.js +79 -14
  14. package/dist/pages/forms/EmbedPanel.js.map +1 -1
  15. package/dist/pages/forms/FormActions.d.ts.map +1 -1
  16. package/dist/pages/forms/FormActions.js +2 -1
  17. package/dist/pages/forms/FormActions.js.map +1 -1
  18. package/dist/pages/forms/FormEmails.d.ts +6 -0
  19. package/dist/pages/forms/FormEmails.d.ts.map +1 -0
  20. package/dist/pages/forms/FormEmails.js +173 -0
  21. package/dist/pages/forms/FormEmails.js.map +1 -0
  22. package/dist/pages/forms/FormSettings.d.ts.map +1 -1
  23. package/dist/pages/forms/FormSettings.js +5 -3
  24. package/dist/pages/forms/FormSettings.js.map +1 -1
  25. package/dist/pages/forms/SubmissionDetail.d.ts.map +1 -1
  26. package/dist/pages/forms/SubmissionDetail.js +22 -3
  27. package/dist/pages/forms/SubmissionDetail.js.map +1 -1
  28. package/dist/pages/forms/SubmissionsTable.d.ts +3 -1
  29. package/dist/pages/forms/SubmissionsTable.d.ts.map +1 -1
  30. package/dist/pages/forms/SubmissionsTable.js +48 -11
  31. package/dist/pages/forms/SubmissionsTable.js.map +1 -1
  32. package/dist/pages/forms/embedPrompts.d.ts +45 -0
  33. package/dist/pages/forms/embedPrompts.d.ts.map +1 -0
  34. package/dist/pages/forms/embedPrompts.js +174 -0
  35. package/dist/pages/forms/embedPrompts.js.map +1 -0
  36. package/package.json +2 -2
  37. package/src/api/resources/forms.ts +75 -4
  38. package/src/pages/FormDetail.tsx +4 -1
  39. package/src/pages/FormsPage.tsx +5 -1
  40. package/src/pages/forms/EmbedPanel.tsx +238 -34
  41. package/src/pages/forms/FormActions.tsx +4 -1
  42. package/src/pages/forms/FormEmails.tsx +449 -0
  43. package/src/pages/forms/FormSettings.tsx +23 -4
  44. package/src/pages/forms/SubmissionDetail.tsx +64 -2
  45. package/src/pages/forms/SubmissionsTable.tsx +123 -16
  46. package/src/pages/forms/embedPrompts.ts +257 -0
@@ -1,10 +1,14 @@
1
- import type { Form } from '@kelpie/schemas'
2
- import { useEffect, useId, useState } from 'react'
1
+ import type { ConsentPurpose, Form } from '@kelpie/schemas'
2
+ import { useEffect, useId, useMemo, useState, type ReactNode } from 'react'
3
3
 
4
+ import { useConsentPurposes } from '../../api/resources/consentPurposes.ts'
4
5
  import { useFormEmbed } from '../../api/resources/forms.ts'
6
+ import { useLists } from '../../api/resources/lists.ts'
7
+ import { useWorkspace } from '../../api/resources/workspace.ts'
5
8
  import { CopyButton } from '../../components/CopyButton.tsx'
6
9
  import { ErrorPanel, LoadingPanel } from '../../components/QueryState.tsx'
7
10
  import { SectionHeader } from '../../components/SectionHeader.tsx'
11
+ import { buildEmbedPrompts, buildJsonSubmitPrompt } from './embedPrompts.ts'
8
12
 
9
13
  /**
10
14
  * What to paste into a website.
@@ -17,6 +21,11 @@ import { SectionHeader } from '../../components/SectionHeader.tsx'
17
21
  * Each iframe snippet has a Preview that opens the bare embed document in a
18
22
  * modal — fields only, the same document the snippet iframes — fixed height for
19
23
  * the plain iframe, auto-resize for the script variant.
24
+ *
25
+ * Each option also has an AI button that opens, in a modal, a prompt for a
26
+ * coding assistant to put that option on the person's site. The fourth option,
27
+ * Submit as JSON, is for a site that builds its own form and posts to the
28
+ * public submit endpoint; its prompt lists the fields the answers are keyed by.
20
29
  */
21
30
 
22
31
  export interface EmbedPanelProps {
@@ -25,9 +34,17 @@ export interface EmbedPanelProps {
25
34
 
26
35
  type PreviewMode = 'fixed' | 'resize'
27
36
 
37
+ interface AiPrompt {
38
+ /** Which embed option the prompt is for: "iframe". */
39
+ readonly label: string
40
+ readonly prompt: string
41
+ }
42
+
28
43
  export function EmbedPanel({ form }: EmbedPanelProps): React.JSX.Element {
29
44
  const { snippets, isLoading, error } = useFormEmbed(form.id)
30
45
  const [preview, setPreview] = useState<PreviewMode | null>(null)
46
+ const [aiPrompt, setAiPrompt] = useState<AiPrompt | null>(null)
47
+ const checkboxText = useCheckboxText()
31
48
 
32
49
  if (error !== null) {
33
50
  return <ErrorPanel error={error} />
@@ -37,6 +54,14 @@ export function EmbedPanel({ form }: EmbedPanelProps): React.JSX.Element {
37
54
  return <LoadingPanel label="Loading embed details…" />
38
55
  }
39
56
 
57
+ const formName = form.title.trim().length > 0 ? form.title : form.name
58
+ const prompts = buildEmbedPrompts({
59
+ formName,
60
+ url: snippets.url,
61
+ iframeSnippet: snippets.iframeSnippet,
62
+ scriptSnippet: snippets.scriptSnippet,
63
+ })
64
+
40
65
  return (
41
66
  <div className="max-w-2xl space-y-5">
42
67
  <SectionHeader
@@ -58,6 +83,12 @@ export function EmbedPanel({ form }: EmbedPanelProps): React.JSX.Element {
58
83
  >
59
84
  Open
60
85
  </a>
86
+ <AiPromptButton
87
+ label="hosted page"
88
+ onOpen={() => {
89
+ setAiPrompt({ label: 'Hosted page', prompt: prompts.hosted })
90
+ }}
91
+ />
61
92
  </div>
62
93
  </div>
63
94
 
@@ -68,6 +99,9 @@ export function EmbedPanel({ form }: EmbedPanelProps): React.JSX.Element {
68
99
  onPreview={() => {
69
100
  setPreview('fixed')
70
101
  }}
102
+ onAiPrompt={() => {
103
+ setAiPrompt({ label: 'iframe', prompt: prompts.iframe })
104
+ }}
71
105
  />
72
106
  <Snippet
73
107
  label="iframe with auto-resize"
@@ -76,8 +110,51 @@ export function EmbedPanel({ form }: EmbedPanelProps): React.JSX.Element {
76
110
  onPreview={() => {
77
111
  setPreview('resize')
78
112
  }}
113
+ onAiPrompt={() => {
114
+ setAiPrompt({ label: 'iframe with auto-resize', prompt: prompts.script })
115
+ }}
79
116
  />
80
117
 
118
+ <div>
119
+ <div className="mb-1.5 flex items-center justify-between gap-2">
120
+ <span className="text-[12px] font-medium text-ink">Submit as JSON</span>
121
+ <div className="flex items-center gap-2">
122
+ <AiPromptButton
123
+ label="JSON submit"
124
+ onOpen={() => {
125
+ setAiPrompt({
126
+ label: 'Submit as JSON',
127
+ prompt: buildJsonSubmitPrompt({
128
+ formName,
129
+ submitUrl: snippets.submitUrl,
130
+ tokenUrl: form.requireSpamCheck ? snippets.tokenUrl : null,
131
+ fields: form.fields,
132
+ thankYouMessage: form.thankYouMessage,
133
+ ...checkboxText,
134
+ }),
135
+ })
136
+ }}
137
+ />
138
+ <CopyButton value={snippets.submitUrl} label="Copy the JSON submit endpoint" />
139
+ </div>
140
+ </div>
141
+ <code className="block overflow-x-auto whitespace-nowrap rounded-md border border-border bg-surface-raised px-3 py-2 font-mono text-[12px] text-ink">
142
+ POST {snippets.submitUrl}
143
+ </code>
144
+ <p className="mt-1 text-[11px] text-ink-faint">
145
+ For a form you build yourself. Post {'{ "answers": { "<field id>": "…" } }'} with no
146
+ credentials. The URL holds the slug, so it changes when the slug does.
147
+ </p>
148
+ {form.requireSpamCheck && (
149
+ <p className="mt-1 text-[11px] text-warning">
150
+ This form requires the spam check. Your own form must first get a token from{' '}
151
+ <code className="font-mono">GET {snippets.tokenUrl}</code> and send it as{' '}
152
+ <code className="font-mono">token</code>, or each submission is held as spam. The AI
153
+ prompt has the full steps.
154
+ </p>
155
+ )}
156
+ </div>
157
+
81
158
  {preview !== null && (
82
159
  <EmbedPreviewModal
83
160
  formId={form.id}
@@ -90,6 +167,16 @@ export function EmbedPanel({ form }: EmbedPanelProps): React.JSX.Element {
90
167
  }}
91
168
  />
92
169
  )}
170
+
171
+ {aiPrompt !== null && (
172
+ <AiPromptModal
173
+ label={aiPrompt.label}
174
+ prompt={aiPrompt.prompt}
175
+ onClose={() => {
176
+ setAiPrompt(null)
177
+ }}
178
+ />
179
+ )}
93
180
  </div>
94
181
  )
95
182
  }
@@ -99,11 +186,13 @@ function Snippet({
99
186
  hint,
100
187
  value,
101
188
  onPreview,
189
+ onAiPrompt,
102
190
  }: {
103
191
  readonly label: string
104
192
  readonly hint: string
105
193
  readonly value: string
106
194
  readonly onPreview: () => void
195
+ readonly onAiPrompt: () => void
107
196
  }): React.JSX.Element {
108
197
  return (
109
198
  <div>
@@ -117,6 +206,7 @@ function Snippet({
117
206
  >
118
207
  Preview
119
208
  </button>
209
+ <AiPromptButton label={label} onOpen={onAiPrompt} />
120
210
  <CopyButton value={value} label={`Copy the ${label} snippet`} />
121
211
  </div>
122
212
  </div>
@@ -132,6 +222,86 @@ function Snippet({
132
222
  )
133
223
  }
134
224
 
225
+ /**
226
+ * What the JSON prompt needs to name each consent and list checkbox the way the
227
+ * embed does: the workspace's purposes, its person and company lists, and its
228
+ * name for the `{{workspace}}` token in a purpose statement.
229
+ */
230
+ function useCheckboxText(): {
231
+ readonly consentPurposes: ReadonlyMap<string, ConsentPurpose>
232
+ readonly listNames: ReadonlyMap<string, string>
233
+ readonly workspaceName: string
234
+ } {
235
+ const purposes = useConsentPurposes({ sort: 'sort_order', limit: 200 })
236
+ const personLists = useLists({ targetType: 'person' })
237
+ const companyLists = useLists({ targetType: 'company' })
238
+ const { workspace } = useWorkspace()
239
+ const consentPurposes = useMemo(
240
+ () => new Map(purposes.records.map((purpose): [string, ConsentPurpose] => [purpose.id, purpose])),
241
+ [purposes.records],
242
+ )
243
+ const listNames = useMemo(
244
+ () =>
245
+ new Map(
246
+ [...personLists.records, ...companyLists.records].map((list): [string, string] => [list.id, list.name]),
247
+ ),
248
+ [personLists.records, companyLists.records],
249
+ )
250
+
251
+ return { consentPurposes, listNames, workspaceName: workspace?.name ?? '' }
252
+ }
253
+
254
+ function AiPromptButton({
255
+ label,
256
+ onOpen,
257
+ }: {
258
+ readonly label: string
259
+ readonly onOpen: () => void
260
+ }): React.JSX.Element {
261
+ return (
262
+ <button
263
+ type="button"
264
+ onClick={onOpen}
265
+ aria-label={`Show an AI prompt to add the ${label} to your site`}
266
+ className="rounded-md border border-border px-2 py-1 text-[11px] font-medium text-ink-muted transition hover:border-border-strong hover:text-ink"
267
+ >
268
+ AI
269
+ </button>
270
+ )
271
+ }
272
+
273
+ /**
274
+ * The prompt for one embed option, read-only, with a Copy button. The person
275
+ * can read what they are about to hand an assistant before they copy it.
276
+ */
277
+ function AiPromptModal({
278
+ label,
279
+ prompt,
280
+ onClose,
281
+ }: {
282
+ readonly label: string
283
+ readonly prompt: string
284
+ readonly onClose: () => void
285
+ }): React.JSX.Element {
286
+ return (
287
+ <ModalFrame
288
+ title={`AI prompt · ${label}`}
289
+ description="Paste this into an AI coding assistant to add the form to your site."
290
+ actions={<CopyButton value={prompt} label={`Copy the ${label} AI prompt`} />}
291
+ bodyClassName="p-4"
292
+ onClose={onClose}
293
+ >
294
+ <textarea
295
+ readOnly
296
+ value={prompt}
297
+ rows={prompt.split('\n').length + 1}
298
+ aria-label={`${label} AI prompt`}
299
+ className="w-full resize-y rounded-md border border-border bg-surface px-3 py-2 font-mono text-[11px] text-ink outline-none"
300
+ />
301
+ </ModalFrame>
302
+ )
303
+ }
304
+
135
305
  /**
136
306
  * Frames the hosted embed the way each snippet would: fixed 720px for the plain
137
307
  * iframe, or listening for the height postMessage when previewing auto-resize.
@@ -151,7 +321,6 @@ function EmbedPreviewModal({
151
321
  readonly mode: PreviewMode
152
322
  readonly onClose: () => void
153
323
  }): React.JSX.Element {
154
- const titleId = useId()
155
324
  const heading = formTitle.trim().length > 0 ? formTitle : formName
156
325
  const [height, setHeight] = useState(720)
157
326
 
@@ -188,6 +357,59 @@ function EmbedPreviewModal({
188
357
  }
189
358
  }, [formId, mode, url])
190
359
 
360
+ return (
361
+ <ModalFrame
362
+ title={`Preview · ${heading}`}
363
+ description={
364
+ mode === 'resize'
365
+ ? 'Bare iframe embed with auto-resize — fields only, height follows the form.'
366
+ : 'Bare iframe embed — fields only, fixed height.'
367
+ }
368
+ bodyClassName="bg-[#f4f4f5] p-4"
369
+ onClose={onClose}
370
+ >
371
+ <div className="overflow-hidden rounded-md border border-[#d4d4d8] bg-white shadow-sm">
372
+ <iframe
373
+ src={url}
374
+ title={`${heading} preview`}
375
+ style={{
376
+ width: '100%',
377
+ border: 0,
378
+ height: mode === 'fixed' ? '720px' : `${String(height)}px`,
379
+ colorScheme: 'normal',
380
+ }}
381
+ className="block bg-transparent"
382
+ />
383
+ </div>
384
+ <p className="mt-2 text-center text-[11px] text-ink-faint">
385
+ Simulated host page — the iframe uses its own neutral styles, not Kelpie&apos;s.
386
+ </p>
387
+ </ModalFrame>
388
+ )
389
+ }
390
+
391
+ /**
392
+ * The dialog shell both modals share: backdrop and Escape close it, a header
393
+ * with a title, a description and optional actions beside Close, and a
394
+ * scrolling body.
395
+ */
396
+ function ModalFrame({
397
+ title,
398
+ description,
399
+ actions,
400
+ bodyClassName,
401
+ onClose,
402
+ children,
403
+ }: {
404
+ readonly title: string
405
+ readonly description: string
406
+ readonly actions?: ReactNode
407
+ readonly bodyClassName: string
408
+ readonly onClose: () => void
409
+ readonly children: ReactNode
410
+ }): React.JSX.Element {
411
+ const titleId = useId()
412
+
191
413
  useEffect(() => {
192
414
  function onKeyDown(event: KeyboardEvent): void {
193
415
  if (event.key === 'Escape') {
@@ -219,41 +441,23 @@ function EmbedPreviewModal({
219
441
  <div className="flex shrink-0 items-start justify-between gap-3 border-b border-border px-4 py-3">
220
442
  <div className="min-w-0">
221
443
  <h2 id={titleId} className="truncate text-[14px] font-semibold text-ink">
222
- Preview · {heading}
444
+ {title}
223
445
  </h2>
224
- <p className="mt-0.5 text-[11px] text-ink-faint">
225
- {mode === 'resize'
226
- ? 'Bare iframe embed with auto-resize — fields only, height follows the form.'
227
- : 'Bare iframe embed — fields only, fixed height.'}
228
- </p>
446
+ <p className="mt-0.5 text-[11px] text-ink-faint">{description}</p>
229
447
  </div>
230
- <button
231
- type="button"
232
- onClick={onClose}
233
- className="shrink-0 rounded-md px-2 py-1 text-[12px] font-medium text-ink-muted transition hover:text-ink"
234
- >
235
- Close
236
- </button>
237
- </div>
238
-
239
- <div className="min-h-0 flex-1 overflow-y-auto bg-[#f4f4f5] p-4">
240
- <div className="overflow-hidden rounded-md border border-[#d4d4d8] bg-white shadow-sm">
241
- <iframe
242
- src={url}
243
- title={`${heading} preview`}
244
- style={{
245
- width: '100%',
246
- border: 0,
247
- height: mode === 'fixed' ? '720px' : `${String(height)}px`,
248
- colorScheme: 'normal',
249
- }}
250
- className="block bg-transparent"
251
- />
448
+ <div className="flex shrink-0 items-center gap-2">
449
+ {actions}
450
+ <button
451
+ type="button"
452
+ onClick={onClose}
453
+ className="rounded-md px-2 py-1 text-[12px] font-medium text-ink-muted transition hover:text-ink"
454
+ >
455
+ Close
456
+ </button>
252
457
  </div>
253
- <p className="mt-2 text-center text-[11px] text-ink-faint">
254
- Simulated host page — the iframe uses its own neutral styles, not Kelpie&apos;s.
255
- </p>
256
458
  </div>
459
+
460
+ <div className={`min-h-0 flex-1 overflow-y-auto ${bodyClassName}`}>{children}</div>
257
461
  </div>
258
462
  </div>
259
463
  )
@@ -16,6 +16,7 @@ import { Chip } from '../../components/Chip.tsx'
16
16
  import { ErrorPanel } from '../../components/QueryState.tsx'
17
17
  import { SectionHeader } from '../../components/SectionHeader.tsx'
18
18
  import { toTags } from '../fields.ts'
19
+ import { FormEmails } from './FormEmails.tsx'
19
20
 
20
21
  /**
21
22
  * What the form does with a submission, once the answers are captured.
@@ -49,7 +50,7 @@ export function FormActions({ form }: FormActionsProps): React.JSX.Element {
49
50
  <div className="max-w-2xl space-y-6">
50
51
  <SectionHeader
51
52
  title="Actions"
52
- description="What the form creates, tags, lists, and links every submitter to."
53
+ description="What the form creates, tags, lists, links every submitter to, and emails."
53
54
  />
54
55
 
55
56
  {updateForm.error !== null && <ErrorPanel error={updateForm.error} />}
@@ -131,6 +132,8 @@ export function FormActions({ form }: FormActionsProps): React.JSX.Element {
131
132
  <ListsBlock form={form} onChange={(next) => patch({ listIds: next })} />
132
133
 
133
134
  <AttachTargetsBlock form={form} onChange={(next) => patch({ attachTargets: next })} />
135
+
136
+ <FormEmails form={form} />
134
137
  </div>
135
138
  )
136
139
  }