Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions apps/mobile/src/components/ui/Dialog.android.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,61 @@ it("ignores a late action from a dialog that a newer one replaced", () => {
expect(olderAction).toHaveBeenCalledOnce();
expect(container.textContent).toContain("Newer");
});

it("cancels a confirm that a newer dialog replaced", async () => {
mountHost();
const onCancel = vi.fn();
const onConfirm = vi.fn();
act(() =>
Dialog.confirm({
title: "Close open pull requests?",
confirmText: "Close & Settle",
cancelText: "Settle",
onConfirm,
onCancel,
}),
);
act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));

await act(async () => {
await Promise.resolve();
});

expect(onCancel).toHaveBeenCalledOnce();
expect(onConfirm).not.toHaveBeenCalled();
});

it("ignores stale confirm and cancel before and after replacement cancellation", async () => {
mountHost();
const onCancel = vi.fn();
const onConfirm = vi.fn();
act(() =>
Dialog.confirm({
title: "Remove project",
confirmText: "Remove project",
cancelText: "Cancel",
onConfirm,
onCancel,
}),
);
const staleCancel = renderedClicks.at(-2)!;
const staleConfirm = renderedClicks.at(-1)!;

act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
act(() => {
staleConfirm();
staleCancel();
});

await act(async () => {
await Promise.resolve();
});

act(() => {
staleConfirm();
staleCancel();
});

expect(onCancel).toHaveBeenCalledOnce();
expect(onConfirm).not.toHaveBeenCalled();
});
80 changes: 51 additions & 29 deletions apps/mobile/src/components/ui/Dialog.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,42 @@ type DialogRequest =
| { readonly kind: "text-input"; readonly request: TextInputDialogRequest };

type PresentedDialog = DialogRequest & { readonly id: number };
type PresentationStatus = "active" | "replaced" | "completed";

// App services raise dialogs before the root mounts, so the request lives in
// the module and the Host renders whatever is current. One dialog shows at a
// time: a new request replaces the presented one.
let presented: PresentedDialog | null = null;
let nextPresentationId = 0;
const presentationStatus = new Map<number, PresentationStatus>();
const listeners = new Set<() => void>();

function setPresented(next: PresentedDialog | null) {
presented = next;
for (const listener of listeners) listener();
}

function statusOf(id: number): PresentationStatus | undefined {
return presentationStatus.get(id);
}

function cancelPresented(dialog: PresentedDialog) {
if (statusOf(dialog.id) === "completed") return;
presentationStatus.set(dialog.id, "completed");
dismiss(dialog.id);
if (dialog.kind === "alert") dialog.request.options?.onDismiss?.();
else dialog.request.onCancel?.();
}

function submitRequest(dialog: DialogRequest) {
const replaced = presented;
setPresented({ ...dialog, id: ++nextPresentationId });
// A replaced alert closed without an answer, which its owner hears as a dismissal.
if (replaced?.kind === "alert") replaced.request.options?.onDismiss?.();
const previous = presented;
if (previous) presentationStatus.set(previous.id, "replaced");
const id = ++nextPresentationId;
presentationStatus.set(id, "active");
setPresented({ ...dialog, id });
// A newer dialog replaces the current one without a button press; cancel the
// previous so awaiters (settle confirms, busy flags) do not hang.
if (previous !== null) cancelPresented(previous);
}

/** Closes the dialog only if it is still the one the action came from. */
Expand Down Expand Up @@ -90,25 +108,32 @@ export function prompt(request: TextInputDialogRequest): void {
submitRequest({ kind: "text-input", request });
}

export function Host() {
const presented = useSyncExternalStore(subscribe, getPresented, getPresented);
function handleCancelFor(dialog: PresentedDialog) {
if (statusOf(dialog.id) !== "active") return;
cancelPresented(dialog);
}

const handleCancel = () => {
if (!presented) return;
dismiss(presented.id);
if (presented.kind === "alert") presented.request.options?.onDismiss?.();
else presented.request.onCancel?.();
};
function handleConfirmFor(dialog: PresentedDialog, nativeInputValue?: string) {
if (statusOf(dialog.id) !== "active") return;
presentationStatus.set(dialog.id, "completed");
dismiss(dialog.id);
if (dialog.kind === "confirm") {
dialog.request.onConfirm();
} else if (dialog.kind === "text-input") {
dialog.request.onConfirm(nativeInputValue ?? dialog.request.initialValue);
}
}

const handleConfirm = (nativeInputValue?: string) => {
if (!presented) return;
dismiss(presented.id);
if (presented.kind === "confirm") {
presented.request.onConfirm();
} else if (presented.kind === "text-input") {
presented.request.onConfirm(nativeInputValue ?? presented.request.initialValue);
}
};
function handleAlertActionFor(dialog: PresentedDialog, action: AlertButton) {
// Stale native alert presses still run their action; only the active
// presentation can clear the store entry.
if (statusOf(dialog.id) === "active") presentationStatus.set(dialog.id, "completed");
dismiss(dialog.id);
action.onPress?.();
}

export function Host() {
const presented = useSyncExternalStore(subscribe, getPresented, getPresented);

if (presented?.kind === "alert") {
return (
Expand All @@ -121,12 +146,9 @@ export function Host() {
}}
alertActions={presented.request.buttons ?? [{ text: "OK" }]}
dismissable={presented.request.options?.cancelable ?? false}
onAlertAction={(action) => {
dismiss(presented.id);
action.onPress?.();
}}
onCancel={handleCancel}
onConfirm={handleConfirm}
onAlertAction={(action) => handleAlertActionFor(presented, action)}
onCancel={() => handleCancelFor(presented)}
onConfirm={(value) => handleConfirmFor(presented, value)}
/>
);
}
Expand All @@ -139,8 +161,8 @@ export function Host() {
presented.kind === "text-input" ? presented.request.initialValue : undefined
}
inputPlaceholder={presented.kind === "text-input" ? presented.request.placeholder : undefined}
onCancel={handleCancel}
onConfirm={handleConfirm}
onCancel={() => handleCancelFor(presented)}
onConfirm={(value) => handleConfirmFor(presented, value)}
/>
) : null;
}
Expand Down
25 changes: 21 additions & 4 deletions apps/mobile/src/components/ui/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,45 @@ import type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.type

export type { ConfirmDialogRequest, TextInputDialogRequest } from "./Dialog.types";

function beginTrackedDialog(onCancel: (() => void) | undefined) {
let finished = false;
const finish = (action?: () => void) => {
if (finished) return;
finished = true;
action?.();
};
const cancel = () => finish(onCancel);
const complete = (action: () => void) => finish(action);
return { cancel, complete };
}

export function alert(...args: Parameters<typeof Alert.alert>): void {
Alert.alert(...args);
}

export function confirm(request: ConfirmDialogRequest): void {
const { cancel, complete } = beginTrackedDialog(request.onCancel);
Alert.alert(request.title, request.message, [
{ text: request.cancelText ?? "Cancel", style: "cancel", onPress: request.onCancel },
{ text: request.cancelText ?? "Cancel", style: "cancel", onPress: cancel },
{
text: request.confirmText,
style: request.destructive ? "destructive" : "default",
onPress: request.onConfirm,
onPress: () => complete(request.onConfirm),
},
]);
}

export function prompt(request: TextInputDialogRequest): void {
const { cancel, complete } = beginTrackedDialog(request.onCancel);
Alert.prompt(
request.title,
request.message,
[
{ text: request.cancelText ?? "Cancel", style: "cancel", onPress: request.onCancel },
{ text: request.confirmText, onPress: (value) => request.onConfirm(value ?? "") },
{ text: request.cancelText ?? "Cancel", style: "cancel", onPress: cancel },
{
text: request.confirmText,
onPress: (value) => complete(() => request.onConfirm(value ?? "")),
},
],
"plain-text",
request.initialValue,
Expand Down
Loading
Loading