Skip to content

Commit a44ad1d

Browse files
fix(mobile): a replaced approval dialog no longer stalls the prompt queue
The Android dialog host now calls onDismiss on an alert that a newer one replaces. Approval prompts and their follow-up alerts treat that as "later": the request goes back on the queue and shows again on the next offer instead of leaving the queue stuck. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a50a789 commit a44ad1d

5 files changed

Lines changed: 55 additions & 16 deletions

File tree

‎apps/mobile/src/components/ui/Dialog.android.test.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ it("keeps a dialog raised while the activity is replaced", () => {
9090
expect(current.container.textContent).toContain("Shared content imported");
9191
});
9292

93+
it("tells a replaced alert it was dismissed", () => {
94+
mountHost();
95+
const onDismiss = vi.fn();
96+
act(() => Dialog.alert("Older", undefined, [{ text: "OK" }], { onDismiss }));
97+
act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
98+
99+
expect(onDismiss).toHaveBeenCalledOnce();
100+
});
101+
93102
it("ignores a late action from a dialog that a newer one replaced", () => {
94103
const { container } = mountHost();
95104
const olderAction = vi.fn();

‎apps/mobile/src/components/ui/Dialog.android.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ function setPresented(next: PresentedDialog | null) {
4949
}
5050

5151
function submitRequest(dialog: DialogRequest) {
52+
const replaced = presented;
5253
setPresented({ ...dialog, id: ++nextPresentationId });
54+
// A replaced alert closed without an answer, which its owner hears as a dismissal.
55+
if (replaced?.kind === "alert") replaced.request.options?.onDismiss?.();
5356
}
5457

5558
/** Closes the dialog only if it is still the one the action came from. */

‎apps/mobile/src/features/connection/ConnectDeviceApprovalCoordinator.tsx‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,22 @@ const approvalPrompts = createApprovalPromptQueue<QueuedApprovalPrompt>((prompt,
5757
return answer("done");
5858
}
5959
if (outcome === "gone") {
60-
return Dialog.alert("Request no longer waiting", "It expired or was answered elsewhere.", [
61-
{ text: "OK", onPress: () => answer("done") },
62-
]);
60+
return Dialog.alert(
61+
"Request no longer waiting",
62+
"It expired or was answered elsewhere.",
63+
[{ text: "OK", onPress: () => answer("done") }],
64+
{ onDismiss: () => answer("done") },
65+
);
6366
}
64-
Dialog.alert(failure, "The request is still waiting. Try again.", [
65-
{
66-
text: "OK",
67-
onPress: () => answer(watchedEnvironments.has(prompt.environmentId) ? "retry" : "done"),
68-
},
69-
]);
67+
const retryIfWatched = () =>
68+
answer(watchedEnvironments.has(prompt.environmentId) ? "retry" : "done");
69+
Dialog.alert(
70+
failure,
71+
"The request is still waiting. Try again.",
72+
[{ text: "OK", onPress: retryIfWatched }],
73+
// Another dialog took the screen; offer the request again on the next update.
74+
{ onDismiss: () => answer("later") },
75+
);
7076
});
7177
Dialog.alert(
7278
prompt.title,
@@ -75,7 +81,8 @@ const approvalPrompts = createApprovalPromptQueue<QueuedApprovalPrompt>((prompt,
7581
{ text: "Deny", onPress: respond(prompt.deny, "Could not deny the device") },
7682
{ text: "Approve", onPress: respond(prompt.approve, "Could not approve the device") },
7783
],
78-
{ cancelable: false },
84+
// Another dialog took the screen; offer the request again on the next update.
85+
{ cancelable: false, onDismiss: () => answer("later") },
7986
);
8087
});
8188

‎apps/mobile/src/features/connection/connectDeviceApproval.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("connectDeviceApprovalPrompts", () => {
4040
describe("createApprovalPromptQueue", () => {
4141
it("shows one prompt at a time and offers a failed one again before the rest", () => {
4242
const shown: string[] = [];
43-
const answers: Array<(outcome: "done" | "retry") => void> = [];
43+
const answers: Array<(outcome: "done" | "retry" | "later") => void> = [];
4444
const queue = createApprovalPromptQueue<{ pairingLinkId: string }>((prompt, answer) => {
4545
shown.push(prompt.pairingLinkId);
4646
answers.push(answer);
@@ -56,9 +56,24 @@ describe("createApprovalPromptQueue", () => {
5656
expect(shown).toEqual(["a", "a", "b"]);
5757
});
5858

59+
it("waits for the next offer when another dialog replaced the prompt", () => {
60+
const shown: string[] = [];
61+
const answers: Array<(outcome: "done" | "retry" | "later") => void> = [];
62+
const queue = createApprovalPromptQueue<{ pairingLinkId: string }>((prompt, answer) => {
63+
shown.push(prompt.pairingLinkId);
64+
answers.push(answer);
65+
});
66+
67+
queue.offer([{ pairingLinkId: "a" }]);
68+
answers.at(-1)!("later");
69+
expect(shown).toEqual(["a"]);
70+
queue.offer([]);
71+
expect(shown).toEqual(["a", "a"]);
72+
});
73+
5974
it("drops a host's waiting prompts so they cannot hold up other hosts", () => {
6075
const shown: string[] = [];
61-
const answers: Array<(outcome: "done" | "retry") => void> = [];
76+
const answers: Array<(outcome: "done" | "retry" | "later") => void> = [];
6277
const queue = createApprovalPromptQueue<{ pairingLinkId: string; host: string }>(
6378
(prompt, answer) => {
6479
shown.push(prompt.pairingLinkId);

‎apps/mobile/src/features/connection/connectDeviceApproval.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ export function connectDeviceApprovalPrompts(input: {
3838

3939
/**
4040
* Shows approval prompts one at a time: an Android alert replaces the one on screen, which
41-
* would drop an unanswered request. A prompt answered with "retry" goes back to the front.
41+
* would drop an unanswered request. A prompt answered with "retry" goes back to the front;
42+
* "later" puts it back without showing anything until the next offer.
4243
*/
4344
export function createApprovalPromptQueue<P extends { readonly pairingLinkId: string }>(
44-
present: (prompt: P, answer: (outcome: "done" | "retry") => void) => void,
45+
present: (prompt: P, answer: (outcome: "done" | "retry" | "later") => void) => void,
4546
) {
4647
const known = new Set<string>();
4748
const waiting: P[] = [];
@@ -51,10 +52,13 @@ export function createApprovalPromptQueue<P extends { readonly pairingLinkId: st
5152
const prompt = waiting.shift();
5253
if (prompt === undefined) return;
5354
showing = true;
55+
let answered = false;
5456
present(prompt, (outcome) => {
57+
if (answered) return;
58+
answered = true;
5559
showing = false;
56-
if (outcome === "retry") waiting.unshift(prompt);
57-
next();
60+
if (outcome !== "done") waiting.unshift(prompt);
61+
if (outcome !== "later") next();
5862
});
5963
};
6064
return {
@@ -69,6 +73,7 @@ export function createApprovalPromptQueue<P extends { readonly pairingLinkId: st
6973
}
7074
},
7175
offer(prompts: ReadonlyArray<P>) {
76+
// Also re-shows a prompt deferred because another dialog took the screen.
7277
for (const prompt of prompts) {
7378
if (known.has(prompt.pairingLinkId)) continue;
7479
known.add(prompt.pairingLinkId);

0 commit comments

Comments
 (0)