Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep a dismissed action's answer off the next dialog

Yudhi Armyndharis005b3b56f180
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 334a463b4..da8f8e8c7 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -144,23 +144,21 @@ function installFetchStub(): void {
);
}
if (method === 'GET' && path === `${base}/contacts/blocked`) return Promise.resolve(jsonResponse(blockedIds));
- // Writes that answer an acknowledgement: group edits, chat state changes, clearing and deleting a chat,
- // and the contact writes (block, unblock, save).
- const pinRoute = method === 'POST' && path.endsWith('/chats/pin');
- if (pinRoute) return gated('pin', () => jsonResponse({ success: !declineWrites }));
- if (
- (method === 'POST' && /\/chats\/(archive|pin|mute)$/.test(path)) ||
- (/\/contacts\/[^/]+\/block$/.test(path) && method !== 'GET') ||
- (method === 'PUT' && /\/contacts\/[^/]+$/.test(path))
- ) {
- return Promise.resolve(jsonResponse({ success: !declineWrites }));
- }
- if (
- (method === 'POST' && (path === `${base}/chats/unread` || path === `${base}/chats/delete`)) ||
- (method === 'DELETE' && /\/chats\/[^/]+\/messages$/.test(path))
- ) {
- return Promise.resolve(jsonResponse({ success: !declineWrites }));
- }
+ // Writes that answer an acknowledgement: chat state changes, clearing and deleting a chat, leaving a group
+ // and the contact writes (block, unblock, save). A test can hold each back by its name.
+ const writes: Array<[name: string, matches: boolean]> = [
+ ['pin', method === 'POST' && path.endsWith('/chats/pin')],
+ ['archive', method === 'POST' && path.endsWith('/chats/archive')],
+ ['mute', method === 'POST' && path.endsWith('/chats/mute')],
+ ['unread', method === 'POST' && path === `${base}/chats/unread`],
+ ['deleteChat', method === 'POST' && path === `${base}/chats/delete`],
+ ['clearChat', method === 'DELETE' && /\/chats\/[^/]+\/messages$/.test(path)],
+ ['leave', method === 'POST' && path === `${groupPath}/leave`],
+ ['block', method !== 'GET' && /\/contacts\/[^/]+\/block$/.test(path)],
+ ['saveContact', method === 'PUT' && /\/contacts\/[^/]+$/.test(path)],
+ ];
+ const write = writes.find(([, matches]) => matches);
+ if (write) return gated(write[0], () => jsonResponse({ success: !declineWrites }));
if (path.startsWith(`${groupPath}/participants`)) {
const { participants } = body as { participants: string[] };
const refusal = refuseMember ? { status: 403, message: 'not-authorized' } : {};
@@ -673,6 +671,10 @@ const settle = async () => {
});
};
+/** A dialog's confirming button, which loses its label to a spinner while its request is out. */
+const confirmButton = (dialog: HTMLElement) =>
+ dialog.querySelector('.modal-footer button:last-child') as HTMLButtonElement;
+
test('a number lookup that answers after the panel closed and the session changed opens nothing', async () => {
const { screen, fireEvent, waitFor } = rtl;
listSecond = true;
@@ -752,6 +754,68 @@ test("a pin that answers after a switch does not pin the other session's chat wi
assert.ok(!screen.queryByRole('img', { name: 'Pinned' }), "B's row was pinned by A's late answer");
});
+test("a dismissed block's late answer leaves the next confirmation open and enabled", async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ listGroup = true;
+ const release = hold('block');
+ renderPage();
+ await screen.findByText('Team');
+ pickRowAction('Alice', 'Block contact');
+ fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Block contact' }));
+ await waitFor(() => assert.ok(fetchCalls.some(c => c.method === 'POST' && c.path.endsWith('/block'))));
+ fireEvent.keyDown(document, { key: 'Escape' });
+ await waitFor(() => assert.ok(!screen.queryByRole('dialog')));
+
+ pickRowAction('Team', 'Leave group');
+ const dialog = await screen.findByRole('dialog');
+ assert.equal(confirmButton(dialog).disabled, false, "the block's busy state showed on the next dialog");
+ release();
+ await settle();
+ assert.ok(screen.queryByRole('dialog'), "the block's answer closed the Leave dialog");
+ assert.ok(!fetchCalls.some(c => c.path.endsWith('/leave')), 'the Leave dialog sent a request nobody asked for');
+});
+
+test("a dismissed mute's late answer leaves the next mute dialog open and enabled", async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ listGroup = true;
+ const release = hold('mute');
+ renderPage();
+ await screen.findByText('Team');
+ pickRowAction('Alice', 'Mute notifications');
+ fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Mute notifications' }));
+ await waitFor(() => assert.ok(fetchCalls.some(c => c.method === 'POST' && c.path.endsWith('/chats/mute'))));
+ fireEvent.keyDown(document, { key: 'Escape' });
+ await waitFor(() => assert.ok(!screen.queryByRole('dialog')));
+
+ pickRowAction('Team', 'Mute notifications');
+ const dialog = await screen.findByRole('dialog');
+ assert.equal(confirmButton(dialog).disabled, false, "the first mute's busy state showed on the next dialog");
+ release();
+ await settle();
+ assert.ok(screen.queryByRole('dialog'), "the first mute's answer closed the next dialog");
+});
+
+test("a dismissed contact save's late answer leaves the next save dialog open and enabled", async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ listExtra = true;
+ const release = hold('saveContact');
+ renderPage();
+ await screen.findByText('Bobby');
+ pickRowAction('Alice', 'Rename contact');
+ fireEvent.click(within(await screen.findByRole('dialog')).getByRole('button', { name: 'Save' }));
+ await waitFor(() => assert.ok(fetchCalls.some(c => c.method === 'PUT' && c.path.includes('/contacts/'))));
+ fireEvent.keyDown(document, { key: 'Escape' });
+ await waitFor(() => assert.ok(!screen.queryByRole('dialog')));
+
+ pickRowAction('Bobby', 'Save contact');
+ const dialog = await screen.findByRole('dialog');
+ fireEvent.change(within(dialog).getByLabelText('First name'), { target: { value: 'Robert' } });
+ assert.equal(confirmButton(dialog).disabled, false, "the first save's busy state showed on the next dialog");
+ release();
+ await settle();
+ assert.ok(screen.queryByRole('dialog'), "the first save's answer closed the next dialog");
+});
+
test('a clear or delete the engine declines keeps the chat and its dialog, and says it failed', async () => {
const { screen, fireEvent, within } = rtl;
declineWrites = true;
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 47b0ac900..06ca0fbf3 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -197,17 +197,21 @@ export function Chats() {
const roomMenuRef = useRef<HTMLDivElement | null>(null);
// The group whose info dialog is open, from its row's menu or from the open room's.
const [groupDialogChat, setGroupDialogChat] = useState<Chat | null>(null);
- // A destructive row action awaiting confirmation, and whether it is in flight.
+ // A destructive row action awaiting confirmation, and the one whose request is out. A dialog is busy only
+ // while its own request is: the answer of a dialog dismissed meanwhile (Escape, the overlay) neither
+ // disables nor closes the next one, so these are compared by identity, never cleared unconditionally.
const [confirmAction, setConfirmAction] = useState<{
kind: 'leave' | 'clear' | 'delete' | 'block';
chat: Chat;
} | null>(null);
- const [confirmBusy, setConfirmBusy] = useState<boolean>(false);
- // The archive view of the Chats tab, and the chats whose mute or contact dialog is open.
+ const [confirmRunning, setConfirmRunning] = useState<typeof confirmAction>(null);
+ const confirmBusy = confirmAction !== null && confirmRunning === confirmAction;
+ // The archive view of the Chats tab, the chats whose mute or contact dialog is open, and the chat whose
+ // mute or contact request is out.
const [showArchived, setShowArchived] = useState<boolean>(false);
const [muteDialogChat, setMuteDialogChat] = useState<Chat | null>(null);
const [contactDialogChat, setContactDialogChat] = useState<Chat | null>(null);
- const [dialogBusy, setDialogBusy] = useState<boolean>(false);
+ const [dialogRunning, setDialogRunning] = useState<Chat | null>(null);
// Selected chat & message history
const [activeChat, setActiveChat] = useState<Chat | null>(null);
@@ -1267,7 +1271,7 @@ export function Chats() {
: duration === 'always'
? MUTE_FOREVER
: Date.now() + (duration === '8h' ? 8 * 3_600_000 : 7 * 86_400_000);
- setDialogBusy(true);
+ setDialogRunning(chat);
try {
expectSuccess(await sessionApi.muteChat(sessionId, chat.id, until));
// The list reports an indefinite mute as expiry 0.
@@ -1275,11 +1279,11 @@ export function Chats() {
muted: until !== null,
muteExpiration: until === null ? undefined : duration === 'always' ? 0 : until,
});
- if (stillOn(sessionId)) setMuteDialogChat(null);
+ setMuteDialogChat(open => (open === chat ? null : open));
} catch (err) {
failWith('chats.rowActions.muteFailed')(err);
} finally {
- setDialogBusy(false);
+ setDialogRunning(running => (running === chat ? null : running));
}
};
@@ -1287,17 +1291,17 @@ export function Chats() {
const sessionId = selectedSessionId;
const contactId = addressbookIdOf(chat);
if (!contactId) return;
- setDialogBusy(true);
+ setDialogRunning(chat);
try {
expectSuccess(await contactApi.upsert(sessionId, contactId, firstName, lastName || undefined));
showSuccessToast(t('chats.rowActions.contactSaved'));
void queryClient.invalidateQueries({ queryKey: ['contacts', sessionId] });
renameChat(sessionId, chat.id, [firstName, lastName].filter(Boolean).join(' '));
- if (stillOn(sessionId)) setContactDialogChat(null);
+ setContactDialogChat(open => (open === chat ? null : open));
} catch (err) {
failWith('chats.rowActions.contactSaveFailed')(err);
} finally {
- setDialogBusy(false);
+ setDialogRunning(running => (running === chat ? null : running));
}
};
@@ -1336,10 +1340,11 @@ export function Chats() {
// A switch closes the dialog, and a request that answers after it changes nothing on the new session.
const runConfirmedAction = async () => {
if (!confirmAction) return;
- const { kind, chat } = confirmAction;
+ const started = confirmAction;
+ const { kind, chat } = started;
const sessionId = selectedSessionId;
const name = chat.name || chat.id.split('@')[0];
- setConfirmBusy(true);
+ setConfirmRunning(started);
try {
if (kind === 'leave') {
expectSuccess(await sessionApi.leaveGroup(sessionId, chat.id));
@@ -1363,7 +1368,7 @@ export function Chats() {
}
if (!stillOn(sessionId)) return;
if ((kind === 'leave' || kind === 'delete') && activeChatIdRef.current === chat.id) setActiveChat(null);
- setConfirmAction(null);
+ setConfirmAction(open => (open === started ? null : open));
} catch (err) {
const failed =
kind === 'leave'
@@ -1375,7 +1380,7 @@ export function Chats() {
: 'chats.rowActions.deleteFailed';
failWith(failed)(err);
} finally {
- setConfirmBusy(false);
+ setConfirmRunning(running => (running === started ? null : running));
}
};
@@ -1828,7 +1833,7 @@ export function Chats() {
{muteDialogChat && (
<MuteChatDialog
chat={muteDialogChat}
- busy={dialogBusy}
+ busy={dialogRunning === muteDialogChat}
onMute={duration => void handleMute(muteDialogChat, duration)}
onClose={() => setMuteDialogChat(null)}
/>
@@ -1837,7 +1842,7 @@ export function Chats() {
{contactDialogChat && (
<SaveContactDialog
chat={contactDialogChat}
- busy={dialogBusy}
+ busy={dialogRunning === contactDialogChat}
onSave={(first, last) => void handleSaveContact(contactDialogChat, first, last)}
onClose={() => setContactDialogChat(null)}
/>