@kelpie/ui 0.19.0 → 0.21.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
@@ -0,0 +1,174 @@
1
+ import { consentCheckboxText, formFieldDisplayLabel } from '@kelpie/schemas';
2
+ export function buildEmbedPrompts({ formName, url, iframeSnippet, scriptSnippet, }) {
3
+ return {
4
+ hosted: [
5
+ `I want to link to my "${formName}" form from my website. The form is hosted by Kelpie, a CRM, at this URL:`,
6
+ '',
7
+ url,
8
+ '',
9
+ 'Please:',
10
+ '1. Look at my site and suggest where a link or button to this form fits best (for example the navigation, footer, or a contact section). Ask me if it is not clear.',
11
+ "2. Add the link using my site's existing components and styles for links or buttons.",
12
+ '3. Use the URL exactly as given. Do not change or rebuild it.',
13
+ '4. Decide with me whether it opens in the same tab or a new tab. If a new tab, add rel="noopener noreferrer".',
14
+ '',
15
+ 'Do not rebuild the form yourself and do not send submissions anywhere else. The hosted page handles submission.',
16
+ ].join('\n'),
17
+ iframe: [
18
+ `I want to embed my "${formName}" form on my website. The form is hosted by Kelpie, a CRM. This is the embed code:`,
19
+ '',
20
+ '```html',
21
+ iframeSnippet,
22
+ '```',
23
+ '',
24
+ 'Please:',
25
+ '1. Find the page where the form belongs. Ask me if it is not clear.',
26
+ "2. Add the iframe in the way my site's framework expects (for example JSX attributes and a style object in React, or a component in Vue or Svelte).",
27
+ '3. Keep the src URL exactly as given. Keep width 100% and no border. The height is fixed; change it only if the form is cut off or leaves a large gap.',
28
+ "4. Place it inside my page's existing layout so it matches the content width.",
29
+ '5. If my site sets a Content-Security-Policy, add the origin of the src URL to frame-src.',
30
+ '',
31
+ 'Do not rebuild the form yourself and do not send submissions anywhere else. The iframe handles submission.',
32
+ ].join('\n'),
33
+ script: [
34
+ `I want to embed my "${formName}" form on my website so that it resizes to fit its content. The form is hosted by Kelpie, a CRM. This is the embed code, an iframe and a script that listens for height messages from it:`,
35
+ '',
36
+ '```html',
37
+ scriptSnippet,
38
+ '```',
39
+ '',
40
+ 'Please:',
41
+ '1. Find the page where the form belongs. Ask me if it is not clear.',
42
+ "2. Add the iframe and the listener in the way my site's framework expects. In a component framework such as React, Vue or Svelte, register the message listener when the component mounts and remove it when it unmounts, and use a ref for the iframe instead of getElementById.",
43
+ '3. Keep the src URL exactly as given. Keep the origin check and the formId check in the listener exactly as given. They stop other pages from resizing the frame.',
44
+ "4. Place it inside my page's existing layout so it matches the content width.",
45
+ '5. If my site sets a Content-Security-Policy, add the origin of the src URL to frame-src, and allow the inline script or move it into a script file.',
46
+ '',
47
+ 'Do not rebuild the form yourself and do not send submissions anywhere else. The iframe handles submission.',
48
+ ].join('\n'),
49
+ };
50
+ }
51
+ export function buildJsonSubmitPrompt({ formName, submitUrl, tokenUrl, fields, thankYouMessage, consentPurposes, listNames, workspaceName, }) {
52
+ const ordered = [...fields].sort((left, right) => left.sortOrder - right.sortOrder);
53
+ const describe = (field) => describeField(field, consentPurposes, listNames, workspaceName);
54
+ const example = Object.fromEntries(ordered
55
+ .filter((field) => field.type !== 'notice')
56
+ .map((field) => [field.id, exampleAnswer(field)]));
57
+ return [
58
+ `I want to build my own form on my website that sends its answers to my "${formName}" form in Kelpie, a CRM, as JSON.`,
59
+ '',
60
+ '## Endpoint',
61
+ '',
62
+ `POST ${submitUrl}`,
63
+ 'Content-Type: application/json',
64
+ '',
65
+ 'Send no API key, cookie or other credential. The endpoint accepts requests from any origin, so the browser can call it directly.',
66
+ '',
67
+ '## Request body',
68
+ '',
69
+ '```json',
70
+ JSON.stringify({ answers: example }, null, 2),
71
+ '```',
72
+ '',
73
+ 'The values above are examples. Rules for `answers`:',
74
+ '- The keys are the field ids below. An unknown key makes the request fail.',
75
+ '- Every value is a string.',
76
+ '- If the visitor gives no answer, leave the key out. A required field must have a value.',
77
+ '- Select: send the option key, not the text the visitor sees.',
78
+ '- Consent and "Add to list": send the ids of the ticked boxes as one comma-separated string, for example "id1,id2". Show the statement and the checkbox text exactly as given below.',
79
+ '- Notice: show the text. Send no answer for it.',
80
+ '',
81
+ '## Fields, in display order',
82
+ '',
83
+ ...ordered.map(describe),
84
+ '',
85
+ ...(tokenUrl === null ? [] : spamCheckSection(tokenUrl)),
86
+ '## Responses',
87
+ '',
88
+ '- 201: `{ "id", "form_id", "submitted_at", "thank_you_message" }`. Replace the form with `thank_you_message`.' +
89
+ (thankYouMessage.trim().length > 0 ? ` It is currently: "${thankYouMessage}"` : ''),
90
+ '- 422 `validation_failed`: `{ "error": { "code", "message", "details": [{ "field": "answers.<field id>", "message" }] } }`. Show each detail message next to its field, and `error.message` above the form.',
91
+ '- 404 (form not found), 409 (form paused) and 429 (too many requests): `{ "error": { "code", "message" } }`. Show `error.message`.',
92
+ '',
93
+ '## Please',
94
+ '',
95
+ '1. Find the page where the form belongs. Ask me if it is not clear.',
96
+ "2. Build the form with my site's existing components and styles: one input for each field above, in the order given. Use an email input for email fields, a textarea for textarea fields, a select for select fields, and checkboxes for consent and \"Add to list\" fields.",
97
+ "3. Submit with fetch or my framework's usual data layer. Disable the submit button while the request is pending, and show the responses as described above.",
98
+ '4. Keep the endpoint URL exactly as given. If my site sets a Content-Security-Policy, add the origin of the endpoint to connect-src.',
99
+ tokenUrl === null
100
+ ? '5. Do not add credentials, extra headers or extra keys to the request.'
101
+ : '5. Do not add credentials or extra headers to the request. The only keys in the body are `answers`, `token` and `trap`.',
102
+ '',
103
+ 'The endpoint URL contains the form\'s slug. If the slug changes in Kelpie, the URL changes. If fields change in Kelpie, this field list changes. In both cases, copy a new prompt from Kelpie.',
104
+ ].join('\n');
105
+ }
106
+ /**
107
+ * What a site's own form must do for a form that requires the spam check.
108
+ * Without it Kelpie holds every submission as spam, and the response does not
109
+ * say so, which is why the prompt says it plainly.
110
+ */
111
+ function spamCheckSection(tokenUrl) {
112
+ return [
113
+ '## Spam check',
114
+ '',
115
+ 'This form requires a spam check. If the request does not pass it, Kelpie still answers 201, but it holds the submission as spam and my team does not see it as a lead. Do all three steps.',
116
+ '',
117
+ `1. When the page that shows the form loads, send \`GET ${tokenUrl}\` with no credentials. The response is \`{ "token": "…" }\`. Keep the token. Do not get it at submit time: Kelpie holds a submit that arrives less than a few seconds after its token was issued. A token is good for 24 hours.`,
118
+ '2. Add `"token": "<the token>"` to the request body, beside `answers`.',
119
+ '3. Add one extra text input to the form that people cannot see or reach: move it off screen with CSS (do not use `display: none`), and give it `tabindex="-1"`, `autocomplete="off"` and `aria-hidden="true"` on its wrapper. If it has a value at submit time, add `"trap": "<its value>"` to the request body. A person leaves it empty, so leave the key out.',
120
+ '',
121
+ 'If my Kelpie uses a CAPTCHA provider, my own form cannot pass the check. Tell me to use the Kelpie iframe embed instead.',
122
+ '',
123
+ ];
124
+ }
125
+ function describeField(field, consentPurposes, listNames, workspaceName) {
126
+ const label = field.label.trim();
127
+ const heading = label.length > 0 ? `"${label}"` : `no visible label (holds ${formFieldDisplayLabel(field)})`;
128
+ const lines = [`- \`${field.id}\`: ${heading}. Type ${field.type}, ${field.required ? 'required' : 'optional'}.`];
129
+ if (field.placeholder !== null && field.placeholder.trim().length > 0) {
130
+ lines.push(` Placeholder: "${field.placeholder}"`);
131
+ }
132
+ if (field.type === 'select') {
133
+ lines.push(' Options (key: text shown):');
134
+ lines.push(...field.options.map((option) => ` - \`${option.key}\`: "${option.value}"`));
135
+ }
136
+ if (field.type === 'notice') {
137
+ lines.push(` Text: "${field.statement ?? ''}"`);
138
+ }
139
+ if (field.type === 'consent') {
140
+ lines.push(` Statement: "${field.statement ?? field.label}"`);
141
+ lines.push(' Checkboxes (id: text shown):');
142
+ lines.push(...field.consentPurposeIds.map((id) => {
143
+ const purpose = consentPurposes.get(id) ?? { label: id, statement: '' };
144
+ const text = consentCheckboxText(field.consentPurposeLabels[id], purpose, workspaceName);
145
+ return ` - \`${id}\`: "${text}"`;
146
+ }));
147
+ }
148
+ if (field.type === 'list') {
149
+ const statement = (field.statement ?? '').trim();
150
+ if (statement.length > 0) {
151
+ lines.push(` Statement: "${statement}"`);
152
+ }
153
+ lines.push(' Checkboxes (id: text shown):');
154
+ lines.push(...field.listIds.map((id) => ` - \`${id}\`: "${field.listLabels[id] ?? listNames.get(id) ?? id}"`));
155
+ }
156
+ return lines.join('\n');
157
+ }
158
+ function exampleAnswer(field) {
159
+ switch (field.type) {
160
+ case 'email':
161
+ return 'jane@example.com';
162
+ case 'select':
163
+ return field.options[0]?.key ?? '';
164
+ case 'consent':
165
+ return field.consentPurposeIds.join(',');
166
+ case 'list':
167
+ return field.listIds.join(',');
168
+ case 'textarea':
169
+ return 'A longer answer.';
170
+ default:
171
+ return 'Example';
172
+ }
173
+ }
174
+ //# sourceMappingURL=embedPrompts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embedPrompts.js","sourceRoot":"","sources":["../../../src/pages/forms/embedPrompts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AA+B5E,MAAM,UAAU,iBAAiB,CAAC,EAChC,QAAQ,EACR,GAAG,EACH,aAAa,EACb,aAAa,GACI;IACjB,OAAO;QACL,MAAM,EAAE;YACN,yBAAyB,QAAQ,2EAA2E;YAC5G,EAAE;YACF,GAAG;YACH,EAAE;YACF,SAAS;YACT,qKAAqK;YACrK,sFAAsF;YACtF,+DAA+D;YAC/D,+GAA+G;YAC/G,EAAE;YACF,iHAAiH;SAClH,CAAC,IAAI,C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CAAC,KAAgB;IACrC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,OAAO;YACV,OAAO,kBAAkB,CAAA;QAC3B,KAAK,QAAQ;YACX,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,CAAA;QACpC,KAAK,SAAS;YACZ,OAAO,KAAK,CAAC,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAC1C,KAAK,MAAM;YACT,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAChC,KAAK,UAAU;YACb,OAAO,kBAAkB,CAAA;QAC3B;YACE,OAAO,SAAS,CAAA;IACpB,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kelpie/ui",
3
- "version": "0.19.0",
3
+ "version": "0.21.0",
4
4
  "description": "React UI for Kelpie: components, pages, API client, and the extension registry an assembly composes modules through.",
5
5
  "keywords": [
6
6
  "kelpie",
@@ -48,7 +48,7 @@
48
48
  "@dnd-kit/core": "^6.3.1",
49
49
  "@dnd-kit/sortable": "^10.0.0",
50
50
  "@dnd-kit/utilities": "^3.2.2",
51
- "@kelpie/schemas": "^0.19.0",
51
+ "@kelpie/schemas": "^0.21.0",
52
52
  "@tanstack/react-query": "^5.101.4",
53
53
  "react-markdown": "^10.1.0",
54
54
  "react-router": "^8.3.0",
@@ -6,6 +6,7 @@ import type {
6
6
  FormInput,
7
7
  FormSubmission,
8
8
  FormSubmissionLinkTarget,
9
+ FormSubmissionStatus,
9
10
  } from '@kelpie/schemas'
10
11
  import {
11
12
  useMutation,
@@ -193,17 +194,79 @@ export function useDeleteForm(): MutationResult<string, void> {
193
194
  * this package is a top-level collection with a fixed path, and generalising the
194
195
  * factory for the one nested list would complicate five modules to shorten one.
195
196
  * The result shape is the same, so a page cannot tell the difference.
197
+ *
198
+ * `status` picks the list: `accepted` is what arrived, `spam` is what the spam
199
+ * check held. They are two lists on the server too, never one.
196
200
  */
197
- export function useFormSubmissions(formId: string | undefined): RecordListResult<FormSubmission> {
201
+ export function useFormSubmissions(
202
+ formId: string | undefined,
203
+ status: FormSubmissionStatus = 'accepted',
204
+ ): RecordListResult<FormSubmission> {
198
205
  return usePagedList<FormSubmission>({
199
- queryKey: ['forms', 'submissions', formId ?? ''],
206
+ queryKey: ['forms', 'submissions', formId ?? '', 'status', status],
200
207
  path: `/forms/${formId ?? ''}/submissions`,
201
208
  decode: formSubmissionSchema.parse,
202
- query: {},
209
+ query: { status },
203
210
  enabled: formId !== undefined,
204
211
  })
205
212
  }
206
213
 
214
+ export interface ReleaseSubmissionArguments {
215
+ readonly formId: string
216
+ readonly submissionId: string
217
+ }
218
+
219
+ /** The CRM lists a released submission can add a record to or change one in. */
220
+ const RELEASE_TOUCHES = [
221
+ 'people',
222
+ 'companies',
223
+ 'positions',
224
+ 'deals',
225
+ 'opportunities',
226
+ 'partnerships',
227
+ 'enquiries',
228
+ ] as const
229
+
230
+ /**
231
+ * Releases a submission the spam check held.
232
+ *
233
+ * Not optimistic: the response is the submission with the records the submit
234
+ * rules made, and none of that is known before the server runs them. A release
235
+ * is a whole submit, so the CRM lists it can write to go stale with it.
236
+ */
237
+ export function useReleaseFormSubmission(): MutationResult<
238
+ ReleaseSubmissionArguments,
239
+ FormSubmission
240
+ > {
241
+ const client = useApiClient()
242
+ const queryClient = useQueryClient()
243
+ const mutation = useMutation({
244
+ mutationFn: ({ formId, submissionId }: ReleaseSubmissionArguments) =>
245
+ client.post(
246
+ `/forms/${formId}/submissions/${submissionId}/release`,
247
+ {},
248
+ formSubmissionSchema.parse,
249
+ ),
250
+ onSuccess: (submission) => {
251
+ void queryClient.invalidateQueries({
252
+ queryKey: ['forms', 'submissions', submission.formId],
253
+ })
254
+ for (const name of RELEASE_TOUCHES) {
255
+ void queryClient.invalidateQueries({ queryKey: [name] })
256
+ }
257
+ },
258
+ })
259
+
260
+ return {
261
+ run: (input) => {
262
+ mutation.mutate(input)
263
+ },
264
+ runAsync: (input) => mutation.mutateAsync(input),
265
+ isPending: mutation.isPending,
266
+ error: toError(mutation.error),
267
+ }
268
+ }
269
+
207
270
  /**
208
271
  * Submissions that touched one record, newest first.
209
272
  *
@@ -263,6 +326,10 @@ export interface EmbedSnippets {
263
326
  readonly embedUrl: string
264
327
  readonly iframeSnippet: string
265
328
  readonly scriptSnippet: string
329
+ /** Where a site's own form posts JSON answers. Built from the slug, so it moves when the slug does. */
330
+ readonly submitUrl: string
331
+ /** Where a site's own form gets a spam-check token, when the form requires the check. */
332
+ readonly tokenUrl: string
266
333
  }
267
334
 
268
335
  function decodeSnippets(value: unknown): EmbedSnippets {
@@ -271,7 +338,9 @@ function decodeSnippets(value: unknown): EmbedSnippets {
271
338
  typeof value.url !== 'string' ||
272
339
  typeof value.embed_url !== 'string' ||
273
340
  typeof value.iframe_snippet !== 'string' ||
274
- typeof value.script_snippet !== 'string'
341
+ typeof value.script_snippet !== 'string' ||
342
+ typeof value.submit_url !== 'string' ||
343
+ typeof value.token_url !== 'string'
275
344
  ) {
276
345
  throw new TypeError('Expected an embed snippet response')
277
346
  }
@@ -281,6 +350,8 @@ function decodeSnippets(value: unknown): EmbedSnippets {
281
350
  embedUrl: value.embed_url,
282
351
  iframeSnippet: value.iframe_snippet,
283
352
  scriptSnippet: value.script_snippet,
353
+ submitUrl: value.submit_url,
354
+ tokenUrl: value.token_url,
284
355
  }
285
356
  }
286
357
 
@@ -30,6 +30,7 @@ export function FormDetail(): React.JSX.Element {
30
30
  const [tab, setTab] = useState<FormTab>('submissions')
31
31
  const { record, isLoading, isNotFound, error } = useForm(id)
32
32
  const submissions = useFormSubmissions(id)
33
+ const spam = useFormSubmissions(id, 'spam')
33
34
  const deleteForm = useDeleteForm()
34
35
 
35
36
  if (isNotFound) {
@@ -85,7 +86,9 @@ export function FormDetail(): React.JSX.Element {
85
86
  onChange={setTab}
86
87
  ariaLabel="Form sections"
87
88
  >
88
- {tab === 'submissions' && <SubmissionsTable form={record} submissions={submissions} />}
89
+ {tab === 'submissions' && (
90
+ <SubmissionsTable form={record} submissions={submissions} spam={spam} />
91
+ )}
89
92
  {tab === 'fields' && <FieldsEditor form={record} />}
90
93
  {tab === 'actions' && <FormActions form={record} />}
91
94
  {tab === 'settings' && <FormSettings form={record} />}
@@ -59,10 +59,14 @@ export function FormsPage(): React.JSX.Element {
59
59
  * A new form starts as the contact template rather than empty, because an
60
60
  * empty field list is not a form the API will accept: it needs a
61
61
  * `person.email` mapping before it can process anything.
62
+ *
63
+ * It starts with the spam check on. The API's own default is off, for a
64
+ * caller that posts JSON; a form made here is used through its embed, which
65
+ * passes the check by itself.
62
66
  */
63
67
  function addForm(): void {
64
68
  createForm
65
- .runAsync({ name: 'New form', fields: CONTACT_FORM_FIELDS })
69
+ .runAsync({ name: 'New form', fields: CONTACT_FORM_FIELDS, requireSpamCheck: true })
66
70
  .then((form) => navigate(`/forms/${form.id}`))
67
71
  .catch(() => undefined)
68
72
  }