TypeScript
fix(mobile): keep native screens ordered during stack pops (#17231)
Julius Marmingea6ec88f7a716
diff --git a/apps/mobile/src/native/createV5StackNavigator.ios.tsx b/apps/mobile/src/native/createV5StackNavigator.ios.tsxindex 4ef229495c6..413bd871270 100644--- a/apps/mobile/src/native/createV5StackNavigator.ios.tsx+++ b/apps/mobile/src/native/createV5StackNavigator.ios.tsx@@ -181,7 +181,14 @@ export function V5CardStackView(props: V5StackViewProps) {><NavigationContext value={descriptor.navigation}><NavigationRouteContext value={descriptor.route}>- <V5StackHeader options={descriptor.options} canGoBack={index > 0} />+ <V5StackHeader+ options={descriptor.options}+ canGoBack={+ attached+ ? props.state.routes.findIndex((current) => current.key === route.key) > 0+ : index > 0+ }+ /><NativeColumnContent>{descriptor.render()}</NativeColumnContent></NavigationRouteContext></NavigationContext>diff --git a/apps/mobile/src/native/workspace-stack-projection.test.ts b/apps/mobile/src/native/workspace-stack-projection.test.tsindex 7f0537653eb..1f5c00c6555 100644--- a/apps/mobile/src/native/workspace-stack-projection.test.ts+++ b/apps/mobile/src/native/workspace-stack-projection.test.ts@@ -143,21 +143,41 @@ describe("v5 stack handoff", () => {});it("still retains an unfinished JS pop beside a completed native pop", () => {expect(reconcileStackScreens([home, thread, files], [home], new Set([files.key]))).toEqual([- thread,home,+ thread,]);});it("retains removed native screens through a pop followed immediately by a push", () => {const popped = reconcileStackScreens([home, thread, files], [home, thread]);const next = { ...files, key: "new-files" };const screens = reconcileStackScreens(popped, [home, thread, next]);- expect(screens.map((route) => route.key)).toEqual(["files", "home", "thread", "new-files"]);+ expect(screens.map((route) => route.key)).toEqual(["home", "thread", "files", "new-files"]);expect(screens.filter((route) => [home, thread, next].includes(route))).toEqual([home,thread,next,]);- expect(screens[0]).toBe(files);+ expect(screens[2]).toBe(files);+ });+ it("keeps the root and outgoing settings page in place until the JS pop finishes", () => {+ const environments = { ...files, key: "environments", name: "SettingsEnvironments" };+ const popped = reconcileStackScreens([settings, environments], [settings]);+ expect(popped).toEqual([settings, environments]);+ expect(reconcileStackScreens(popped, [settings])).toEqual(popped);+ const next = { ...environments, key: "next-environments" };+ expect(reconcileStackScreens(popped, [settings, next])).toEqual([settings, environments, next]);+ expect(reconcileStackScreens(popped, [settings], new Set([environments.key]))).toEqual([+ settings,+ ]);+ });+ it("retains a multi-page pop in order without changing the active router order", () => {+ const replacement = { ...thread, key: "replacement" };+ const screens = reconcileStackScreens([home, thread, files], [home, replacement]);+ expect(screens).toEqual([home, thread, files, replacement]);+ expect(screens.filter((route) => route === home || route === replacement)).toEqual([+ home,+ replacement,+ ]);});it("updates params without retaining duplicate copies of the same screen", () => {const updated = { ...thread, params: { threadId: "another-thread" } };diff --git a/apps/mobile/src/native/workspace-stack-projection.ts b/apps/mobile/src/native/workspace-stack-projection.tsindex 48f196cd3f1..79e66290fe6 100644--- a/apps/mobile/src/native/workspace-stack-projection.ts+++ b/apps/mobile/src/native/workspace-stack-projection.ts@@ -57,17 +57,18 @@ export function partitionStackPresentations<T>(return groups;}-/** Preserve UIKit's outgoing screens, before newly pushed screens, until dismissal. */+/** Keep outgoing screens in place until dismissal; Fabric moves enqueue native pop/push operations. */export function reconcileStackScreens<T extends { readonly key: string }>(previous: readonly T[],current: readonly T[],completedNativeDismissals: ReadonlySet<string> = new Set(),): T[] {const active = new Set(current.map((route) => route.key));- return [- ...previous.filter(- (route) => !active.has(route.key) && !completedNativeDismissals.has(route.key),- ),- ...current,- ];+ const screens = [...current];+ previous.forEach((route, index) => {+ if (!active.has(route.key) && !completedNativeDismissals.has(route.key)) {+ screens.splice(Math.min(index, screens.length), 0, route);+ }+ });+ return screens;}