@kelpie/ui 0.20.0 → 0.22.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 (68) hide show
  1. package/dist/api/resources/forms.d.ts +21 -7
  2. package/dist/api/resources/forms.d.ts.map +1 -1
  3. package/dist/api/resources/forms.js +39 -1
  4. package/dist/api/resources/forms.js.map +1 -1
  5. package/dist/api/resources/tags.d.ts +20 -0
  6. package/dist/api/resources/tags.d.ts.map +1 -0
  7. package/dist/api/resources/tags.js +52 -0
  8. package/dist/api/resources/tags.js.map +1 -0
  9. package/dist/components/Chip.d.ts +5 -1
  10. package/dist/components/Chip.d.ts.map +1 -1
  11. package/dist/components/Chip.js +8 -2
  12. package/dist/components/Chip.js.map +1 -1
  13. package/dist/components/TagInput.d.ts +29 -0
  14. package/dist/components/TagInput.d.ts.map +1 -0
  15. package/dist/components/TagInput.js +139 -0
  16. package/dist/components/TagInput.js.map +1 -0
  17. package/dist/pages/CompanyDetail.d.ts.map +1 -1
  18. package/dist/pages/CompanyDetail.js +4 -3
  19. package/dist/pages/CompanyDetail.js.map +1 -1
  20. package/dist/pages/DealDetail.d.ts.map +1 -1
  21. package/dist/pages/DealDetail.js +4 -3
  22. package/dist/pages/DealDetail.js.map +1 -1
  23. package/dist/pages/EnquiryDetail.js +4 -4
  24. package/dist/pages/EnquiryDetail.js.map +1 -1
  25. package/dist/pages/EventDetail.js +4 -4
  26. package/dist/pages/EventDetail.js.map +1 -1
  27. package/dist/pages/OpportunityDetail.js +4 -4
  28. package/dist/pages/OpportunityDetail.js.map +1 -1
  29. package/dist/pages/PartnershipDetail.js +4 -4
  30. package/dist/pages/PartnershipDetail.js.map +1 -1
  31. package/dist/pages/PersonDetail.js +5 -5
  32. package/dist/pages/PersonDetail.js.map +1 -1
  33. package/dist/pages/RaiseDetail.js +4 -4
  34. package/dist/pages/RaiseDetail.js.map +1 -1
  35. package/dist/pages/forms/FieldsEditor.d.ts.map +1 -1
  36. package/dist/pages/forms/FieldsEditor.js +6 -7
  37. package/dist/pages/forms/FieldsEditor.js.map +1 -1
  38. package/dist/pages/forms/FormActions.d.ts.map +1 -1
  39. package/dist/pages/forms/FormActions.js +4 -11
  40. package/dist/pages/forms/FormActions.js.map +1 -1
  41. package/dist/pages/forms/SubmissionDetail.d.ts.map +1 -1
  42. package/dist/pages/forms/SubmissionDetail.js +11 -3
  43. package/dist/pages/forms/SubmissionDetail.js.map +1 -1
  44. package/dist/pages/forms/SubmissionsTable.d.ts.map +1 -1
  45. package/dist/pages/forms/SubmissionsTable.js +76 -4
  46. package/dist/pages/forms/SubmissionsTable.js.map +1 -1
  47. package/dist/pages/forms/fieldList.d.ts +13 -5
  48. package/dist/pages/forms/fieldList.d.ts.map +1 -1
  49. package/dist/pages/forms/fieldList.js +12 -4
  50. package/dist/pages/forms/fieldList.js.map +1 -1
  51. package/package.json +2 -2
  52. package/src/api/resources/forms.ts +68 -7
  53. package/src/api/resources/tags.ts +71 -0
  54. package/src/components/Chip.tsx +32 -1
  55. package/src/components/TagInput.tsx +285 -0
  56. package/src/pages/CompanyDetail.tsx +6 -17
  57. package/src/pages/DealDetail.tsx +6 -17
  58. package/src/pages/EnquiryDetail.tsx +6 -18
  59. package/src/pages/EventDetail.tsx +6 -18
  60. package/src/pages/OpportunityDetail.tsx +6 -18
  61. package/src/pages/PartnershipDetail.tsx +6 -18
  62. package/src/pages/PersonDetail.tsx +7 -19
  63. package/src/pages/RaiseDetail.tsx +6 -18
  64. package/src/pages/forms/FieldsEditor.tsx +6 -7
  65. package/src/pages/forms/FormActions.tsx +18 -20
  66. package/src/pages/forms/SubmissionDetail.tsx +24 -5
  67. package/src/pages/forms/SubmissionsTable.tsx +171 -4
  68. package/src/pages/forms/fieldList.ts +21 -7
@@ -1,5 +1,11 @@
1
1
  import { FORM_ATTACH_TARGET_TYPES, PLAN_ITEM_TARGET_TYPE_LABELS } from '@kelpie/schemas'
2
- import type { Form, FormAttachTarget, FormAttachTargetType, PipelineKind } from '@kelpie/schemas'
2
+ import type {
3
+ Form,
4
+ FormAttachTarget,
5
+ FormAttachTargetType,
6
+ PipelineKind,
7
+ TagTargetType,
8
+ } from '@kelpie/schemas'
3
9
  import { useMemo, useState } from 'react'
4
10
 
5
11
  import { useDeals } from '../../api/resources/deals.ts'
@@ -12,10 +18,9 @@ import { useOpportunities } from '../../api/resources/opportunities.ts'
12
18
  import { usePartnerships } from '../../api/resources/partnerships.ts'
13
19
  import { usePipelineStages } from '../../api/resources/pipelineStages.ts'
14
20
  import { useRaises } from '../../api/resources/raises.ts'
15
- import { Chip } from '../../components/Chip.tsx'
16
21
  import { ErrorPanel } from '../../components/QueryState.tsx'
17
22
  import { SectionHeader } from '../../components/SectionHeader.tsx'
18
- import { toTags } from '../fields.ts'
23
+ import { TagInput } from '../../components/TagInput.tsx'
19
24
  import { FormEmails } from './FormEmails.tsx'
20
25
 
21
26
  /**
@@ -119,12 +124,14 @@ export function FormActions({ form }: FormActionsProps): React.JSX.Element {
119
124
  <TagsBlock
120
125
  title="Tag the person"
121
126
  hint="Merged into the submitter's tags. Never removes a tag someone set by hand."
127
+ targetType="person"
122
128
  value={form.personTags}
123
129
  onChange={(next) => patch({ personTags: next })}
124
130
  />
125
131
  <TagsBlock
126
132
  title="Tag the company"
127
133
  hint="Merged into the resolved company's tags. Skipped when no company is resolved."
134
+ targetType="company"
128
135
  value={form.companyTags}
129
136
  onChange={(next) => patch({ companyTags: next })}
130
137
  />
@@ -278,35 +285,26 @@ function TriggerBlock(props: TriggerBlockProps): React.JSX.Element {
278
285
  function TagsBlock({
279
286
  title,
280
287
  hint,
288
+ targetType,
281
289
  value,
282
290
  onChange,
283
291
  }: {
284
292
  readonly title: string
285
293
  readonly hint: string
294
+ readonly targetType: TagTargetType
286
295
  readonly value: readonly string[]
287
296
  readonly onChange: (next: readonly string[]) => void
288
297
  }): React.JSX.Element {
289
298
  return (
290
299
  <div className="space-y-2 rounded-md border border-border p-4">
291
300
  <div className="text-[13px] font-medium text-ink">{title}</div>
292
- <input
293
- className={inputClass}
294
- defaultValue={value.join(', ')}
295
- onBlur={(event) => {
296
- const next = toTags(event.target.value)
297
- const changed =
298
- next.length !== value.length || next.some((tag, index) => tag !== value[index])
299
-
300
- if (changed) {
301
- onChange(next)
302
- }
303
- }}
301
+ <TagInput
302
+ value={value}
303
+ onChange={onChange}
304
+ targetType={targetType}
305
+ size="md"
306
+ label={title}
304
307
  />
305
- <div className="flex flex-wrap gap-1">
306
- {value.map((tag) => (
307
- <Chip key={tag}>{tag}</Chip>
308
- ))}
309
- </div>
310
308
  <p className="text-[11px] text-ink-faint">{hint}</p>
311
309
  </div>
312
310
  )
@@ -1,16 +1,18 @@
1
1
  import { FORM_SPAM_REASON_LABELS, formFieldDisplayLabel } from '@kelpie/schemas'
2
2
  import type { Form, FormField, FormSubmission, FormSubmissionActionEntry } from '@kelpie/schemas'
3
3
  import { useMemo } from 'react'
4
- import { Link, useParams } from 'react-router'
4
+ import { Link, useNavigate, useParams } from 'react-router'
5
5
 
6
6
  import { useTimezone } from '../../api/resources/account.ts'
7
7
  import { useCompanies } from '../../api/resources/companies.ts'
8
8
  import {
9
+ useDeleteFormSubmissions,
9
10
  useForm,
10
11
  useFormSubmission,
11
12
  useReleaseFormSubmission,
12
13
  } from '../../api/resources/forms.ts'
13
14
  import { usePeople } from '../../api/resources/people.ts'
15
+ import { DeleteRecord } from '../../components/DeleteRecord.tsx'
14
16
  import { ErrorPanel, LoadingPanel, NotFoundPanel } from '../../components/QueryState.tsx'
15
17
  import { SectionHeader } from '../../components/SectionHeader.tsx'
16
18
  import { formatDateTime } from '../../lib/dates.ts'
@@ -90,6 +92,8 @@ function SubmissionDetailView({
90
92
  field,
91
93
  value: displayAnswer(field, submission.answers[field.id]),
92
94
  }))
95
+ const remove = useDeleteFormSubmissions()
96
+ const navigate = useNavigate()
93
97
 
94
98
  return (
95
99
  <div className="animate-fade-in mx-auto max-w-2xl">
@@ -100,10 +104,25 @@ function SubmissionDetailView({
100
104
  ← {form.name}
101
105
  </Link>
102
106
 
103
- <SectionHeader
104
- title="Submission"
105
- description={`Received ${formatDateTime(submission.submittedAt, timezone)}.`}
106
- />
107
+ {/* The records this submission linked are not deleted with it. */}
108
+ <div className="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
109
+ <SectionHeader
110
+ title="Submission"
111
+ description={`Received ${formatDateTime(submission.submittedAt, timezone)}.`}
112
+ />
113
+ <DeleteRecord
114
+ recordLabel="Submission"
115
+ recordName="this submission"
116
+ isPending={remove.isPending}
117
+ error={remove.error}
118
+ onConfirm={() => {
119
+ remove
120
+ .runAsync({ formId: form.id, submissionIds: [submission.id] })
121
+ .then(() => navigate(`/forms/${form.id}`))
122
+ .catch(() => undefined)
123
+ }}
124
+ />
125
+ </div>
107
126
 
108
127
  <SpamNotice form={form} submission={submission} />
109
128
 
@@ -11,7 +11,10 @@ import { Link, useNavigate } from 'react-router'
11
11
  import type { RecordListResult } from '../../api/resource.ts'
12
12
  import { useTimezone } from '../../api/resources/account.ts'
13
13
  import { useCompanies } from '../../api/resources/companies.ts'
14
- import { useReleaseFormSubmission } from '../../api/resources/forms.ts'
14
+ import {
15
+ useDeleteFormSubmissions,
16
+ useReleaseFormSubmission,
17
+ } from '../../api/resources/forms.ts'
15
18
  import { usePeople } from '../../api/resources/people.ts'
16
19
  import { DataTable } from '../../components/DataTable.tsx'
17
20
  import type { Column } from '../../components/DataTable.tsx'
@@ -36,6 +39,11 @@ import { formatDateTime } from '../../lib/dates.ts'
36
39
  * What the spam check held is a second list, behind the Spam button. Those rows
37
40
  * link no records, so they show the reason and a Release button in place of the
38
41
  * person and company.
42
+ *
43
+ * Each row has a checkbox, and the ticked rows can be deleted together. A
44
+ * selection is of the rows on screen only: it is cleared when the list, the
45
+ * page or the page size changes, so a delete never takes a row the reader can
46
+ * no longer see. The records a submission linked are not deleted.
39
47
  */
40
48
 
41
49
  /** `?limit=` maxes out at 200 (`docs/agents/api-and-webhooks.md`). */
@@ -54,7 +62,9 @@ export function SubmissionsTable({
54
62
  spam,
55
63
  }: SubmissionsTableProps): React.JSX.Element {
56
64
  const [view, setView] = useState<FormSubmissionStatus>('accepted')
65
+ const [selected, setSelected] = useState<ReadonlySet<string>>(() => new Set())
57
66
  const release = useReleaseFormSubmission()
67
+ const remove = useDeleteFormSubmissions()
58
68
  const navigate = useNavigate()
59
69
  const people = usePeople({ limit: MAX_PAGE })
60
70
  const companies = useCompanies({ limit: MAX_PAGE })
@@ -65,7 +75,56 @@ export function SubmissionsTable({
65
75
  [people.records, companies.records],
66
76
  )
67
77
 
78
+ const list = view === 'spam' ? spam : submissions
79
+ const pageIds = list.records.map((submission) => submission.id)
80
+ const pageKey = pageIds.join(',')
81
+ const [selectionPage, setSelectionPage] = useState(pageKey)
82
+
83
+ // A new page, a new list, or a refetch after a delete: the ticks belonged to
84
+ // rows that are no longer the ones on screen.
85
+ if (selectionPage !== pageKey) {
86
+ setSelectionPage(pageKey)
87
+ setSelected(new Set())
88
+ }
89
+
90
+ const selectedOnPage = pageIds.filter((id) => selected.has(id))
91
+
92
+ function toggle(id: string): void {
93
+ setSelected((current) => {
94
+ const next = new Set(current)
95
+
96
+ if (next.has(id)) {
97
+ next.delete(id)
98
+ } else {
99
+ next.add(id)
100
+ }
101
+
102
+ return next
103
+ })
104
+ }
105
+
106
+ const selectColumn: Column<FormSubmission> = {
107
+ key: 'select',
108
+ header: '',
109
+ className: 'w-8',
110
+ render: (submission) => (
111
+ <input
112
+ type="checkbox"
113
+ aria-label="Select submission"
114
+ checked={selected.has(submission.id)}
115
+ onClick={(event) => {
116
+ event.stopPropagation()
117
+ }}
118
+ onChange={() => {
119
+ toggle(submission.id)
120
+ }}
121
+ className="h-3.5 w-3.5 rounded border-border text-accent focus:ring-accent"
122
+ />
123
+ ),
124
+ }
125
+
68
126
  const columns: readonly Column<FormSubmission>[] = [
127
+ selectColumn,
69
128
  {
70
129
  key: 'submitted',
71
130
  header: 'Submitted',
@@ -129,7 +188,7 @@ export function SubmissionsTable({
129
188
  ]
130
189
 
131
190
  const spamColumns: readonly Column<FormSubmission>[] = [
132
- ...columns.filter((column) => column.key === 'submitted'),
191
+ ...columns.filter((column) => column.key === 'select' || column.key === 'submitted'),
133
192
  {
134
193
  key: 'reason',
135
194
  header: 'Why it was held',
@@ -168,8 +227,6 @@ export function SubmissionsTable({
168
227
  },
169
228
  ]
170
229
 
171
- const list = view === 'spam' ? spam : submissions
172
-
173
230
  if (list.error !== null) {
174
231
  return <ErrorPanel error={list.error} />
175
232
  }
@@ -201,6 +258,20 @@ export function SubmissionsTable({
201
258
  <ErrorPanel error={release.error} />
202
259
  </div>
203
260
  )}
261
+ {list.records.length > 0 && (
262
+ <SelectionBar
263
+ pageCount={pageIds.length}
264
+ selectedCount={selectedOnPage.length}
265
+ isPending={remove.isPending}
266
+ error={remove.error}
267
+ onSelectAll={(all) => {
268
+ setSelected(all ? new Set(pageIds) : new Set())
269
+ }}
270
+ onDelete={() => {
271
+ remove.run({ formId: form.id, submissionIds: selectedOnPage })
272
+ }}
273
+ />
274
+ )}
204
275
  <Paginator list={list} placement="top" />
205
276
  <DataTable
206
277
  columns={view === 'spam' ? spamColumns : columns}
@@ -216,6 +287,102 @@ export function SubmissionsTable({
216
287
  )
217
288
  }
218
289
 
290
+ /**
291
+ * "Select all on this page", and, while rows are ticked, a Delete behind one
292
+ * confirmation. The confirmation says that the linked records stay, because
293
+ * that is the question a reader has before deleting what created them.
294
+ */
295
+ function SelectionBar({
296
+ pageCount,
297
+ selectedCount,
298
+ isPending,
299
+ error,
300
+ onSelectAll,
301
+ onDelete,
302
+ }: {
303
+ readonly pageCount: number
304
+ readonly selectedCount: number
305
+ readonly isPending: boolean
306
+ readonly error: Error | null
307
+ readonly onSelectAll: (all: boolean) => void
308
+ readonly onDelete: () => void
309
+ }): React.JSX.Element {
310
+ const [confirming, setConfirming] = useState(false)
311
+ const allSelected = selectedCount === pageCount
312
+ const noun = selectedCount === 1 ? 'submission' : 'submissions'
313
+
314
+ if (confirming && selectedCount === 0) {
315
+ setConfirming(false)
316
+ }
317
+
318
+ return (
319
+ <div className="mb-3">
320
+ <div className="flex min-h-7 flex-wrap items-center gap-x-3 gap-y-2">
321
+ <label className="inline-flex items-center gap-2 text-[12px] text-ink-muted">
322
+ <input
323
+ type="checkbox"
324
+ checked={allSelected}
325
+ ref={(input) => {
326
+ if (input !== null) {
327
+ input.indeterminate = selectedCount > 0 && !allSelected
328
+ }
329
+ }}
330
+ onChange={() => {
331
+ onSelectAll(!allSelected)
332
+ }}
333
+ className="h-3.5 w-3.5 rounded border-border text-accent focus:ring-accent"
334
+ />
335
+ {selectedCount > 0 ? `${String(selectedCount)} selected` : 'Select all on this page'}
336
+ </label>
337
+ {selectedCount > 0 &&
338
+ (confirming ? (
339
+ <div className="flex flex-wrap items-center gap-2">
340
+ <span className="text-[12px] text-ink-muted">
341
+ Delete {selectedCount} {noun}? The people and records they created stay.
342
+ </span>
343
+ <button
344
+ type="button"
345
+ onClick={() => {
346
+ setConfirming(false)
347
+ }}
348
+ className="rounded-md px-2 py-1 text-[12px] font-medium text-ink-muted hover:text-ink"
349
+ >
350
+ Cancel
351
+ </button>
352
+ <button
353
+ type="button"
354
+ disabled={isPending}
355
+ onClick={() => {
356
+ setConfirming(false)
357
+ onDelete()
358
+ }}
359
+ className="rounded-md bg-danger px-2.5 py-1 text-[12px] font-semibold text-danger-fg transition hover:opacity-90 disabled:opacity-50"
360
+ >
361
+ {isPending ? 'Deleting…' : `Delete ${noun}`}
362
+ </button>
363
+ </div>
364
+ ) : (
365
+ <button
366
+ type="button"
367
+ disabled={isPending}
368
+ onClick={() => {
369
+ setConfirming(true)
370
+ }}
371
+ className="rounded-md border border-border px-2.5 py-1 text-[12px] font-medium text-ink-muted transition hover:border-danger hover:text-danger disabled:opacity-50"
372
+ >
373
+ {isPending ? 'Deleting…' : 'Delete'}
374
+ </button>
375
+ ))}
376
+ </div>
377
+ {error !== null && (
378
+ <div className="mt-2">
379
+ <ErrorPanel error={error} />
380
+ </div>
381
+ )}
382
+ </div>
383
+ )
384
+ }
385
+
219
386
  function ViewButton({
220
387
  active,
221
388
  onClick,
@@ -21,7 +21,7 @@ import { CRM_FIELD_PRESETS, NEW_SELECT_OPTIONS } from './template.ts'
21
21
  /**
22
22
  * Editing a form's field list, as pure functions over an array.
23
23
  *
24
- * A write replaces the whole list, so the builder holds one and hands it back;
24
+ * A write carries the whole list, so the builder holds one and hands it back;
25
25
  * these are the transformations it applies. No React and no fetching here, which
26
26
  * is what makes the rules that keep a list valid testable on their own.
27
27
  *
@@ -30,8 +30,11 @@ import { CRM_FIELD_PRESETS, NEW_SELECT_OPTIONS } from './template.ts'
30
30
  * keep the builder from constructing a list it already knows will be refused.
31
31
  */
32
32
 
33
+ /** What the builder edits on a field: everything a write carries except the id. */
34
+ export type FieldSettings = Omit<FormFieldInput, 'id'>
35
+
33
36
  /** A field being edited, with the identity the list uses to address it. */
34
- export interface EditableField extends FormFieldInput {
37
+ export interface EditableField extends FieldSettings {
35
38
  /** The stored field id, or a local one for a field not yet saved. */
36
39
  readonly id: string
37
40
  }
@@ -66,9 +69,17 @@ export function toEditableFields(form: Form): EditableField[] {
66
69
  }))
67
70
  }
68
71
 
69
- /** Strips the local ids back off. Ids are assigned server-side on every write. */
70
- export function toFieldInputs(fields: readonly EditableField[]): FormFieldInput[] {
71
- return fields.map(({ id: _id, ...field }) => field)
72
+ /**
73
+ * The list as a write sends it.
74
+ *
75
+ * A saved field goes with its stored id, which is what lets it keep that id
76
+ * through the edit. A field added since the last save has only a local id, and
77
+ * goes without one: the server gives it its id.
78
+ */
79
+ export function toFieldInputs(form: Form, fields: readonly EditableField[]): FormFieldInput[] {
80
+ const storedIds = new Set(form.fields.map((field) => field.id))
81
+
82
+ return fields.map(({ id, ...field }) => (storedIds.has(id) ? { id, ...field } : field))
72
83
  }
73
84
 
74
85
  /**
@@ -79,7 +90,10 @@ export function toFieldInputs(fields: readonly EditableField[]): FormFieldInput[
79
90
  * renders.
80
91
  */
81
92
  export function fieldsChanged(form: Form, fields: readonly EditableField[]): boolean {
82
- return JSON.stringify(toFieldInputs(toEditableFields(form))) !== JSON.stringify(toFieldInputs(fields))
93
+ return (
94
+ JSON.stringify(toFieldInputs(form, toEditableFields(form))) !==
95
+ JSON.stringify(toFieldInputs(form, fields))
96
+ )
83
97
  }
84
98
 
85
99
  function withoutOptions(field: EditableField): EditableField {
@@ -100,7 +114,7 @@ function withoutOptions(field: EditableField): EditableField {
100
114
  export function editField(
101
115
  fields: readonly EditableField[],
102
116
  id: string,
103
- change: Partial<FormFieldInput>,
117
+ change: Partial<FieldSettings>,
104
118
  ): EditableField[] {
105
119
  return fields.map((field) => {
106
120
  if (field.id !== id) {