This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathuseInboxBulkActions.ts
More file actions
457 lines (412 loc) · 13.9 KB
/
Copy pathuseInboxBulkActions.ts
File metadata and controls
457 lines (412 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import {
buildBulkActionEvents,
type InboxBulkActionType,
} from "@posthog/core/inbox/engagement";
import { inboxStatusLabel } from "@posthog/core/inbox/reportPresentation";
import type { InboxReportActionSurface } from "@posthog/shared/analytics-events";
import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";
import type { SignalReport } from "@posthog/shared/types";
import type { DismissReportDialogResult } from "@posthog/ui/features/inbox/components/DismissReportDialog";
import { reportKeys } from "@posthog/ui/features/inbox/hooks/useInboxReports";
import { useInboxReportSelectionStore } from "@posthog/ui/features/inbox/stores/inboxReportSelectionStore";
import { useAuthenticatedMutation } from "@posthog/ui/hooks/useAuthenticatedMutation";
import { track } from "@posthog/ui/shell/analytics";
import { useQueryClient } from "@tanstack/react-query";
import { useCallback, useMemo } from "react";
import { toast } from "sonner";
type BulkActionName = "suppress" | "snooze" | "delete" | "reingest";
interface BulkActionResult {
successCount: number;
failureCount: number;
succeededIds: string[];
}
async function runBulkAction(
reportIds: string[],
perItem: (reportId: string) => Promise<unknown>,
): Promise<BulkActionResult> {
const results = await Promise.allSettled(reportIds.map(perItem));
const succeededIds: string[] = [];
for (let i = 0; i < results.length; i += 1) {
if (results[i].status === "fulfilled") {
succeededIds.push(reportIds[i]);
}
}
return {
successCount: succeededIds.length,
failureCount: results.length - succeededIds.length,
succeededIds,
};
}
/** Active workflow statuses for snooze and suppress. Terminal `suppressed` / `deleted` are excluded. */
const suppressibleStatuses = new Set<SignalReport["status"]>([
"potential",
"candidate",
"in_progress",
"pending_input",
"ready",
"failed",
]);
/** Clause after "Disabled because …" (see `@posthog/ui/primitives/Button`). */
const DISABLED_NO_SELECTION = "you haven't selected a report";
/** Statuses that block suppression; labels match `inboxStatusLabel`. */
const SUPPRESS_BLOCKED_STATUS_PHRASE = (
["suppressed", "deleted"] as const satisfies readonly SignalReport["status"][]
)
.map((status) => inboxStatusLabel(status))
.join(" or ");
type SelectedReportEligibility = {
selectedReports: SignalReport[];
selectedIds: string[];
selectedCount: number;
snoozeDisabledReason: string | null;
suppressDisabledReason: string | null;
deleteDisabledReason: string | null;
reingestDisabledReason: string | null;
};
function formatBulkActionSummary(
action: BulkActionName,
result: BulkActionResult,
): string {
const { successCount, failureCount } = result;
const pluralized = successCount === 1 ? "report" : "reports";
const formulated =
action === "suppress"
? `${pluralized} dismissed`
: action === "snooze"
? `${pluralized} snoozed`
: action === "delete"
? `${pluralized} deleted`
: `${pluralized} reingested`;
if (failureCount === 0) {
return `${successCount} ${formulated}`;
}
return `${successCount} ${formulated}, ${failureCount} failed`;
}
function getSnoozeOrSuppressDisabledReason(
selectedCount: number,
selectedReports: SignalReport[],
): string | null {
if (selectedCount === 0) {
return DISABLED_NO_SELECTION;
}
const ok = selectedReports.every((report) =>
suppressibleStatuses.has(report.status),
);
if (ok) {
return null;
}
return `every selected report must not already be ${SUPPRESS_BLOCKED_STATUS_PHRASE}`;
}
function getSelectedReportEligibility(
reports: SignalReport[],
selectedIds: string[],
): SelectedReportEligibility {
const selectedIdSet = new Set(selectedIds);
const selectedReports = reports.filter((report) =>
selectedIdSet.has(report.id),
);
const selectedCount = selectedReports.length;
const snoozeOrSuppressDisabledReason = getSnoozeOrSuppressDisabledReason(
selectedCount,
selectedReports,
);
return {
selectedReports,
selectedIds: selectedReports.map((report) => report.id),
selectedCount,
snoozeDisabledReason: snoozeOrSuppressDisabledReason,
suppressDisabledReason: snoozeOrSuppressDisabledReason,
deleteDisabledReason: selectedCount === 0 ? DISABLED_NO_SELECTION : null,
reingestDisabledReason: selectedCount === 0 ? DISABLED_NO_SELECTION : null,
};
}
/** Toolbar: selected report ids. Dismiss dialog: that report's id, or null when closed. */
export type InboxBulkSelection = string[] | string | null;
const emptyBulkIds: string[] = [];
function effectiveBulkIdsFromSelection(
selection: InboxBulkSelection,
): string[] {
if (selection == null) {
return emptyBulkIds;
}
if (Array.isArray(selection)) {
return selection;
}
return [selection];
}
/** Snooze disabled reason when `selectedIds` are treated as the bulk selection (matches toolbar logic). */
export function inboxBulkSnoozeDisabledReason(
reports: SignalReport[],
selectedIds: string[],
): string | null {
return getSelectedReportEligibility(reports, selectedIds)
.snoozeDisabledReason;
}
/** Suppress/dismiss disabled reason when `selectedIds` are treated as the bulk selection. */
export function inboxBulkSuppressDisabledReason(
reports: SignalReport[],
selectedIds: string[],
): string | null {
return getSelectedReportEligibility(reports, selectedIds)
.suppressDisabledReason;
}
/**
* Per-report suppress-disabled reason precomputed in one O(N) pass.
* Cheaper than calling `inboxBulkSuppressDisabledReason(reports, [id])` per
* card, which collapses to O(N²) when the list rerenders on every keystroke.
*
* The disabled-reason depends only on `report.status` here (single-report
* selection always has count ≥ 1), so we can compute each entry directly
* without re-running the full eligibility pipeline.
*/
export function buildSuppressDisabledReasonMap(
reports: SignalReport[],
): Map<string, string | null> {
const blockedReason = `every selected report must not already be ${SUPPRESS_BLOCKED_STATUS_PHRASE}`;
const map = new Map<string, string | null>();
for (const report of reports) {
map.set(
report.id,
suppressibleStatuses.has(report.status) ? null : blockedReason,
);
}
return map;
}
export function useInboxBulkActions(
reports: SignalReport[],
selection: InboxBulkSelection,
surface: InboxReportActionSurface = "toolbar",
) {
const queryClient = useQueryClient();
const clearSelection = useInboxReportSelectionStore(
(state) => state.clearSelection,
);
const removeFromSelection = useInboxReportSelectionStore(
(state) => state.removeFromSelection,
);
/**
* Emit one `INBOX_REPORT_ACTION` per report the action succeeded for. Resolves
* report metadata from the pre-mutation list (the query is invalidated right
* after, dropping the affected reports), and skips firing when nothing landed.
*/
const trackBulkAction = useCallback(
(
actionType: InboxBulkActionType,
result: BulkActionResult,
dismissal?: DismissReportDialogResult,
) => {
if (result.successCount === 0) return;
const byId = new Map(reports.map((report) => [report.id, report]));
const succeeded = result.succeededIds
.map((id) => byId.get(id))
.filter((report): report is SignalReport => report !== undefined);
if (succeeded.length === 0) return;
const events = buildBulkActionEvents({
reports: succeeded,
actionType,
surface,
dismissal: dismissal
? { reason: dismissal.reason, note: dismissal.note }
: undefined,
});
for (const event of events) {
track(ANALYTICS_EVENTS.INBOX_REPORT_ACTION, event);
}
},
[reports, surface],
);
/**
* Reflect a bulk-action result in the selection: drop succeeded ids so the
* user can retry the failed subset; keep everything if nothing succeeded so
* the toast's "N failed" still points at a real selection.
*/
const applyBulkResultToSelection = useCallback(
(result: BulkActionResult) => {
if (result.successCount === 0) return;
if (result.failureCount === 0) {
clearSelection();
return;
}
removeFromSelection(result.succeededIds);
},
[clearSelection, removeFromSelection],
);
const effectiveBulkIds = effectiveBulkIdsFromSelection(selection);
const eligibility = useMemo(
() => getSelectedReportEligibility(reports, effectiveBulkIds),
[reports, effectiveBulkIds],
);
const invalidateInboxQueries = useCallback(async () => {
await queryClient.invalidateQueries({
queryKey: reportKeys.all,
exact: false,
});
}, [queryClient]);
const suppressMutation = useAuthenticatedMutation(
async (
client,
input: { reportIds: string[]; dismissal?: DismissReportDialogResult },
) => {
// TODO: When dismissing a report that has an open implementation PR
// (implementation_pr_url), close that PR on GitHub – likely in main, not here.
return runBulkAction(input.reportIds, (reportId) =>
client.updateSignalReportState(reportId, {
state: "suppressed",
...(input.dismissal
? {
dismissal_reason: input.dismissal.reason,
dismissal_note: input.dismissal.note.slice(0, 4000),
}
: {}),
}),
);
},
{
onSuccess: async (result, variables) => {
trackBulkAction("dismiss", result, variables.dismissal);
await invalidateInboxQueries();
applyBulkResultToSelection(result);
if (result.failureCount > 0) {
toast.error(formatBulkActionSummary("suppress", result));
return;
}
toast.success(formatBulkActionSummary("suppress", result));
},
onError: (error) => {
toast.error(error.message || "Failed to dismiss reports");
},
},
);
const snoozeMutation = useAuthenticatedMutation(
async (client, reportIds: string[]) =>
runBulkAction(reportIds, (reportId) =>
client.updateSignalReportState(reportId, {
state: "potential",
snooze_for: 1,
}),
),
{
onSuccess: async (result) => {
trackBulkAction("snooze", result);
await invalidateInboxQueries();
applyBulkResultToSelection(result);
if (result.failureCount > 0) {
toast.error(formatBulkActionSummary("snooze", result));
return;
}
toast.success(formatBulkActionSummary("snooze", result));
},
onError: (error) => {
toast.error(error.message || "Failed to snooze reports");
},
},
);
const deleteMutation = useAuthenticatedMutation(
async (client, reportIds: string[]) =>
runBulkAction(reportIds, (reportId) =>
client.deleteSignalReport(reportId),
),
{
onSuccess: async (result) => {
trackBulkAction("delete", result);
await invalidateInboxQueries();
applyBulkResultToSelection(result);
if (result.failureCount > 0) {
toast.error(formatBulkActionSummary("delete", result));
return;
}
toast.success(formatBulkActionSummary("delete", result));
},
onError: (error) => {
toast.error(error.message || "Failed to delete reports");
},
},
);
const reingestMutation = useAuthenticatedMutation(
async (client, reportIds: string[]) =>
runBulkAction(reportIds, (reportId) =>
client.reingestSignalReport(reportId),
),
{
onSuccess: async (result) => {
trackBulkAction("reingest", result);
await invalidateInboxQueries();
applyBulkResultToSelection(result);
if (result.failureCount > 0) {
toast.error(formatBulkActionSummary("reingest", result));
return;
}
toast.success(formatBulkActionSummary("reingest", result));
},
onError: (error) => {
toast.error(error.message || "Failed to reingest reports");
},
},
);
const suppressSelected = useCallback(
async (dismissal?: DismissReportDialogResult) => {
if (eligibility.suppressDisabledReason !== null) {
return false;
}
await suppressMutation.mutateAsync({
reportIds: eligibility.selectedIds,
...(dismissal != null ? { dismissal } : {}),
});
return true;
},
[
eligibility.suppressDisabledReason,
eligibility.selectedIds,
suppressMutation,
],
);
const snoozeSelected = useCallback(async () => {
if (eligibility.snoozeDisabledReason !== null) {
return false;
}
await snoozeMutation.mutateAsync(eligibility.selectedIds);
return true;
}, [
eligibility.snoozeDisabledReason,
eligibility.selectedIds,
snoozeMutation,
]);
const deleteSelected = useCallback(async () => {
if (eligibility.deleteDisabledReason !== null) {
return false;
}
await deleteMutation.mutateAsync(eligibility.selectedIds);
return true;
}, [
deleteMutation,
eligibility.deleteDisabledReason,
eligibility.selectedIds,
]);
const reingestSelected = useCallback(async () => {
if (eligibility.reingestDisabledReason !== null) {
return false;
}
await reingestMutation.mutateAsync(eligibility.selectedIds);
return true;
}, [
eligibility.reingestDisabledReason,
eligibility.selectedIds,
reingestMutation,
]);
return {
selectedReports: eligibility.selectedReports,
selectedCount: eligibility.selectedCount,
snoozeDisabledReason: eligibility.snoozeDisabledReason,
suppressDisabledReason: eligibility.suppressDisabledReason,
deleteDisabledReason: eligibility.deleteDisabledReason,
reingestDisabledReason: eligibility.reingestDisabledReason,
isSuppressing: suppressMutation.isPending,
isSnoozing: snoozeMutation.isPending,
isDeleting: deleteMutation.isPending,
isReingesting: reingestMutation.isPending,
suppressSelected,
snoozeSelected,
deleteSelected,
reingestSelected,
};
}