TypeScript
fix(mobile): Android dialogs keep working after the activity is replaced (#22128)
Alex100d146c6c34
diff --git a/apps/mobile/src/components/ui/Dialog.android.test.tsx b/apps/mobile/src/components/ui/Dialog.android.test.tsxnew file mode 100644index 00000000000..1d67caa7d86--- /dev/null+++ b/apps/mobile/src/components/ui/Dialog.android.test.tsx@@ -0,0 +1,67 @@+import { act, type ReactNode } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, expect, it, vi } from "vite-plus/test";++vi.mock("@expo/ui/jetpack-compose", () => {+ const Slot = (props: { readonly children?: ReactNode }) => <div>{props.children}</div>;+ const Button = (props: { readonly children?: ReactNode; readonly onClick?: () => void }) => (+ <button onClick={props.onClick}>{props.children}</button>+ );+ return {+ AlertDialog: Object.assign(Slot, {+ Title: Slot,+ Text: Slot,+ DismissButton: Slot,+ ConfirmButton: Slot,+ }),+ Button,+ OutlinedButton: Button,+ Column: Slot,+ FlowRow: Slot,+ Icon: () => null,+ OutlinedTextField: Object.assign(Slot, { Label: Slot, Placeholder: Slot }),+ Text: Slot,+ useNativeState: (value: unknown) => ({ get: () => value }),+ };+});+vi.mock("./NativeThemeHost.android", () => ({+ NativeThemeHost: (props: { readonly children?: ReactNode }) => <div>{props.children}</div>,+}));+vi.mock("../../features/settings/appearance/AppearancePreferencesProvider", () => ({+ useAppearancePreferences: () => ({ themeVariables: {} }),+}));+vi.mock("../../features/settings/appearance/useScaledTextRole", () => ({+ useScaledTextRole: () => ({}),+}));+vi.mock("../../lib/mobileTheme", () => ({ themeColorWithContrast: () => "#ffffff" }));++import * as Dialog from "./Dialog.android";++const roots: Root[] = [];++function mountHost() {+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ const container = document.createElement("div");+ document.body.append(container);+ const root = createRoot(container);+ roots.push(root);+ act(() => root.render(<Dialog.Host />));+ return { root, container };+}++afterEach(() => {+ for (const root of roots.splice(0)) act(() => root.unmount());+ document.body.innerHTML = "";+});++it("keeps presenting dialogs after an older root's Host unmounts", () => {+ // Android activity replacement mounts the new root before the old one stops.+ const previous = mountHost();+ const current = mountHost();+ roots.splice(roots.indexOf(previous.root), 1);+ act(() => previous.root.unmount());++ act(() => Dialog.alert("Saved", undefined, [{ text: "OK" }]));++ expect(current.container.textContent).toContain("Saved");+});diff --git a/apps/mobile/src/components/ui/Dialog.android.tsx b/apps/mobile/src/components/ui/Dialog.android.tsxindex 5cd3519c1b6..626c6c305a5 100644--- a/apps/mobile/src/components/ui/Dialog.android.tsx+++ b/apps/mobile/src/components/ui/Dialog.android.tsx@@ -62,12 +62,15 @@ export function Host() {const [inputValue, setInputValue] = useState("");const nextPresentationId = useRef(0);useEffect(() => {- presentRequest = (request) => {+ const present = (request: DialogRequest) => {setInputValue(request.kind === "text-input" ? request.request.initialValue : "");setPresented({ ...request, id: ++nextPresentationId.current });};+ presentRequest = present;return () => {- presentRequest = null;+ // When Android replaces the activity, the new root's Host registers before the old root+ // unmounts. Clearing unconditionally would leave the live Host without dialogs.+ if (presentRequest === present) presentRequest = null;};}, []);diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.tsindex 07c54a2bb6e..7fe24034f1e 100644--- a/apps/mobile/vite.config.ts+++ b/apps/mobile/vite.config.ts@@ -2,6 +2,7 @@ import { defineConfig } from "vite-plus";import "vite-plus/test/config";const reactTests = [+ "src/components/ui/Dialog.android.test.tsx","src/features/home/home-route-visibility.test.tsx","src/features/settings/SettingsServerControlsRouteScreen.test.tsx","src/features/settings/components/BranchNamingSettings.test.tsx",