Skip to content

Commit ab7a842

Browse files
fix(mobile): finish dismissing project flow before inserting a draft
1 parent 7846b08 commit ab7a842

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎apps/mobile/src/features/projects/AddProjectScreen.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { createRoot } from "react-dom/client";
1414
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
1515

1616
const state = vi.hoisted(() => ({
17+
transitions: new Set<(event: { data: { closing: boolean } }) => void>(),
1718
scopes: new Set<string>(),
1819
baseDirectory: "",
1920
projects: [] as string[],
@@ -74,6 +75,10 @@ vi.mock("react-native", () => ({
7475
vi.mock("@react-navigation/native", () => ({
7576
useNavigation: () => ({
7677
dispatch: state.dispatch,
78+
addListener: (_name: string, listener: (event: { data: { closing: boolean } }) => void) => {
79+
state.transitions.add(listener);
80+
return () => state.transitions.delete(listener);
81+
},
7782
getState: () =>
7883
state.navigationState ?? {
7984
index: 4,
@@ -88,6 +93,7 @@ vi.mock("@react-navigation/native", () => ({
8893
}),
8994
CommonActions: { reset: (input: unknown) => input },
9095
StackActions: {
96+
popToTop: () => ({ type: "POP_TO_TOP" }),
9197
push: (name: string, params: unknown) => ({ type: "PUSH", payload: { name, params } }),
9298
},
9399
}));
@@ -329,7 +335,9 @@ function mountProjectFlow(
329335
state.navigationState = current;
330336
state.dispatch.mockImplementation((action) =>
331337
apply(
332-
action.type === "PUSH" || action.type === "SELECT_WORKSPACE_DETAIL"
338+
action.type === "PUSH" ||
339+
action.type === "POP_TO_TOP" ||
340+
action.type === "SELECT_WORKSPACE_DETAIL"
333341
? action
334342
: CommonActions.reset(action),
335343
),
@@ -495,6 +503,7 @@ describe("add project source environment", () => {
495503

496504
describe("local project navigation", () => {
497505
function setup() {
506+
state.transitions.clear();
498507
state.navigationState = undefined;
499508
state.scopes = new Set([AuthOrchestrationOperateScope]);
500509
state.baseDirectory = "/tmp/t3-mobile-audit-fixture";
@@ -655,6 +664,21 @@ describe("local project navigation", () => {
655664
}
656665
});
657666

667+
it("does not replace a destination opened while the chooser is closing", async () => {
668+
const { created, projected, submit } = setup();
669+
const navigation = mountProjectFlow(false, false, true);
670+
const submission = submit();
671+
created.resolve(AsyncResult.success(undefined));
672+
projected.resolve({ environmentId: "environment", id: "project" });
673+
await submission;
674+
expect(navigation.getState().routes.map((route) => route.name)).toEqual(["Home"]);
675+
navigation.push("PullRequest");
676+
const selected = navigation.getState();
677+
for (const listener of state.transitions) listener({ data: { closing: true } });
678+
expect(navigation.getState()).toBe(selected);
679+
expect(state.transitions.size).toBe(0);
680+
});
681+
658682
it.each([
659683
{ origin: "Thread", withDraft: true, withChooser: true },
660684
{ origin: "NewTaskProject", withDraft: false, withChooser: true },
@@ -672,6 +696,10 @@ describe("local project navigation", () => {
672696
expect(navigation.getState()).toBe(before);
673697
projected.resolve({ environmentId: "environment", id: "project" });
674698
await submission;
699+
if (!withDraft) {
700+
expect(navigation.getState().routes.map((route) => route.name)).toEqual(["Home"]);
701+
for (const listener of state.transitions) listener({ data: { closing: true } });
702+
}
675703
expect(navigation.getState().routes.map((route) => route.name)).toEqual(["Home", "Thread"]);
676704
expect(navigation.getState().routes[1]?.params).toEqual({
677705
draft: {

‎apps/mobile/src/features/projects/AddProjectScreen.tsx‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
12
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
23
import * as Dialog from "../../components/ui/Dialog";
34
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
@@ -667,7 +668,7 @@ function getActiveRoute(navigation: {
667668
}
668669

669670
function useOpenNewTaskDraft() {
670-
const navigation = useNavigation();
671+
const navigation = useNavigation<NativeStackNavigationProp<ReactNavigation.RootParamList>>();
671672
const selectDetail = useSelectWorkspaceDetail();
672673
return (
673674
params: {
@@ -688,6 +689,20 @@ function useOpenNewTaskDraft() {
688689
activeRoute.params !== originRoute.params
689690
)
690691
return;
692+
const state = navigation.getState();
693+
if (Platform.OS === "ios" && state && !state.routes.some((route) => route.name === "Thread")) {
694+
const homeKey = state.routes[0]?.key;
695+
// UIKit must finish removing the chooser before a new detail card is inserted.
696+
const unsubscribe = navigation.addListener("transitionEnd", (event) => {
697+
if (!event.data.closing) return;
698+
unsubscribe();
699+
const current = navigation.getState();
700+
if (current?.index !== 0 || current.routes[0]?.key !== homeKey) return;
701+
selectDetail({ name: "Thread", params: { draft: params } });
702+
});
703+
navigation.dispatch(StackActions.popToTop());
704+
return;
705+
}
691706
selectDetail({ name: "Thread", params: { draft: params } });
692707
};
693708
}

0 commit comments

Comments
 (0)