Skip to content

Commit 5306339

Browse files
fix(mobile): ignore project completion after warm route changes
1 parent 77a3149 commit 5306339

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ function mountProjectFlow(
326326
flowRoutes: current.routes.slice(underlyingRoutes.length),
327327
getState: () => current,
328328
back: () => apply(StackActions.pop()),
329+
setParams: (params: object) => apply(CommonActions.setParams(params)),
329330
push: (name: string) => apply(StackActions.push(name)),
330331
};
331332
}
@@ -536,6 +537,23 @@ describe("local project navigation", () => {
536537
expect(state.dispatch).not.toHaveBeenCalled();
537538
});
538539

540+
it("does not replace a warm-linked flow sharing the original route key", async () => {
541+
const { created, projected, submit } = setup();
542+
const navigation = mountProjectFlow(false);
543+
const submission = submit();
544+
created.resolve(AsyncResult.success(undefined));
545+
await created.promise;
546+
const originalKey = navigation.getState().routes.at(-1)!.key;
547+
navigation.setParams({ environmentId: "other-environment" });
548+
const remaining = navigation.getState();
549+
expect(remaining.routes.at(-1)!.key).toBe(originalKey);
550+
projected.resolve({ environmentId: "environment", id: "project" });
551+
await submission;
552+
553+
expect(navigation.getState()).toEqual(remaining);
554+
expect(state.dispatch).not.toHaveBeenCalled();
555+
});
556+
539557
it("does not redirect a reopened flow when an earlier legacy clone finishes", async () => {
540558
const { created, projected } = setup();
541559
state.scopes.add(AuthSourceControlWriteScope);

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,11 @@ export function AddProjectSourceScreen(props: { readonly environmentId?: string
659659
);
660660
}
661661

662-
function getActiveRouteKey(navigation: {
662+
function getActiveRoute(navigation: {
663663
getState: () => NavigationState<ParamListBase> | undefined;
664-
}): string | undefined {
664+
}) {
665665
const state = navigation.getState();
666-
return state?.routes[state.index]?.key;
666+
return state?.routes[state.index];
667667
}
668668

669669
function openNewTaskDraft(
@@ -678,11 +678,19 @@ function openNewTaskDraft(
678678
cloning?: "1";
679679
draftId?: string;
680680
},
681-
originRouteKey: string | undefined,
681+
originRoute: NavigationState<ParamListBase>["routes"][number] | undefined,
682682
) {
683683
const state = navigation.getState();
684-
// Async creation can finish after this flow was dismissed or another route opened.
685-
if (!state || state.routes[state.index]?.key !== originRouteKey) return;
684+
// Warm links can replace route params without changing the native controller key.
685+
const activeRoute = state?.routes[state.index];
686+
if (
687+
!state ||
688+
!originRoute ||
689+
activeRoute?.key !== originRoute.key ||
690+
activeRoute.name !== originRoute.name ||
691+
activeRoute.params !== originRoute.params
692+
)
693+
return;
686694
const activeRoutes = state.routes.slice(0, state.index + 1);
687695
const threadIndex = activeRoutes.findLastIndex((route) => route.name === "Thread");
688696
const flowIndex = activeRoutes.findIndex(
@@ -714,7 +722,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
714722
const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false });
715723
const projects = useProjects();
716724

717-
return async (workspaceRoot: string, originRouteKey = getActiveRouteKey(navigation)) => {
725+
return async (workspaceRoot: string, originRoute = getActiveRoute(navigation)) => {
718726
if (
719727
!environment ||
720728
!canCreateProjectInEnvironment(environment.connectionState) ||
@@ -741,7 +749,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
741749
projectId: existing.id,
742750
title: existing.title,
743751
},
744-
originRouteKey,
752+
originRoute,
745753
);
746754
return;
747755
}
@@ -779,7 +787,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
779787
projectId,
780788
title: inferProjectTitleFromPath(workspaceRoot),
781789
},
782-
originRouteKey,
790+
originRoute,
783791
);
784792
return result;
785793
};
@@ -1063,7 +1071,7 @@ export function AddProjectNewScreen(props: { readonly environmentId?: string | s
10631071
const submittingRef = useRef(false);
10641072
const submit = async () => {
10651073
if (!environment || trimmedName.length === 0 || submittingRef.current) return;
1066-
const originRouteKey = getActiveRouteKey(navigation);
1074+
const originRoute = getActiveRoute(navigation);
10671075
submittingRef.current = true;
10681076
setError(null);
10691077
setIsSubmitting(true);
@@ -1121,7 +1129,7 @@ export function AddProjectNewScreen(props: { readonly environmentId?: string | s
11211129
projectId,
11221130
title: trimmedName,
11231131
},
1124-
originRouteKey,
1132+
originRoute,
11251133
);
11261134
};
11271135

@@ -1364,7 +1372,7 @@ export function AddProjectDestinationScreen(props: {
13641372
return;
13651373
}
13661374

1367-
const originRouteKey = getActiveRouteKey(navigation);
1375+
const originRoute = getActiveRoute(navigation);
13681376
setIsSubmitting(true);
13691377
if (environment.supportsCloneTracking) {
13701378
const projectId = ProjectId.make(uuidv4());
@@ -1406,7 +1414,7 @@ export function AddProjectDestinationScreen(props: {
14061414
)
14071415
: {}),
14081416
},
1409-
originRouteKey,
1417+
originRoute,
14101418
);
14111419
}
14121420
}
@@ -1423,7 +1431,7 @@ export function AddProjectDestinationScreen(props: {
14231431
if (AsyncResult.isFailure(cloneResult)) {
14241432
setError(errorMessage(Cause.squash(cloneResult.cause)));
14251433
} else {
1426-
const createResult = await createProject(cloneResult.value.cwd, originRouteKey);
1434+
const createResult = await createProject(cloneResult.value.cwd, originRoute);
14271435
if (createResult && AsyncResult.isFailure(createResult)) {
14281436
setError(errorMessage(Cause.squash(createResult.cause)));
14291437
}

0 commit comments

Comments
 (0)