Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): key webhook filter rows by condition, not index

Yudhi Armyndharis9ad2bd1217c3
diff --git a/dashboard/src/components/FilterBuilder.test.ts b/dashboard/src/components/FilterBuilder.test.ts
index d74e91af0..9fbbb5b0e 100644
--- a/dashboard/src/components/FilterBuilder.test.ts
+++ b/dashboard/src/components/FilterBuilder.test.ts
@@ -3,7 +3,8 @@
import '../test-helpers/register-hooks.ts';
import { test, before, after } from 'node:test';
import assert from 'node:assert/strict';
-import { createElement } from 'react';
+import { createElement, useState } from 'react';
+import type { WebhookFilters } from '../services/api.ts';
let rtl: typeof import('@testing-library/react');
let FilterBuilder: (typeof import('./FilterBuilder.tsx'))['FilterBuilder'];
@@ -69,3 +70,33 @@ test('the body text stops at the gateway limit of 1000 characters', () => {
);
assert.equal(container.querySelector<HTMLInputElement>('.filter-text input[type="text"]')?.maxLength, 1000);
});
+
+test('removing a row does not hand its unsent chip text to the row below', () => {
+ let latest: WebhookFilters | null = null;
+ function Harness() {
+ const [filters, setFilters] = useState<WebhookFilters | null>({
+ conditions: [
+ { field: 'sender', operator: 'is', value: ['[email protected]'] },
+ { field: 'chatId', operator: 'is', value: ['[email protected]'] },
+ ],
+ });
+ latest = filters;
+ return createElement(FilterBuilder, { filters, onChange: setFilters, chats: [] });
+ }
+ const { container } = rtl.render(createElement(Harness));
+
+ const [first] = container.querySelectorAll<HTMLInputElement>('.chips-text');
+ rtl.fireEvent.change(first, { target: { value: '62812' } });
+ rtl.fireEvent.click(container.querySelector('.filter-remove')!);
+
+ const [left] = container.querySelectorAll<HTMLInputElement>('.chips-text');
+ assert.equal(left.value, '');
+ rtl.fireEvent.keyDown(left, { key: 'Enter' });
+ assert.deepEqual(latest, { conditions: [{ field: 'chatId', operator: 'is', value: ['[email protected]'] }] });
+
+ // Editing a row keeps its key, so the input is not remounted (and keeps focus) on every chip added.
+ rtl.fireEvent.change(left, { target: { value: '62813' } });
+ rtl.fireEvent.keyDown(left, { key: 'Enter' });
+ assert.deepEqual(latest, { conditions: [{ field: 'chatId', operator: 'is', value: ['[email protected]', '[email protected]'] }] });
+ assert.equal(container.querySelector('.chips-text'), left);
+});
diff --git a/dashboard/src/components/FilterBuilder.tsx b/dashboard/src/components/FilterBuilder.tsx
index 68b080319..88bcb3735 100644
--- a/dashboard/src/components/FilterBuilder.tsx
+++ b/dashboard/src/components/FilterBuilder.tsx
@@ -39,6 +39,16 @@ const MESSAGE_FIELDS: FieldDescriptor[] = [
const descriptorFor = (field: string): FieldDescriptor =>
MESSAGE_FIELDS.find(f => f.field === field) ?? MESSAGE_FIELDS[0];
+// Rows are keyed by condition, not by index: with an index key, removing a row hands its unsent chip text
+// to the row that moves into its place. An edited condition is a new object, so it inherits the old key.
+const rowKeys = new WeakMap<WebhookFilterCondition, number>();
+let nextRowKey = 0;
+function rowKeyFor(condition: WebhookFilterCondition): number {
+ let key = rowKeys.get(condition);
+ if (key === undefined) rowKeys.set(condition, (key = nextRowKey++));
+ return key;
+}
+
function defaultValueFor(kind: FieldKind): WebhookFilterCondition['value'] {
if (kind === 'boolean') return true;
if (kind === 'text') return '';
@@ -159,7 +169,14 @@ export function FilterBuilder({ filters, onChange, chats }: FilterBuilderProps)
const emit = (next: WebhookFilterCondition[]) => onChange(next.length ? { conditions: next } : null);
const updateAt = (index: number, patch: Partial<WebhookFilterCondition>) =>
- emit(conditions.map((c, i) => (i === index ? { ...c, ...patch } : c)));
+ emit(
+ conditions.map((c, i) => {
+ if (i !== index) return c;
+ const next = { ...c, ...patch };
+ rowKeys.set(next, rowKeyFor(c));
+ return next;
+ }),
+ );
const addCondition = () => {
const def = MESSAGE_FIELDS[0];
@@ -181,7 +198,7 @@ export function FilterBuilder({ filters, onChange, chats }: FilterBuilderProps)
{conditions.map((condition, index) => {
const def = descriptorFor(condition.field);
return (
- <div key={index} className="filter-row">
+ <div key={rowKeyFor(condition)} className="filter-row">
<select
className="filter-field"
aria-label={t('webhooks.filters.fieldLabel')}