@kelpie/ui 0.27.0 → 0.28.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.
- package/dist/api/resources/bulkActions.d.ts +50 -0
- package/dist/api/resources/bulkActions.d.ts.map +1 -0
- package/dist/api/resources/bulkActions.js +192 -0
- package/dist/api/resources/bulkActions.js.map +1 -0
- package/dist/api/resources/forms.d.ts +26 -1
- package/dist/api/resources/forms.d.ts.map +1 -1
- package/dist/api/resources/forms.js +98 -1
- package/dist/api/resources/forms.js.map +1 -1
- package/dist/app/KelpieApp.d.ts.map +1 -1
- package/dist/app/KelpieApp.js +3 -1
- package/dist/app/KelpieApp.js.map +1 -1
- package/dist/components/BulkActions.d.ts +61 -0
- package/dist/components/BulkActions.d.ts.map +1 -0
- package/dist/components/BulkActions.js +351 -0
- package/dist/components/BulkActions.js.map +1 -0
- package/dist/components/BulkParamFields.d.ts +13 -0
- package/dist/components/BulkParamFields.d.ts.map +1 -0
- package/dist/components/BulkParamFields.js +177 -0
- package/dist/components/BulkParamFields.js.map +1 -0
- package/dist/components/CellLinks.d.ts +38 -0
- package/dist/components/CellLinks.d.ts.map +1 -0
- package/dist/components/CellLinks.js +58 -0
- package/dist/components/CellLinks.js.map +1 -0
- package/dist/components/CustomFieldsPanel.d.ts +12 -1
- package/dist/components/CustomFieldsPanel.d.ts.map +1 -1
- package/dist/components/CustomFieldsPanel.js +5 -1
- package/dist/components/CustomFieldsPanel.js.map +1 -1
- package/dist/components/DataTable.d.ts +42 -1
- package/dist/components/DataTable.d.ts.map +1 -1
- package/dist/components/DataTable.js +51 -16
- package/dist/components/DataTable.js.map +1 -1
- package/dist/components/PhonesField.d.ts.map +1 -1
- package/dist/components/PhonesField.js +1 -3
- package/dist/components/PhonesField.js.map +1 -1
- package/dist/components/Shell.d.ts.map +1 -1
- package/dist/components/Shell.js +16 -6
- package/dist/components/Shell.js.map +1 -1
- package/dist/components/TagInput.d.ts.map +1 -1
- package/dist/components/TagInput.js +5 -0
- package/dist/components/TagInput.js.map +1 -1
- package/dist/components/bulkActionParams.d.ts +71 -0
- package/dist/components/bulkActionParams.d.ts.map +1 -0
- package/dist/components/bulkActionParams.js +241 -0
- package/dist/components/bulkActionParams.js.map +1 -0
- package/dist/components/bulkActionSummary.d.ts +22 -0
- package/dist/components/bulkActionSummary.d.ts.map +1 -0
- package/dist/components/bulkActionSummary.js +68 -0
- package/dist/components/bulkActionSummary.js.map +1 -0
- package/dist/components/bulkJobStatus.d.ts +4 -0
- package/dist/components/bulkJobStatus.d.ts.map +1 -0
- package/dist/components/bulkJobStatus.js +10 -0
- package/dist/components/bulkJobStatus.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/bulkSelection.d.ts +43 -0
- package/dist/lib/bulkSelection.d.ts.map +1 -0
- package/dist/lib/bulkSelection.js +70 -0
- package/dist/lib/bulkSelection.js.map +1 -0
- package/dist/lib/contactLinks.d.ts +15 -0
- package/dist/lib/contactLinks.d.ts.map +1 -0
- package/dist/lib/contactLinks.js +22 -0
- package/dist/lib/contactLinks.js.map +1 -0
- package/dist/pages/BulkJobsPage.d.ts +3 -0
- package/dist/pages/BulkJobsPage.d.ts.map +1 -0
- package/dist/pages/BulkJobsPage.js +92 -0
- package/dist/pages/BulkJobsPage.js.map +1 -0
- package/dist/pages/CompaniesPage.d.ts.map +1 -1
- package/dist/pages/CompaniesPage.js +8 -4
- package/dist/pages/CompaniesPage.js.map +1 -1
- package/dist/pages/DealsPage.d.ts.map +1 -1
- package/dist/pages/DealsPage.js +15 -4
- package/dist/pages/DealsPage.js.map +1 -1
- package/dist/pages/DecisionsPage.d.ts.map +1 -1
- package/dist/pages/DecisionsPage.js +11 -1
- package/dist/pages/DecisionsPage.js.map +1 -1
- package/dist/pages/EnquiriesPage.d.ts.map +1 -1
- package/dist/pages/EnquiriesPage.js +15 -6
- package/dist/pages/EnquiriesPage.js.map +1 -1
- package/dist/pages/EventDetail.d.ts.map +1 -1
- package/dist/pages/EventDetail.js +14 -6
- package/dist/pages/EventDetail.js.map +1 -1
- package/dist/pages/EventsPage.d.ts.map +1 -1
- package/dist/pages/EventsPage.js +13 -3
- package/dist/pages/EventsPage.js.map +1 -1
- package/dist/pages/FormsPage.d.ts.map +1 -1
- package/dist/pages/FormsPage.js +2 -2
- package/dist/pages/FormsPage.js.map +1 -1
- package/dist/pages/FundraisingPage.d.ts.map +1 -1
- package/dist/pages/FundraisingPage.js +15 -4
- package/dist/pages/FundraisingPage.js.map +1 -1
- package/dist/pages/HiringPage.d.ts.map +1 -1
- package/dist/pages/HiringPage.js +2 -1
- package/dist/pages/HiringPage.js.map +1 -1
- package/dist/pages/ListDetail.d.ts.map +1 -1
- package/dist/pages/ListDetail.js +39 -7
- package/dist/pages/ListDetail.js.map +1 -1
- package/dist/pages/ListsPage.d.ts.map +1 -1
- package/dist/pages/ListsPage.js +2 -1
- package/dist/pages/ListsPage.js.map +1 -1
- package/dist/pages/OpportunitiesPage.d.ts.map +1 -1
- package/dist/pages/OpportunitiesPage.js +15 -6
- package/dist/pages/OpportunitiesPage.js.map +1 -1
- package/dist/pages/PartnershipsPage.d.ts.map +1 -1
- package/dist/pages/PartnershipsPage.js +15 -4
- package/dist/pages/PartnershipsPage.js.map +1 -1
- package/dist/pages/PeoplePage.d.ts.map +1 -1
- package/dist/pages/PeoplePage.js +10 -9
- package/dist/pages/PeoplePage.js.map +1 -1
- package/dist/pages/PlanningPage.d.ts.map +1 -1
- package/dist/pages/PlanningPage.js +15 -4
- package/dist/pages/PlanningPage.js.map +1 -1
- package/dist/pages/RoleDetail.d.ts.map +1 -1
- package/dist/pages/RoleDetail.js +12 -4
- package/dist/pages/RoleDetail.js.map +1 -1
- package/dist/pages/forms/BlockedSendersPage.d.ts +10 -0
- package/dist/pages/forms/BlockedSendersPage.d.ts.map +1 -0
- package/dist/pages/forms/BlockedSendersPage.js +65 -0
- package/dist/pages/forms/BlockedSendersPage.js.map +1 -0
- package/dist/pages/forms/MarkSpamPanel.d.ts +19 -0
- package/dist/pages/forms/MarkSpamPanel.d.ts.map +1 -0
- package/dist/pages/forms/MarkSpamPanel.js +62 -0
- package/dist/pages/forms/MarkSpamPanel.js.map +1 -0
- package/dist/pages/forms/SubmissionDetail.d.ts.map +1 -1
- package/dist/pages/forms/SubmissionDetail.js +41 -11
- package/dist/pages/forms/SubmissionDetail.js.map +1 -1
- package/dist/pages/forms/SubmissionsTable.d.ts +1 -1
- package/dist/pages/forms/SubmissionsTable.d.ts.map +1 -1
- package/dist/pages/forms/SubmissionsTable.js +6 -4
- package/dist/pages/forms/SubmissionsTable.js.map +1 -1
- package/dist/pages/forms/markSpamChoices.d.ts +51 -0
- package/dist/pages/forms/markSpamChoices.d.ts.map +1 -0
- package/dist/pages/forms/markSpamChoices.js +96 -0
- package/dist/pages/forms/markSpamChoices.js.map +1 -0
- package/dist/pages/positionDirectory.d.ts +5 -0
- package/dist/pages/positionDirectory.d.ts.map +1 -1
- package/dist/pages/positionDirectory.js +11 -3
- package/dist/pages/positionDirectory.js.map +1 -1
- package/dist/registry/context.d.ts +3 -1
- package/dist/registry/context.d.ts.map +1 -1
- package/dist/registry/context.js +4 -0
- package/dist/registry/context.js.map +1 -1
- package/dist/registry/contributions.d.ts +23 -0
- package/dist/registry/contributions.d.ts.map +1 -1
- package/dist/registry/contributions.js.map +1 -1
- package/dist/registry/registry.d.ts +4 -1
- package/dist/registry/registry.d.ts.map +1 -1
- package/dist/registry/registry.js +6 -0
- package/dist/registry/registry.js.map +1 -1
- package/dist/registry/visibleNav.d.ts.map +1 -1
- package/dist/registry/visibleNav.js +1 -0
- package/dist/registry/visibleNav.js.map +1 -1
- package/package.json +3 -3
- package/src/api/resources/bulkActions.ts +267 -0
- package/src/api/resources/forms.ts +135 -0
- package/src/app/KelpieApp.tsx +5 -0
- package/src/components/BulkActions.tsx +961 -0
- package/src/components/BulkParamFields.tsx +524 -0
- package/src/components/CellLinks.tsx +138 -0
- package/src/components/CustomFieldsPanel.tsx +6 -2
- package/src/components/DataTable.tsx +142 -2
- package/src/components/PhonesField.tsx +2 -4
- package/src/components/Shell.tsx +23 -6
- package/src/components/TagInput.tsx +5 -0
- package/src/components/bulkActionParams.ts +405 -0
- package/src/components/bulkActionSummary.ts +104 -0
- package/src/components/bulkJobStatus.ts +11 -0
- package/src/index.ts +3 -0
- package/src/lib/bulkSelection.ts +128 -0
- package/src/lib/contactLinks.ts +25 -0
- package/src/pages/BulkJobsPage.tsx +206 -0
- package/src/pages/CompaniesPage.tsx +20 -5
- package/src/pages/DealsPage.tsx +30 -2
- package/src/pages/DecisionsPage.tsx +21 -1
- package/src/pages/EnquiriesPage.tsx +33 -5
- package/src/pages/EventDetail.tsx +43 -16
- package/src/pages/EventsPage.tsx +26 -2
- package/src/pages/FormsPage.tsx +7 -1
- package/src/pages/FundraisingPage.tsx +30 -2
- package/src/pages/HiringPage.tsx +6 -1
- package/src/pages/ListDetail.tsx +69 -5
- package/src/pages/ListsPage.tsx +6 -1
- package/src/pages/OpportunitiesPage.tsx +33 -5
- package/src/pages/PartnershipsPage.tsx +30 -2
- package/src/pages/PeoplePage.tsx +26 -21
- package/src/pages/PlanningPage.tsx +65 -24
- package/src/pages/RoleDetail.tsx +35 -7
- package/src/pages/forms/BlockedSendersPage.tsx +155 -0
- package/src/pages/forms/MarkSpamPanel.tsx +193 -0
- package/src/pages/forms/SubmissionDetail.tsx +73 -18
- package/src/pages/forms/SubmissionsTable.tsx +7 -5
- package/src/pages/forms/markSpamChoices.ts +141 -0
- package/src/pages/positionDirectory.ts +16 -4
- package/src/registry/context.ts +6 -0
- package/src/registry/contributions.ts +25 -0
- package/src/registry/registry.ts +12 -0
- package/src/registry/visibleNav.ts +1 -0
|
@@ -0,0 +1,961 @@
|
|
|
1
|
+
import { BULK_ACTION_MAX_RECORDS } from '@kelpie/schemas'
|
|
2
|
+
import type {
|
|
3
|
+
BulkActionCatalogEntry,
|
|
4
|
+
BulkActionInput,
|
|
5
|
+
BulkActionTargetType,
|
|
6
|
+
BulkJob,
|
|
7
|
+
} from '@kelpie/schemas'
|
|
8
|
+
import { useEffect, useRef, useState } from 'react'
|
|
9
|
+
import type { ReactNode } from 'react'
|
|
10
|
+
import { Link } from 'react-router'
|
|
11
|
+
|
|
12
|
+
import { ApiError } from '../api/client.ts'
|
|
13
|
+
import {
|
|
14
|
+
isBulkJobFinal,
|
|
15
|
+
useBulkActionCatalog,
|
|
16
|
+
useBulkActionPreview,
|
|
17
|
+
useBulkJob,
|
|
18
|
+
useBulkSelectionCount,
|
|
19
|
+
useCancelBulkJob,
|
|
20
|
+
useCreateBulkJob,
|
|
21
|
+
useDownloadBulkJob,
|
|
22
|
+
useRefreshAfterBulkJob,
|
|
23
|
+
} from '../api/resources/bulkActions.ts'
|
|
24
|
+
import { saveCsv } from '../api/resources/importJobs.ts'
|
|
25
|
+
import type { BulkSelectionController } from '../lib/bulkSelection.ts'
|
|
26
|
+
import { BulkParamFields } from './BulkParamFields.tsx'
|
|
27
|
+
import { initialParamValues, paramFieldsFor, paramsFromValues } from './bulkActionParams.ts'
|
|
28
|
+
import type { ParamField, ParamValues } from './bulkActionParams.ts'
|
|
29
|
+
import { runSummary } from './bulkActionSummary.ts'
|
|
30
|
+
import { STATUS_TEXT } from './bulkJobStatus.ts'
|
|
31
|
+
import { ErrorPanel } from './QueryState.tsx'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Bulk actions on a list page: the action bar, the "select all that match"
|
|
35
|
+
* banner, the form and confirmation for one action, and the running job.
|
|
36
|
+
*
|
|
37
|
+
* While anything is selected the bar takes the place of the page's filter
|
|
38
|
+
* bar, which the page passes as `children`. Choosing "Run" starts a job and
|
|
39
|
+
* clears the selection: the server fixed the set of records when it accepted
|
|
40
|
+
* the job, so the ticks have done their work.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/** Over this many records, a destructive action also asks for the count to be typed. */
|
|
44
|
+
export const TYPED_CONFIRM_THRESHOLD = 100
|
|
45
|
+
|
|
46
|
+
/** The action that exports a selection, offered before a destructive one. */
|
|
47
|
+
const EXPORT_ACTION = 'core.export'
|
|
48
|
+
|
|
49
|
+
/** An export started before a destructive action, and where the reader is with its file. */
|
|
50
|
+
interface ExportRun {
|
|
51
|
+
readonly jobId: string
|
|
52
|
+
readonly outcome: 'waiting' | 'saved' | 'waiving' | 'waived'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface RecordNoun {
|
|
56
|
+
readonly one: string
|
|
57
|
+
readonly many: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface BulkActionsProps {
|
|
61
|
+
readonly targetType: BulkActionTargetType
|
|
62
|
+
readonly selection: BulkSelectionController
|
|
63
|
+
/** The ids of the rows on screen. */
|
|
64
|
+
readonly pageIds: readonly string[]
|
|
65
|
+
readonly hasNextPage: boolean
|
|
66
|
+
readonly noun: RecordNoun
|
|
67
|
+
/** The page's filter bar, shown while nothing is selected. */
|
|
68
|
+
readonly children?: ReactNode
|
|
69
|
+
/** Params the page fixes for an action, by action key. */
|
|
70
|
+
readonly presets?: Readonly<Record<string, BulkActionPreset>> | undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* An action whose params the page already knows, such as the list a List
|
|
75
|
+
* page shows for "Remove from this list". Those params are not asked for.
|
|
76
|
+
*/
|
|
77
|
+
export interface BulkActionPreset {
|
|
78
|
+
/** Replaces the catalog's label in the menu and on the form. */
|
|
79
|
+
readonly label?: string
|
|
80
|
+
readonly params: Readonly<Record<string, string>>
|
|
81
|
+
/** How the confirm step names each preset value, by param name. */
|
|
82
|
+
readonly choices?: Readonly<Record<string, string>>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface RenderableAction {
|
|
86
|
+
readonly entry: BulkActionCatalogEntry
|
|
87
|
+
/** The fields the form asks for: every param the preset does not fix. */
|
|
88
|
+
readonly fields: readonly ParamField[]
|
|
89
|
+
/** Every param's field, for the confirm sentence. */
|
|
90
|
+
readonly allFields: readonly ParamField[]
|
|
91
|
+
readonly preset: BulkActionPreset | undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The catalog entry as this page offers it, or nothing when its form cannot be drawn. */
|
|
95
|
+
function renderable(
|
|
96
|
+
entry: BulkActionCatalogEntry,
|
|
97
|
+
targetType: BulkActionTargetType,
|
|
98
|
+
preset: BulkActionPreset | undefined,
|
|
99
|
+
): RenderableAction | undefined {
|
|
100
|
+
const allFields = paramFieldsFor(entry, targetType)
|
|
101
|
+
|
|
102
|
+
if (allFields === undefined) {
|
|
103
|
+
return undefined
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (preset === undefined) {
|
|
107
|
+
return { entry, fields: allFields, allFields, preset }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
entry: { ...entry, label: preset.label ?? entry.label },
|
|
112
|
+
fields: allFields.filter((field) => !(field.name in preset.params)),
|
|
113
|
+
allFields,
|
|
114
|
+
preset,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface StartedJob {
|
|
119
|
+
readonly id: string
|
|
120
|
+
readonly label: string
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function countOf(count: number, noun: RecordNoun): string {
|
|
124
|
+
return `${count.toLocaleString()} ${count === 1 ? noun.one : noun.many}`
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function BulkActions({
|
|
128
|
+
targetType,
|
|
129
|
+
selection,
|
|
130
|
+
pageIds,
|
|
131
|
+
hasNextPage,
|
|
132
|
+
noun,
|
|
133
|
+
children,
|
|
134
|
+
presets,
|
|
135
|
+
}: BulkActionsProps): React.JSX.Element {
|
|
136
|
+
const [chosenKey, setChosenKey] = useState<string | undefined>(undefined)
|
|
137
|
+
const [job, setJob] = useState<StartedJob | undefined>(undefined)
|
|
138
|
+
const catalog = useBulkActionCatalog(targetType, selection.hasSelection)
|
|
139
|
+
const actions: readonly RenderableAction[] = catalog.actions.flatMap((entry) => {
|
|
140
|
+
const action = renderable(entry, targetType, presets?.[entry.key])
|
|
141
|
+
|
|
142
|
+
return action === undefined ? [] : [action]
|
|
143
|
+
})
|
|
144
|
+
const chosen = actions.find((action) => action.entry.key === chosenKey)
|
|
145
|
+
const canExport = actions.some((action) => action.entry.key === EXPORT_ACTION)
|
|
146
|
+
|
|
147
|
+
// The API has no list totals, so a filter selection is counted by a
|
|
148
|
+
// preview with no action. A refusal (a key without the type's read scope)
|
|
149
|
+
// leaves the banner without a number rather than showing an error.
|
|
150
|
+
const matching = useBulkSelectionCount(
|
|
151
|
+
selection.state.mode === 'filter' && selection.selection !== undefined
|
|
152
|
+
? { targetType, selection: selection.selection }
|
|
153
|
+
: undefined,
|
|
154
|
+
)
|
|
155
|
+
// Over the cap the preview answers the cap plus one, which is not a count to show.
|
|
156
|
+
const matchingCount = matching.preview?.overLimit === true ? undefined : matching.preview?.count
|
|
157
|
+
const matchingOverLimit = matching.preview?.overLimit === true
|
|
158
|
+
|
|
159
|
+
if (!selection.hasSelection && chosenKey !== undefined) {
|
|
160
|
+
setChosenKey(undefined)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<>
|
|
165
|
+
{job !== undefined && (
|
|
166
|
+
<BulkJobProgress
|
|
167
|
+
jobId={job.id}
|
|
168
|
+
label={job.label}
|
|
169
|
+
targetType={targetType}
|
|
170
|
+
onDismiss={() => {
|
|
171
|
+
setJob(undefined)
|
|
172
|
+
}}
|
|
173
|
+
/>
|
|
174
|
+
)}
|
|
175
|
+
{selection.hasSelection ? (
|
|
176
|
+
<div className="mb-3 space-y-2">
|
|
177
|
+
<ActionBar
|
|
178
|
+
countLabel={
|
|
179
|
+
selection.idCount !== undefined
|
|
180
|
+
? `${countOf(selection.idCount, noun)} selected`
|
|
181
|
+
: matchingCount !== undefined
|
|
182
|
+
? `All ${countOf(matchingCount, noun)} selected`
|
|
183
|
+
: `All matching ${noun.many} selected`
|
|
184
|
+
}
|
|
185
|
+
actions={actions}
|
|
186
|
+
catalogLoading={catalog.isLoading}
|
|
187
|
+
onChoose={setChosenKey}
|
|
188
|
+
onClear={selection.clear}
|
|
189
|
+
/>
|
|
190
|
+
{catalog.error !== null && <BulkError error={catalog.error} />}
|
|
191
|
+
<SelectAllBanner
|
|
192
|
+
selection={selection}
|
|
193
|
+
pageIds={pageIds}
|
|
194
|
+
hasNextPage={hasNextPage}
|
|
195
|
+
matchingCount={matchingCount}
|
|
196
|
+
overLimit={matchingOverLimit}
|
|
197
|
+
noun={noun}
|
|
198
|
+
/>
|
|
199
|
+
{chosen !== undefined && selection.selection !== undefined && (
|
|
200
|
+
<BulkActionPanel
|
|
201
|
+
// A new action starts a new form.
|
|
202
|
+
key={chosen.entry.key}
|
|
203
|
+
targetType={targetType}
|
|
204
|
+
action={chosen}
|
|
205
|
+
selection={selection}
|
|
206
|
+
noun={noun}
|
|
207
|
+
canExport={canExport}
|
|
208
|
+
onCancel={() => {
|
|
209
|
+
setChosenKey(undefined)
|
|
210
|
+
}}
|
|
211
|
+
onStarted={(started) => {
|
|
212
|
+
setJob({ id: started.id, label: chosen.entry.label })
|
|
213
|
+
setChosenKey(undefined)
|
|
214
|
+
selection.clear()
|
|
215
|
+
}}
|
|
216
|
+
/>
|
|
217
|
+
)}
|
|
218
|
+
</div>
|
|
219
|
+
) : (
|
|
220
|
+
children
|
|
221
|
+
)}
|
|
222
|
+
</>
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function ActionBar({
|
|
227
|
+
countLabel,
|
|
228
|
+
actions,
|
|
229
|
+
catalogLoading,
|
|
230
|
+
onChoose,
|
|
231
|
+
onClear,
|
|
232
|
+
}: {
|
|
233
|
+
readonly countLabel: string
|
|
234
|
+
readonly actions: readonly RenderableAction[]
|
|
235
|
+
readonly catalogLoading: boolean
|
|
236
|
+
readonly onChoose: (key: string) => void
|
|
237
|
+
readonly onClear: () => void
|
|
238
|
+
}): React.JSX.Element {
|
|
239
|
+
const [open, setOpen] = useState(false)
|
|
240
|
+
const rootRef = useRef<HTMLDivElement>(null)
|
|
241
|
+
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
242
|
+
const menuRef = useRef<HTMLDivElement>(null)
|
|
243
|
+
|
|
244
|
+
// A menu, with a menu's keys: focus starts on the first item, the arrows,
|
|
245
|
+
// Home and End move it, Escape closes and hands focus back to the button,
|
|
246
|
+
// and Tab closes and moves on.
|
|
247
|
+
function items(): HTMLElement[] {
|
|
248
|
+
return Array.from(menuRef.current?.querySelectorAll<HTMLElement>('[role="menuitem"]') ?? [])
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function moveFocus(step: 'next' | 'previous' | 'first' | 'last'): void {
|
|
252
|
+
const all = items()
|
|
253
|
+
const current = all.findIndex((item) => item === document.activeElement)
|
|
254
|
+
const index =
|
|
255
|
+
step === 'first'
|
|
256
|
+
? 0
|
|
257
|
+
: step === 'last'
|
|
258
|
+
? all.length - 1
|
|
259
|
+
: step === 'next'
|
|
260
|
+
? (current + 1) % all.length
|
|
261
|
+
: (current - 1 + all.length) % all.length
|
|
262
|
+
|
|
263
|
+
all[index]?.focus()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function closeToButton(): void {
|
|
267
|
+
setOpen(false)
|
|
268
|
+
buttonRef.current?.focus()
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
useEffect(() => {
|
|
272
|
+
if (!open) {
|
|
273
|
+
return undefined
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
items()[0]?.focus()
|
|
277
|
+
|
|
278
|
+
function close(event: MouseEvent): void {
|
|
279
|
+
if (rootRef.current !== null && !rootRef.current.contains(event.target as Node)) {
|
|
280
|
+
setOpen(false)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
document.addEventListener('mousedown', close)
|
|
285
|
+
|
|
286
|
+
return () => {
|
|
287
|
+
document.removeEventListener('mousedown', close)
|
|
288
|
+
}
|
|
289
|
+
}, [open])
|
|
290
|
+
|
|
291
|
+
return (
|
|
292
|
+
<div
|
|
293
|
+
role="toolbar"
|
|
294
|
+
aria-label="Bulk actions"
|
|
295
|
+
className="flex min-h-[34px] flex-wrap items-center gap-2 rounded-md border border-accent/40 bg-accent-soft px-3 py-1.5"
|
|
296
|
+
>
|
|
297
|
+
<span className="text-[13px] font-medium text-ink" aria-live="polite">
|
|
298
|
+
{countLabel}
|
|
299
|
+
</span>
|
|
300
|
+
<div ref={rootRef} className="relative">
|
|
301
|
+
<button
|
|
302
|
+
ref={buttonRef}
|
|
303
|
+
type="button"
|
|
304
|
+
aria-haspopup="menu"
|
|
305
|
+
aria-expanded={open}
|
|
306
|
+
disabled={catalogLoading}
|
|
307
|
+
onClick={() => {
|
|
308
|
+
setOpen((current) => !current)
|
|
309
|
+
}}
|
|
310
|
+
onKeyDown={(event) => {
|
|
311
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
312
|
+
event.preventDefault()
|
|
313
|
+
setOpen(true)
|
|
314
|
+
}
|
|
315
|
+
}}
|
|
316
|
+
className="rounded-md border border-border bg-surface-raised px-2.5 py-1 text-[12px] font-medium text-ink transition hover:border-border-strong disabled:opacity-50"
|
|
317
|
+
>
|
|
318
|
+
{catalogLoading ? 'Loading actions…' : 'Actions'}
|
|
319
|
+
</button>
|
|
320
|
+
{open && (
|
|
321
|
+
<div
|
|
322
|
+
ref={menuRef}
|
|
323
|
+
role="menu"
|
|
324
|
+
aria-label="Actions"
|
|
325
|
+
onKeyDown={(event) => {
|
|
326
|
+
const moves: Readonly<Record<string, 'next' | 'previous' | 'first' | 'last'>> = {
|
|
327
|
+
ArrowDown: 'next',
|
|
328
|
+
ArrowUp: 'previous',
|
|
329
|
+
Home: 'first',
|
|
330
|
+
End: 'last',
|
|
331
|
+
}
|
|
332
|
+
const move = moves[event.key]
|
|
333
|
+
|
|
334
|
+
if (move !== undefined) {
|
|
335
|
+
event.preventDefault()
|
|
336
|
+
moveFocus(move)
|
|
337
|
+
} else if (event.key === 'Escape') {
|
|
338
|
+
event.preventDefault()
|
|
339
|
+
closeToButton()
|
|
340
|
+
} else if (event.key === 'Tab') {
|
|
341
|
+
setOpen(false)
|
|
342
|
+
}
|
|
343
|
+
}}
|
|
344
|
+
className="absolute left-0 z-20 mt-1 min-w-[200px] rounded-md border border-border bg-surface-raised py-1 shadow-lg"
|
|
345
|
+
>
|
|
346
|
+
{actions.length === 0 ? (
|
|
347
|
+
<p className="px-3 py-1.5 text-[12px] text-ink-faint">No actions you can run here</p>
|
|
348
|
+
) : (
|
|
349
|
+
actions.map((action) => (
|
|
350
|
+
<button
|
|
351
|
+
key={action.entry.key}
|
|
352
|
+
type="button"
|
|
353
|
+
role="menuitem"
|
|
354
|
+
tabIndex={-1}
|
|
355
|
+
onClick={() => {
|
|
356
|
+
setOpen(false)
|
|
357
|
+
onChoose(action.entry.key)
|
|
358
|
+
}}
|
|
359
|
+
className={`block w-full px-3 py-1.5 text-left text-[13px] transition hover:bg-surface-sunken ${
|
|
360
|
+
action.entry.destructive ? 'text-danger' : 'text-ink'
|
|
361
|
+
}`}
|
|
362
|
+
>
|
|
363
|
+
{action.entry.label}
|
|
364
|
+
</button>
|
|
365
|
+
))
|
|
366
|
+
)}
|
|
367
|
+
</div>
|
|
368
|
+
)}
|
|
369
|
+
</div>
|
|
370
|
+
<button
|
|
371
|
+
type="button"
|
|
372
|
+
onClick={onClear}
|
|
373
|
+
className="ml-auto rounded-md px-2 py-1 text-[12px] font-medium text-ink-muted hover:text-ink"
|
|
374
|
+
>
|
|
375
|
+
Clear
|
|
376
|
+
</button>
|
|
377
|
+
</div>
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function SelectAllBanner({
|
|
382
|
+
selection,
|
|
383
|
+
pageIds,
|
|
384
|
+
hasNextPage,
|
|
385
|
+
matchingCount,
|
|
386
|
+
overLimit,
|
|
387
|
+
noun,
|
|
388
|
+
}: {
|
|
389
|
+
readonly selection: BulkSelectionController
|
|
390
|
+
readonly pageIds: readonly string[]
|
|
391
|
+
readonly hasNextPage: boolean
|
|
392
|
+
readonly matchingCount: number | undefined
|
|
393
|
+
readonly overLimit: boolean
|
|
394
|
+
readonly noun: RecordNoun
|
|
395
|
+
}): React.JSX.Element | null {
|
|
396
|
+
const linkClass = 'font-medium text-accent hover:underline'
|
|
397
|
+
|
|
398
|
+
if (selection.state.mode === 'filter') {
|
|
399
|
+
return (
|
|
400
|
+
<p className="text-[12px] text-ink-muted">
|
|
401
|
+
{overLimit
|
|
402
|
+
? `More than ${BULK_ACTION_MAX_RECORDS.toLocaleString()} ${noun.many} match. One action runs on at most that many, so narrow the filter.`
|
|
403
|
+
: matchingCount === undefined
|
|
404
|
+
? `All matching ${noun.many} are selected.`
|
|
405
|
+
: `All ${matchingCount.toLocaleString()} matching ${matchingCount === 1 ? noun.one : noun.many} are selected.`}{' '}
|
|
406
|
+
<button type="button" onClick={selection.clear} className={linkClass}>
|
|
407
|
+
Clear selection
|
|
408
|
+
</button>
|
|
409
|
+
</p>
|
|
410
|
+
)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const wholePage = pageIds.length > 0 && pageIds.every(selection.isSelected)
|
|
414
|
+
|
|
415
|
+
if (!wholePage || !hasNextPage) {
|
|
416
|
+
return null
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return (
|
|
420
|
+
<p className="text-[12px] text-ink-muted">
|
|
421
|
+
All {countOf(pageIds.length, noun)} on this page are selected.{' '}
|
|
422
|
+
<button type="button" onClick={selection.selectAllMatching} className={linkClass}>
|
|
423
|
+
Select all that match this filter
|
|
424
|
+
</button>
|
|
425
|
+
</p>
|
|
426
|
+
)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* The params form, then the count and the confirmation, then Run.
|
|
431
|
+
*
|
|
432
|
+
* The count comes from a preview of the exact request Run will send, so a
|
|
433
|
+
* `200` there means the job will be accepted unless the data changes first.
|
|
434
|
+
*/
|
|
435
|
+
function BulkActionPanel({
|
|
436
|
+
targetType,
|
|
437
|
+
action,
|
|
438
|
+
selection,
|
|
439
|
+
noun,
|
|
440
|
+
canExport,
|
|
441
|
+
onCancel,
|
|
442
|
+
onStarted,
|
|
443
|
+
}: {
|
|
444
|
+
readonly targetType: BulkActionTargetType
|
|
445
|
+
readonly action: RenderableAction
|
|
446
|
+
readonly selection: BulkSelectionController
|
|
447
|
+
readonly noun: RecordNoun
|
|
448
|
+
/** The catalog offers `core.export` on this type to this reader. */
|
|
449
|
+
readonly canExport: boolean
|
|
450
|
+
readonly onCancel: () => void
|
|
451
|
+
readonly onStarted: (job: BulkJob) => void
|
|
452
|
+
}): React.JSX.Element {
|
|
453
|
+
const { entry, fields, allFields, preset } = action
|
|
454
|
+
const [values, setValues] = useState<ParamValues>(() => initialParamValues(fields))
|
|
455
|
+
const [choices, setChoices] = useState<Readonly<Record<string, string>>>(() => preset?.choices ?? {})
|
|
456
|
+
const headingRef = useRef<HTMLHeadingElement>(null)
|
|
457
|
+
|
|
458
|
+
// The reader chose this action from the menu, so the focus follows it here.
|
|
459
|
+
useEffect(() => {
|
|
460
|
+
headingRef.current?.focus()
|
|
461
|
+
}, [])
|
|
462
|
+
const [reviewing, setReviewing] = useState(fields.length === 0)
|
|
463
|
+
const [typed, setTyped] = useState('')
|
|
464
|
+
// An export started from here holds Run until its file is saved, or until
|
|
465
|
+
// the reader says to go on without it: the file is built when it is
|
|
466
|
+
// downloaded, so after the delete it would hold nothing.
|
|
467
|
+
//
|
|
468
|
+
// The export's job and outcome live here, beside the hold they decide, so
|
|
469
|
+
// Back and Continue redraw the export panel as it was, release button
|
|
470
|
+
// included, rather than leaving Run held with no way to free it.
|
|
471
|
+
const [exportRun, setExportRun] = useState<ExportRun | undefined>(undefined)
|
|
472
|
+
const exportHeld = exportRun !== undefined && (exportRun.outcome === 'waiting' || exportRun.outcome === 'waiving')
|
|
473
|
+
const create = useCreateBulkJob()
|
|
474
|
+
const asked = paramsFromValues(fields, values)
|
|
475
|
+
const params = asked === undefined ? undefined : { ...preset?.params, ...asked }
|
|
476
|
+
const request =
|
|
477
|
+
reviewing && params !== undefined && selection.selection !== undefined
|
|
478
|
+
? { targetType, action: entry.key, params, selection: selection.selection }
|
|
479
|
+
: undefined
|
|
480
|
+
const preview = useBulkActionPreview(request)
|
|
481
|
+
const count = preview.preview?.count
|
|
482
|
+
const needsTypedCount = entry.destructive && count !== undefined && count > TYPED_CONFIRM_THRESHOLD
|
|
483
|
+
const canRun =
|
|
484
|
+
request !== undefined &&
|
|
485
|
+
count !== undefined &&
|
|
486
|
+
count > 0 &&
|
|
487
|
+
preview.preview?.overLimit === false &&
|
|
488
|
+
(!needsTypedCount || typed.trim() === String(count)) &&
|
|
489
|
+
!exportHeld &&
|
|
490
|
+
!create.isPending
|
|
491
|
+
|
|
492
|
+
function run(): void {
|
|
493
|
+
if (!canRun || request === undefined) {
|
|
494
|
+
return
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
create
|
|
498
|
+
.runAsync(request)
|
|
499
|
+
.then(onStarted)
|
|
500
|
+
// The failure is shown from `create.error` below.
|
|
501
|
+
.catch(() => undefined)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return (
|
|
505
|
+
<section
|
|
506
|
+
aria-label={entry.label}
|
|
507
|
+
className="max-w-lg rounded-md border border-border bg-surface-raised p-4"
|
|
508
|
+
onKeyDown={(event) => {
|
|
509
|
+
if (event.key === 'Escape' && !create.isPending) {
|
|
510
|
+
onCancel()
|
|
511
|
+
}
|
|
512
|
+
}}
|
|
513
|
+
>
|
|
514
|
+
<h2 ref={headingRef} tabIndex={-1} className="text-[13px] font-semibold text-ink outline-none">
|
|
515
|
+
{entry.label}
|
|
516
|
+
</h2>
|
|
517
|
+
|
|
518
|
+
{!reviewing ? (
|
|
519
|
+
<form
|
|
520
|
+
className="mt-3 space-y-3"
|
|
521
|
+
onSubmit={(event) => {
|
|
522
|
+
event.preventDefault()
|
|
523
|
+
|
|
524
|
+
if (params !== undefined) {
|
|
525
|
+
setReviewing(true)
|
|
526
|
+
}
|
|
527
|
+
}}
|
|
528
|
+
>
|
|
529
|
+
<BulkParamFields
|
|
530
|
+
fields={fields}
|
|
531
|
+
values={values}
|
|
532
|
+
onChange={(name, value, choice) => {
|
|
533
|
+
setValues((current) => ({ ...current, [name]: value }))
|
|
534
|
+
setChoices((current) => {
|
|
535
|
+
const next = { ...current }
|
|
536
|
+
|
|
537
|
+
if (choice === undefined) {
|
|
538
|
+
delete next[name]
|
|
539
|
+
} else {
|
|
540
|
+
next[name] = choice
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
return next
|
|
544
|
+
})
|
|
545
|
+
}}
|
|
546
|
+
/>
|
|
547
|
+
<div className="flex justify-end gap-2">
|
|
548
|
+
<button
|
|
549
|
+
type="button"
|
|
550
|
+
onClick={onCancel}
|
|
551
|
+
className="rounded-md px-2.5 py-1 text-[12px] font-medium text-ink-muted hover:text-ink"
|
|
552
|
+
>
|
|
553
|
+
Cancel
|
|
554
|
+
</button>
|
|
555
|
+
<button
|
|
556
|
+
type="submit"
|
|
557
|
+
disabled={params === undefined}
|
|
558
|
+
className="rounded-md bg-accent px-3 py-1.5 text-[12px] font-semibold text-accent-fg hover:bg-accent-hover disabled:opacity-50"
|
|
559
|
+
>
|
|
560
|
+
Continue
|
|
561
|
+
</button>
|
|
562
|
+
</div>
|
|
563
|
+
</form>
|
|
564
|
+
) : (
|
|
565
|
+
<div className="mt-3 space-y-3">
|
|
566
|
+
{preview.isLoading && <p className="text-[12px] text-ink-muted">Counting records…</p>}
|
|
567
|
+
{preview.error !== null && <BulkError error={preview.error} />}
|
|
568
|
+
{preview.preview !== undefined &&
|
|
569
|
+
(preview.preview.overLimit ? (
|
|
570
|
+
<p className="text-[13px] text-danger">
|
|
571
|
+
This selection is over the limit of {BULK_ACTION_MAX_RECORDS.toLocaleString()} records. Narrow
|
|
572
|
+
the filter and try again.
|
|
573
|
+
</p>
|
|
574
|
+
) : preview.preview.count === 0 ? (
|
|
575
|
+
<p className="text-[13px] text-ink-muted">None of the selected records can be changed.</p>
|
|
576
|
+
) : (
|
|
577
|
+
<p className="text-[13px] text-ink">
|
|
578
|
+
{params === undefined
|
|
579
|
+
? `${entry.label}: ${countOf(preview.preview.count, noun)}.`
|
|
580
|
+
: runSummary({
|
|
581
|
+
actionKey: entry.key,
|
|
582
|
+
actionLabel: entry.label,
|
|
583
|
+
fields: allFields,
|
|
584
|
+
params,
|
|
585
|
+
choices,
|
|
586
|
+
records: countOf(preview.preview.count, noun),
|
|
587
|
+
})}
|
|
588
|
+
</p>
|
|
589
|
+
))}
|
|
590
|
+
{entry.destructive && count !== undefined && count > 0 && (
|
|
591
|
+
<div className="rounded-md border border-danger/40 bg-danger-soft px-3 py-2 text-[12px] text-danger">
|
|
592
|
+
<p>This cannot be undone.</p>
|
|
593
|
+
{canExport && request !== undefined && entry.key !== EXPORT_ACTION && (
|
|
594
|
+
<ExportFirst
|
|
595
|
+
targetType={targetType}
|
|
596
|
+
selection={request.selection}
|
|
597
|
+
run={exportRun}
|
|
598
|
+
onChange={setExportRun}
|
|
599
|
+
/>
|
|
600
|
+
)}
|
|
601
|
+
{needsTypedCount && (
|
|
602
|
+
<label className="mt-2 block font-medium">
|
|
603
|
+
Type {count} to confirm
|
|
604
|
+
<input
|
|
605
|
+
value={typed}
|
|
606
|
+
inputMode="numeric"
|
|
607
|
+
onChange={(event) => {
|
|
608
|
+
setTyped(event.target.value)
|
|
609
|
+
}}
|
|
610
|
+
className="mt-1 block w-32 rounded-md border border-danger/40 bg-surface px-2 py-1 text-[13px] text-ink outline-none focus:border-danger"
|
|
611
|
+
/>
|
|
612
|
+
</label>
|
|
613
|
+
)}
|
|
614
|
+
</div>
|
|
615
|
+
)}
|
|
616
|
+
{create.error !== null && <BulkError error={create.error} />}
|
|
617
|
+
{exportHeld && (
|
|
618
|
+
<p className="text-[12px] text-ink-muted">
|
|
619
|
+
Download the export, or choose to go on without it, before you run this.
|
|
620
|
+
</p>
|
|
621
|
+
)}
|
|
622
|
+
<div className="flex justify-end gap-2">
|
|
623
|
+
<button
|
|
624
|
+
type="button"
|
|
625
|
+
onClick={() => {
|
|
626
|
+
if (fields.length === 0) {
|
|
627
|
+
onCancel()
|
|
628
|
+
} else {
|
|
629
|
+
setReviewing(false)
|
|
630
|
+
}
|
|
631
|
+
}}
|
|
632
|
+
disabled={create.isPending}
|
|
633
|
+
className="rounded-md px-2.5 py-1 text-[12px] font-medium text-ink-muted hover:text-ink disabled:opacity-50"
|
|
634
|
+
>
|
|
635
|
+
{fields.length === 0 ? 'Cancel' : 'Back'}
|
|
636
|
+
</button>
|
|
637
|
+
<button
|
|
638
|
+
type="button"
|
|
639
|
+
onClick={run}
|
|
640
|
+
disabled={!canRun}
|
|
641
|
+
className={`rounded-md px-3 py-1.5 text-[12px] font-semibold disabled:opacity-50 ${
|
|
642
|
+
entry.destructive
|
|
643
|
+
? 'bg-danger text-danger-fg hover:opacity-90'
|
|
644
|
+
: 'bg-accent text-accent-fg hover:bg-accent-hover'
|
|
645
|
+
}`}
|
|
646
|
+
>
|
|
647
|
+
{create.isPending ? 'Starting…' : count === undefined ? 'Run' : `Run on ${countOf(count, noun)}`}
|
|
648
|
+
</button>
|
|
649
|
+
</div>
|
|
650
|
+
</div>
|
|
651
|
+
)}
|
|
652
|
+
</section>
|
|
653
|
+
)
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* A running job, polled until it ends. On the end, the page's list and what
|
|
658
|
+
* else the job may have changed are read again.
|
|
659
|
+
*/
|
|
660
|
+
function BulkJobProgress({
|
|
661
|
+
jobId,
|
|
662
|
+
label,
|
|
663
|
+
targetType,
|
|
664
|
+
onDismiss,
|
|
665
|
+
}: {
|
|
666
|
+
readonly jobId: string
|
|
667
|
+
readonly label: string
|
|
668
|
+
readonly targetType: BulkActionTargetType
|
|
669
|
+
readonly onDismiss: () => void
|
|
670
|
+
}): React.JSX.Element {
|
|
671
|
+
const watched = useBulkJob(jobId)
|
|
672
|
+
const cancel = useCancelBulkJob()
|
|
673
|
+
const refresh = useRefreshAfterBulkJob()
|
|
674
|
+
const job = watched.record
|
|
675
|
+
const ended = job !== undefined && isBulkJobFinal(job.status)
|
|
676
|
+
|
|
677
|
+
const actionKey = job?.action
|
|
678
|
+
|
|
679
|
+
useEffect(() => {
|
|
680
|
+
if (ended && actionKey !== undefined) {
|
|
681
|
+
refresh(targetType, actionKey)
|
|
682
|
+
}
|
|
683
|
+
}, [ended, refresh, targetType, actionKey])
|
|
684
|
+
|
|
685
|
+
return (
|
|
686
|
+
<section
|
|
687
|
+
aria-label={`${label} progress`}
|
|
688
|
+
className="mb-3 rounded-md border border-border bg-surface-raised px-4 py-3"
|
|
689
|
+
>
|
|
690
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
691
|
+
<p className="text-[13px] font-semibold text-ink">{label}</p>
|
|
692
|
+
{job !== undefined && (
|
|
693
|
+
<span className="text-[12px] text-ink-muted" aria-live="polite">
|
|
694
|
+
{STATUS_TEXT[job.status]}
|
|
695
|
+
</span>
|
|
696
|
+
)}
|
|
697
|
+
<div className="ml-auto flex items-center gap-2">
|
|
698
|
+
{job !== undefined && job.downloadUrl !== null && <BulkJobDownload job={job} />}
|
|
699
|
+
<Link
|
|
700
|
+
to={`/bulk-jobs/${jobId}`}
|
|
701
|
+
className="rounded-md px-2 py-1 text-[12px] font-medium text-accent hover:underline"
|
|
702
|
+
>
|
|
703
|
+
Open in Bulk jobs
|
|
704
|
+
</Link>
|
|
705
|
+
{job !== undefined && !ended && (
|
|
706
|
+
<button
|
|
707
|
+
type="button"
|
|
708
|
+
disabled={cancel.isPending}
|
|
709
|
+
onClick={() => {
|
|
710
|
+
cancel.run(job.id)
|
|
711
|
+
}}
|
|
712
|
+
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"
|
|
713
|
+
>
|
|
714
|
+
{cancel.isPending ? 'Cancelling…' : 'Cancel'}
|
|
715
|
+
</button>
|
|
716
|
+
)}
|
|
717
|
+
{(ended || watched.error !== null) && (
|
|
718
|
+
<button
|
|
719
|
+
type="button"
|
|
720
|
+
onClick={onDismiss}
|
|
721
|
+
className="rounded-md px-2 py-1 text-[12px] font-medium text-ink-muted hover:text-ink"
|
|
722
|
+
>
|
|
723
|
+
Dismiss
|
|
724
|
+
</button>
|
|
725
|
+
)}
|
|
726
|
+
</div>
|
|
727
|
+
</div>
|
|
728
|
+
|
|
729
|
+
{watched.error !== null && (
|
|
730
|
+
<div className="mt-2">
|
|
731
|
+
<BulkError error={watched.error} />
|
|
732
|
+
</div>
|
|
733
|
+
)}
|
|
734
|
+
{cancel.error !== null && (
|
|
735
|
+
<div className="mt-2">
|
|
736
|
+
<BulkError error={cancel.error} />
|
|
737
|
+
</div>
|
|
738
|
+
)}
|
|
739
|
+
|
|
740
|
+
{job !== undefined && <JobCounts job={job} />}
|
|
741
|
+
</section>
|
|
742
|
+
)
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* Before a destructive action: a link that starts an export of the same
|
|
747
|
+
* selection, then offers its file once the export job has run.
|
|
748
|
+
*
|
|
749
|
+
* The file is built from the records when it is downloaded, not when the
|
|
750
|
+
* export runs. So from the start of the export until its file is saved, the
|
|
751
|
+
* panel holds the destructive action. Saving the file, or saying outright
|
|
752
|
+
* to go on without it, lets it run.
|
|
753
|
+
*/
|
|
754
|
+
function ExportFirst({
|
|
755
|
+
targetType,
|
|
756
|
+
selection,
|
|
757
|
+
run,
|
|
758
|
+
onChange,
|
|
759
|
+
}: {
|
|
760
|
+
readonly targetType: BulkActionTargetType
|
|
761
|
+
readonly selection: BulkActionInput['selection']
|
|
762
|
+
/** The export started from this panel, if any. The panel holds Run while its outcome is open. */
|
|
763
|
+
readonly run: ExportRun | undefined
|
|
764
|
+
readonly onChange: (run: ExportRun) => void
|
|
765
|
+
}): React.JSX.Element {
|
|
766
|
+
const create = useCreateBulkJob()
|
|
767
|
+
const watched = useBulkJob(run?.jobId)
|
|
768
|
+
const job = watched.record
|
|
769
|
+
|
|
770
|
+
if (run === undefined) {
|
|
771
|
+
return (
|
|
772
|
+
<div className="mt-2 text-ink">
|
|
773
|
+
<button
|
|
774
|
+
type="button"
|
|
775
|
+
disabled={create.isPending}
|
|
776
|
+
onClick={() => {
|
|
777
|
+
create
|
|
778
|
+
.runAsync({ targetType, action: EXPORT_ACTION, params: {}, selection })
|
|
779
|
+
.then((started) => {
|
|
780
|
+
onChange({ jobId: started.id, outcome: 'waiting' })
|
|
781
|
+
})
|
|
782
|
+
// The failure is shown from `create.error` below.
|
|
783
|
+
.catch(() => undefined)
|
|
784
|
+
}}
|
|
785
|
+
className="font-medium text-accent hover:underline disabled:opacity-50"
|
|
786
|
+
>
|
|
787
|
+
{create.isPending ? 'Starting the export…' : 'Export these first'}
|
|
788
|
+
</button>
|
|
789
|
+
{create.error !== null && (
|
|
790
|
+
<div className="mt-1">
|
|
791
|
+
<BulkError error={create.error} />
|
|
792
|
+
</div>
|
|
793
|
+
)}
|
|
794
|
+
</div>
|
|
795
|
+
)
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
return (
|
|
799
|
+
<section aria-label="Export" className="mt-2 space-y-1.5 text-ink">
|
|
800
|
+
<div className="flex flex-wrap items-center gap-2" aria-live="polite">
|
|
801
|
+
<span>Export: {job === undefined ? 'Starting' : STATUS_TEXT[job.status]}</span>
|
|
802
|
+
{job !== undefined && job.downloadUrl !== null && (
|
|
803
|
+
<BulkJobDownload
|
|
804
|
+
job={job}
|
|
805
|
+
onSaved={() => {
|
|
806
|
+
onChange({ jobId: run.jobId, outcome: 'saved' })
|
|
807
|
+
}}
|
|
808
|
+
/>
|
|
809
|
+
)}
|
|
810
|
+
</div>
|
|
811
|
+
{watched.error !== null && <BulkError error={watched.error} />}
|
|
812
|
+
<p className="text-ink-muted">
|
|
813
|
+
The file shows the records as they are when you download it. Download it before you run this.
|
|
814
|
+
</p>
|
|
815
|
+
{run.outcome === 'saved' && <p>The file is saved. You can run this now.</p>}
|
|
816
|
+
{run.outcome === 'waived' && <p>You chose to go on without the file.</p>}
|
|
817
|
+
{run.outcome === 'waiting' && (
|
|
818
|
+
<button
|
|
819
|
+
type="button"
|
|
820
|
+
onClick={() => {
|
|
821
|
+
onChange({ jobId: run.jobId, outcome: 'waiving' })
|
|
822
|
+
}}
|
|
823
|
+
className="font-medium text-ink-muted hover:text-ink hover:underline"
|
|
824
|
+
>
|
|
825
|
+
Go on without the file
|
|
826
|
+
</button>
|
|
827
|
+
)}
|
|
828
|
+
{run.outcome === 'waiving' && (
|
|
829
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
830
|
+
<span>Run this without the exported file?</span>
|
|
831
|
+
<button
|
|
832
|
+
type="button"
|
|
833
|
+
onClick={() => {
|
|
834
|
+
onChange({ jobId: run.jobId, outcome: 'waived' })
|
|
835
|
+
}}
|
|
836
|
+
className="font-medium text-danger hover:underline"
|
|
837
|
+
>
|
|
838
|
+
Yes, go on without it
|
|
839
|
+
</button>
|
|
840
|
+
<button
|
|
841
|
+
type="button"
|
|
842
|
+
onClick={() => {
|
|
843
|
+
onChange({ jobId: run.jobId, outcome: 'waiting' })
|
|
844
|
+
}}
|
|
845
|
+
className="font-medium text-ink-muted hover:text-ink"
|
|
846
|
+
>
|
|
847
|
+
Keep waiting
|
|
848
|
+
</button>
|
|
849
|
+
</div>
|
|
850
|
+
)}
|
|
851
|
+
</section>
|
|
852
|
+
)
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/** The file a finished job made, saved by the browser when asked. */
|
|
856
|
+
export function BulkJobDownload({
|
|
857
|
+
job,
|
|
858
|
+
onSaved,
|
|
859
|
+
}: {
|
|
860
|
+
readonly job: BulkJob
|
|
861
|
+
/** Called once the browser has been handed the file. */
|
|
862
|
+
readonly onSaved?: (() => void) | undefined
|
|
863
|
+
}): React.JSX.Element {
|
|
864
|
+
const download = useDownloadBulkJob()
|
|
865
|
+
|
|
866
|
+
return (
|
|
867
|
+
<>
|
|
868
|
+
<button
|
|
869
|
+
type="button"
|
|
870
|
+
disabled={download.isPending}
|
|
871
|
+
onClick={() => {
|
|
872
|
+
download
|
|
873
|
+
.runAsync(job)
|
|
874
|
+
.then((file) => {
|
|
875
|
+
saveCsv(file)
|
|
876
|
+
onSaved?.()
|
|
877
|
+
})
|
|
878
|
+
// The failure is shown from `download.error` below.
|
|
879
|
+
.catch(() => undefined)
|
|
880
|
+
}}
|
|
881
|
+
className="rounded-md border border-border px-2.5 py-1 text-[12px] font-medium text-ink transition hover:border-accent hover:text-accent disabled:opacity-50"
|
|
882
|
+
>
|
|
883
|
+
{download.isPending ? 'Downloading…' : 'Download CSV'}
|
|
884
|
+
</button>
|
|
885
|
+
{download.error !== null && <BulkError error={download.error} />}
|
|
886
|
+
</>
|
|
887
|
+
)
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
export function JobCounts({ job }: { readonly job: BulkJob }): React.JSX.Element {
|
|
891
|
+
const unlisted = job.failedCount - job.failures.length
|
|
892
|
+
|
|
893
|
+
return (
|
|
894
|
+
<>
|
|
895
|
+
<div
|
|
896
|
+
role="progressbar"
|
|
897
|
+
aria-label="Records processed"
|
|
898
|
+
aria-valuemin={0}
|
|
899
|
+
aria-valuemax={job.totalCount}
|
|
900
|
+
aria-valuenow={job.processedCount}
|
|
901
|
+
className="mt-2 h-1.5 w-full overflow-hidden rounded-full bg-surface-sunken"
|
|
902
|
+
>
|
|
903
|
+
<div
|
|
904
|
+
className="h-full bg-accent transition-[width]"
|
|
905
|
+
style={{ width: `${String(job.totalCount === 0 ? 100 : (job.processedCount / job.totalCount) * 100)}%` }}
|
|
906
|
+
/>
|
|
907
|
+
</div>
|
|
908
|
+
<p className="mt-1 text-[12px] text-ink-muted">
|
|
909
|
+
{job.processedCount.toLocaleString()} of {job.totalCount.toLocaleString()} processed ·{' '}
|
|
910
|
+
{job.succeededCount.toLocaleString()} succeeded · {job.skippedCount.toLocaleString()} skipped ·{' '}
|
|
911
|
+
{job.failedCount.toLocaleString()} failed
|
|
912
|
+
</p>
|
|
913
|
+
{job.failureReason !== null && <p className="mt-1 text-[12px] text-danger">{job.failureReason}</p>}
|
|
914
|
+
{job.failures.length > 0 && (
|
|
915
|
+
<div className="mt-2">
|
|
916
|
+
<p className="text-[12px] font-medium text-ink">Records that failed</p>
|
|
917
|
+
<ul className="mt-1 max-h-48 space-y-0.5 overflow-y-auto">
|
|
918
|
+
{job.failures.map((failure) => (
|
|
919
|
+
<li key={failure.recordId} className="text-[12px] text-ink-muted">
|
|
920
|
+
<span className="font-medium text-ink">{failure.recordName ?? failure.recordId}</span>:{' '}
|
|
921
|
+
{failure.message}
|
|
922
|
+
</li>
|
|
923
|
+
))}
|
|
924
|
+
</ul>
|
|
925
|
+
{unlisted > 0 && (
|
|
926
|
+
<p className="mt-1 text-[11px] text-ink-faint">
|
|
927
|
+
{unlisted.toLocaleString()} more failed and are not listed.
|
|
928
|
+
</p>
|
|
929
|
+
)}
|
|
930
|
+
</div>
|
|
931
|
+
)}
|
|
932
|
+
</>
|
|
933
|
+
)
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* A refusal in the server's own words. `ErrorPanel` turns every `403` into
|
|
938
|
+
* "not available to your account", which is right for a page and wrong here:
|
|
939
|
+
* a bulk `403` names the missing scope or role, and a `409` says why the job
|
|
940
|
+
* cannot start or stop.
|
|
941
|
+
*/
|
|
942
|
+
export function BulkError({ error }: { readonly error: Error }): React.JSX.Element {
|
|
943
|
+
if (!(error instanceof ApiError) || error.code === 'service_unreachable') {
|
|
944
|
+
return <ErrorPanel error={error} />
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
return (
|
|
948
|
+
<div role="alert" className="rounded-md border border-danger/40 bg-danger-soft px-3 py-2">
|
|
949
|
+
<p className="text-[12px] font-medium text-danger">{error.message}</p>
|
|
950
|
+
{error.details.length > 0 && (
|
|
951
|
+
<ul className="mt-1 space-y-0.5">
|
|
952
|
+
{error.details.map((detail) => (
|
|
953
|
+
<li key={`${detail.field}-${detail.message}`} className="text-[12px] text-danger">
|
|
954
|
+
{detail.message}
|
|
955
|
+
</li>
|
|
956
|
+
))}
|
|
957
|
+
</ul>
|
|
958
|
+
)}
|
|
959
|
+
</div>
|
|
960
|
+
)
|
|
961
|
+
}
|