Skip to content

Commit bd6d3b4

Browse files
fix(web,mobile): webhook filter rows keep their identity when one is removed
Rows were keyed by index, so removing the first of two filters handed the focused remove button to the next row. Draft rows now carry a client-only id, keep it through edits and reopen, and are removed by id. Layout classes move from the inputs and select trigger to wrapper elements, with an identical rendered layout. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f14571a commit bd6d3b4

7 files changed

Lines changed: 77 additions & 50 deletions

File tree

‎apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import {
2525
} from "@t3tools/client-runtime/state/runtime";
2626
import {
2727
DEFAULT_WEBHOOK_PROMPT,
28-
EMPTY_WEBHOOK_FILTER_DRAFT,
28+
emptyWebhookFilterDraft,
2929
parseMaxDeliveryAge,
3030
parseWebhookFilterDrafts,
31+
removeWebhookFilterDraft,
3132
WEBHOOK_FILTER_OPS,
3233
type WebhookFilterDraft,
3334
} from "@t3tools/client-runtime/scheduled-task-webhook";
@@ -239,17 +240,15 @@ function WebhookFilterRows(props: {
239240
return (
240241
<>
241242
{props.filters.map((filter, index) => (
242-
// Rows have no identity of their own; removing one re-renders the rest in order.
243-
// oxlint-disable-next-line react/no-array-index-key
244-
<View key={index}>
243+
<View key={filter.id}>
245244
<View className="flex-row items-center gap-3 border-t border-border-subtle px-4 pt-3">
246245
<Text className="min-w-0 flex-1 text-sm text-foreground-muted">Filter {index + 1}</Text>
247246
<Pressable
248247
accessibilityRole="button"
249248
accessibilityLabel={`Remove filter ${index + 1}`}
250249
disabled={props.disabled}
251250
hitSlop={8}
252-
onPress={() => props.onChange(props.filters.filter((_, at) => at !== index))}
251+
onPress={() => props.onChange(removeWebhookFilterDraft(props.filters, filter.id))}
253252
className="active:opacity-70"
254253
>
255254
<SymbolView name="trash" size={16} tintColorClassName="accent-danger-foreground" />
@@ -290,7 +289,7 @@ function WebhookFilterRows(props: {
290289
<Pressable
291290
accessibilityRole="button"
292291
disabled={props.disabled}
293-
onPress={() => props.onChange([...props.filters, EMPTY_WEBHOOK_FILTER_DRAFT])}
292+
onPress={() => props.onChange([...props.filters, emptyWebhookFilterDraft()])}
294293
className="min-h-14 flex-row items-center gap-3 border-t border-border-subtle px-4 py-3 active:opacity-70"
295294
>
296295
<Text className="min-w-0 flex-1 text-lg text-foreground">Add filter</Text>

‎apps/mobile/src/features/settings/scheduledTaskDraft.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe("scheduleDraftForTask", () => {
7979
filters: filters.slice(1),
8080
});
8181
expect(
82-
scheduleFromDraft({ ...draft, filters: [{ path: "", op: "exists", value: "" }] }),
82+
scheduleFromDraft({ ...draft, filters: [{ id: "f1", path: "", op: "exists", value: "" }] }),
8383
).toBeNull();
8484
});
8585
});

‎apps/mobile/src/features/settings/scheduledTaskDraft.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function draftSignature(draft: ScheduledTaskDraft): string {
179179
[...draft.schedule.weekdays].sort((a, b) => a - b),
180180
draft.schedule.intervalMinutes,
181181
draft.schedule.maxDeliveryAgeMinutes,
182-
draft.schedule.filters,
182+
draft.schedule.filters.map(({ path, op, value }) => [path, op, value]),
183183
draft.workspace,
184184
draft.baseRef,
185185
draft.checkoutPath,

‎apps/web/src/components/settings/ScheduledTasksSettings.tsx‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import type {
2828
} from "@t3tools/contracts";
2929
import {
3030
DEFAULT_WEBHOOK_PROMPT,
31-
EMPTY_WEBHOOK_FILTER_DRAFT,
31+
emptyWebhookFilterDraft,
3232
parseWebhookFilterDrafts,
33+
removeWebhookFilterDraft,
3334
WEBHOOK_FILTER_OPS,
3435
type WebhookFilterDraft,
3536
} from "@t3tools/client-runtime/scheduled-task-webhook";
@@ -179,31 +180,31 @@ function WebhookFiltersField({
179180
variant="outline"
180181
size="sm"
181182
disabled={filters.length >= MAX_WEBHOOK_FILTERS}
182-
onClick={() => onChange([...filters, EMPTY_WEBHOOK_FILTER_DRAFT])}
183+
onClick={() => onChange([...filters, emptyWebhookFilterDraft()])}
183184
>
184185
<PlusIcon />
185186
Add filter
186187
</Button>
187188
</div>
188189
{filters.map((filter, index) => (
189190
// Always stacked: the dialog stays narrow on wide windows, so a single line
190-
// would cut off typical paths and values.
191-
// Rows have no identity of their own; removing one re-renders the rest in order.
192-
// oxlint-disable-next-line react/no-array-index-key
193-
<div key={index} className="space-y-2 pt-1">
191+
// would cut off typical paths and values. The grid wrappers size each control
192+
// without restyling it; a grid item stretches to its minmax(0, 1fr) track.
193+
<div key={filter.id} className="space-y-2 pt-1">
194194
<div className="flex items-center gap-2">
195-
<Input
196-
aria-label={`Filter ${index + 1} path`}
197-
className="min-w-0 flex-1"
198-
placeholder="body.action"
199-
value={filter.path}
200-
onChange={(event) => update(index, { path: event.target.value })}
201-
/>
195+
<div className="grid min-w-0 flex-1 grid-cols-1">
196+
<Input
197+
aria-label={`Filter ${index + 1} path`}
198+
placeholder="body.action"
199+
value={filter.path}
200+
onChange={(event) => update(index, { path: event.target.value })}
201+
/>
202+
</div>
202203
<Button
203204
variant="ghost"
204205
size="icon-sm"
205206
aria-label={`Remove filter ${index + 1}`}
206-
onClick={() => onChange(filters.filter((_, at) => at !== index))}
207+
onClick={() => onChange(removeWebhookFilterDraft(filters, filter.id))}
207208
>
208209
<Trash2Icon />
209210
</Button>
@@ -216,15 +217,13 @@ function WebhookFiltersField({
216217
if (known) update(index, { op: known.op });
217218
}}
218219
>
219-
<SelectTrigger
220-
size="sm"
221-
className="w-44 shrink-0"
222-
aria-label={`Filter ${index + 1} test`}
223-
>
224-
<SelectValue>
225-
{WEBHOOK_FILTER_OPS.find((option) => option.op === filter.op)?.label}
226-
</SelectValue>
227-
</SelectTrigger>
220+
<div className="grid w-44 shrink-0 grid-cols-1">
221+
<SelectTrigger size="sm" aria-label={`Filter ${index + 1} test`}>
222+
<SelectValue>
223+
{WEBHOOK_FILTER_OPS.find((option) => option.op === filter.op)?.label}
224+
</SelectValue>
225+
</SelectTrigger>
226+
</div>
228227
<SelectPopup>
229228
{WEBHOOK_FILTER_OPS.map((option) => (
230229
<SelectItem key={option.op} value={option.op}>
@@ -234,13 +233,14 @@ function WebhookFiltersField({
234233
</SelectPopup>
235234
</Select>
236235
{filter.op === "exists" ? null : (
237-
<Input
238-
aria-label={`Filter ${index + 1} value`}
239-
className="min-w-0 flex-1"
240-
placeholder={filter.op === "in" ? "opened, reopened" : "opened"}
241-
value={filter.value}
242-
onChange={(event) => update(index, { value: event.target.value })}
243-
/>
236+
<div className="grid min-w-0 flex-1 grid-cols-1">
237+
<Input
238+
aria-label={`Filter ${index + 1} value`}
239+
placeholder={filter.op === "in" ? "opened, reopened" : "opened"}
240+
value={filter.value}
241+
onChange={(event) => update(index, { value: event.target.value })}
242+
/>
243+
</div>
244244
)}
245245
</div>
246246
</div>

‎apps/web/src/components/settings/scheduledTasksSettings.logic.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ describe("webhook scheduled tasks", () => {
237237
// Removing every row is a change, so it clears the stored filters.
238238
expect(scheduleFromDraft({ ...draft, filters: [] })).toMatchObject({ filters: [] });
239239
expect(
240-
scheduleFromDraft({ ...draft, filters: [{ path: "body.action", op: "in", value: " , " }] }),
240+
scheduleFromDraft({
241+
...draft,
242+
filters: [{ id: "f1", path: "body.action", op: "in", value: " , " }],
243+
}),
241244
).toBeNull();
242245
});
243246
});

‎packages/client-runtime/src/scheduledTaskWebhook.test.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import {
44
parseMaxDeliveryAge,
5+
emptyWebhookFilterDraft,
56
parseWebhookFilterDrafts,
7+
removeWebhookFilterDraft,
68
webhookFilterDrafts,
79
webhookFilterDraftsChanged,
810
} from "./scheduledTaskWebhook.ts";
@@ -26,24 +28,25 @@ describe("webhook filter drafts", () => {
2628
{ path: "query.source", op: "exists" },
2729
] as const;
2830
const drafts = webhookFilterDrafts(filters);
29-
expect(drafts[1]).toEqual({ path: "body.action", op: "in", value: "opened, reopened" });
31+
expect(drafts[1]).toMatchObject({ path: "body.action", op: "in", value: "opened, reopened" });
3032
expect(parseWebhookFilterDrafts(drafts)).toEqual({ filters });
3133
});
3234

3335
it("names the first row the server would refuse", () => {
3436
expect(
3537
parseWebhookFilterDrafts([
36-
{ path: "body.action", op: "equals", value: "opened" },
37-
{ path: "body.action", op: "in", value: " , " },
38-
{ path: "", op: "exists", value: "" },
38+
{ id: "f1", path: "body.action", op: "equals", value: "opened" },
39+
{ id: "f2", path: "body.action", op: "in", value: " , " },
40+
{ id: "f3", path: "", op: "exists", value: "" },
3941
]),
4042
).toEqual({
4143
error: "Filter 2 needs a path under body, headers or query, and at least one value.",
4244
});
43-
expect(parseWebhookFilterDrafts([{ path: " ", op: "exists", value: "" }])).toEqual({
45+
expect(parseWebhookFilterDrafts([{ id: "f1", path: " ", op: "exists", value: "" }])).toEqual({
4446
error: "Filter 1 needs a path under body, headers or query.",
4547
});
46-
const seventeen = Array.from({ length: 17 }, () => ({
48+
const seventeen = Array.from({ length: 17 }, (_, index) => ({
49+
id: `f${index}`,
4750
path: "body.action",
4851
op: "exists" as const,
4952
value: "",
@@ -58,4 +61,14 @@ describe("webhook filter drafts", () => {
5861
expect(webhookFilterDraftsChanged(saved, [{ ...saved[0]!, value: "closed" }])).toBe(true);
5962
expect(webhookFilterDraftsChanged([], [])).toBe(false);
6063
});
64+
65+
it("removing the first row keeps the second row's id", () => {
66+
const drafts = [
67+
...webhookFilterDrafts([{ path: "body.action", op: "equals", value: "opened" }]),
68+
emptyWebhookFilterDraft(),
69+
];
70+
expect(new Set(drafts.map((draft) => draft.id)).size).toBe(2);
71+
const remaining = removeWebhookFilterDraft(drafts, drafts[0]!.id);
72+
expect(remaining.map((draft) => draft.id)).toEqual([drafts[1]!.id]);
73+
});
6174
});

‎packages/client-runtime/src/scheduledTaskWebhook.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export function parseMaxDeliveryAge(value: string): number | null | undefined {
2323
* lossless because list values can't contain commas; `exists` ignores the value.
2424
*/
2525
export interface WebhookFilterDraft {
26+
/** Stable row identity for the form; never sent to the server. */
27+
readonly id: string;
2628
readonly path: string;
2729
readonly op: ScheduledTaskWebhookFilterOp;
2830
readonly value: string;
@@ -39,16 +41,26 @@ export const WEBHOOK_FILTER_OPS: ReadonlyArray<{
3941
{ op: "exists", label: "exists" },
4042
];
4143

42-
export const EMPTY_WEBHOOK_FILTER_DRAFT: WebhookFilterDraft = {
43-
path: "",
44-
op: "equals",
45-
value: "",
46-
};
44+
let lastWebhookFilterDraftId = 0;
45+
const nextWebhookFilterDraftId = () => `filter-${++lastWebhookFilterDraftId}`;
46+
47+
export function emptyWebhookFilterDraft(): WebhookFilterDraft {
48+
return { id: nextWebhookFilterDraftId(), path: "", op: "equals", value: "" };
49+
}
50+
51+
/** By id, so a removed row's identity never passes to the row after it. */
52+
export function removeWebhookFilterDraft(
53+
drafts: ReadonlyArray<WebhookFilterDraft>,
54+
id: string,
55+
): ReadonlyArray<WebhookFilterDraft> {
56+
return drafts.filter((draft) => draft.id !== id);
57+
}
4758

4859
export function webhookFilterDrafts(
4960
filters: ReadonlyArray<ScheduledTaskWebhookFilter> | undefined,
5061
): ReadonlyArray<WebhookFilterDraft> {
5162
return (filters ?? []).map((filter) => ({
63+
id: nextWebhookFilterDraftId(),
5264
path: filter.path,
5365
op: filter.op,
5466
value:

0 commit comments

Comments
 (0)