From fa7d3d781db5896f13d507a81e64f2be010966e5 Mon Sep 17 00:00:00 2001 From: wSedlacek Date: Fri, 6 Nov 2026 05:39:52 -0800 Subject: [PATCH 1/4] fix(mobile): reuse existing thread or PR screen instead of pushing again Opening a compose action or PR link from a drilled-in page was stacking a duplicate Thread/PR. Pop back when that page is already on the stack. --- .../features/layout/useOpenWorkspaceDetail.ts | 17 +++++- .../workspace-detail-selection.test.ts | 60 +++++++++++++++++++ .../workspace/workspace-detail-selection.ts | 22 ++++++- apps/mobile/src/native/useOpenPullRequest.ts | 15 +++-- 4 files changed, 103 insertions(+), 11 deletions(-) diff --git a/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts b/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts index 64966c35844c..14c60d8b6c14 100644 --- a/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts +++ b/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts @@ -2,11 +2,16 @@ import { StackActions, useNavigation } from "@react-navigation/native"; import { Platform } from "react-native"; import { useSelectWorkspaceDetail } from "./workspace/useSelectWorkspaceDetail"; -import type { WorkspaceDetail, WorkspaceDetailSheet } from "./workspace/workspace-detail-selection"; +import { + workspaceDetailOpenStep, + type WorkspaceDetail, + type WorkspaceDetailSheet, +} from "./workspace/workspace-detail-selection"; /** * Opens a thread or pull request from another page. Android shows one detail page at a time, so - * it selects the detail; iOS pushes it over the current page, so Back returns there. A `sheet` + * it selects the detail; iOS pops back to it when that page is already on the stack (so composing + * from a drilled-in page returns there) and otherwise pushes over the current page. A `sheet` * opens over the detail on Android and on its own over the current page on iOS. */ export function useOpenWorkspaceDetail() { @@ -15,6 +20,12 @@ export function useOpenWorkspaceDetail() { return (detail: WorkspaceDetail, sheet?: WorkspaceDetailSheet) => { if (Platform.OS === "android") selectDetail(detail, sheet); else if (sheet) navigation.navigate(sheet.name, sheet.params); - else navigation.dispatch(StackActions.push(detail.name, detail.params)); + else { + const state = navigation.getState(); + const step = state ? workspaceDetailOpenStep(state, detail) : { type: "push" as const }; + if (step.type === "stay") return; + if (step.type === "pop") navigation.dispatch(StackActions.pop(step.count)); + else navigation.dispatch(StackActions.push(detail.name, detail.params)); + } }; } diff --git a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts index 94e4e3be7dbc..8217df0ebc88 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts +++ b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts @@ -4,6 +4,7 @@ import { selectWorkspaceDetail, setNativelyPoppingWorkspaceRoute, workspaceDetailLinkAction, + workspaceDetailOpenStep, workspaceDetailRouter, type WorkspaceDetail, } from "./workspace-detail-selection"; @@ -139,3 +140,62 @@ describe("workspaceDetailLinkAction", () => { ).toBeUndefined(); }); }); + +describe("workspaceDetailOpenStep", () => { + it("stays when the detail is already focused", () => { + const state = select(home(), threadA); + expect(workspaceDetailOpenStep(state, threadA)).toEqual({ type: "stay" }); + }); + + it("pops back to a thread under a drilled-in pull request", () => { + const state = push(select(home(), threadA), "PullRequest", pullRequest.params); + expect(workspaceDetailOpenStep(state, threadA)).toEqual({ type: "pop", count: 1 }); + }); + + it("pops past several pages to the matching thread", () => { + const state = push( + push(select(home(), threadA), "PullRequest", pullRequest.params), + "GitOverview", + ); + expect(workspaceDetailOpenStep(state, threadA)).toEqual({ type: "pop", count: 2 }); + }); + + it("pushes when the thread is not on the stack", () => { + const state = select(home(), pullRequest); + expect(workspaceDetailOpenStep(state, threadA)).toEqual({ type: "push" }); + }); + + it("does not treat a different thread as a match", () => { + const state = push(select(home(), threadA), "PullRequest", pullRequest.params); + expect(workspaceDetailOpenStep(state, threadB)).toEqual({ type: "push" }); + }); + + it("pushes a new-task draft instead of reusing a thread", () => { + const state = push(select(home(), threadA), "PullRequest", pullRequest.params); + expect( + workspaceDetailOpenStep(state, { + name: "Thread", + params: { draft: { draftId: "new-task:ask", title: "Ask about #1" } }, + }), + ).toEqual({ type: "push" }); + }); + + it("stays when the pull request is already focused", () => { + const state = select(home(), pullRequest); + expect(workspaceDetailOpenStep(state, pullRequest)).toEqual({ type: "stay" }); + }); + + it("pops back to a pull request under a drilled-in page", () => { + const state = push(select(home(), pullRequest), "GitOverview"); + expect(workspaceDetailOpenStep(state, pullRequest)).toEqual({ type: "pop", count: 1 }); + }); + + it("does not treat a different pull request as a match", () => { + const other = { + name: "PullRequest" as const, + params: { ...pullRequest.params, number: 2 }, + }; + const state = push(select(home(), pullRequest), "GitOverview"); + expect(workspaceDetailOpenStep(state, other)).toEqual({ type: "push" }); + }); +}); diff --git a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts index ad7f37f14036..db135aebb605 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts +++ b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts @@ -46,13 +46,31 @@ const IDENTITY = { type RouteLike = { readonly name: string; readonly key?: string; readonly params?: object }; -function showsDetail(route: RouteLike, detail: WorkspaceDetail) { +export function workspaceDetailMatches(route: RouteLike, detail: WorkspaceDetail) { if (route.name !== detail.name) return false; const current = route.params as Readonly> | undefined; const target = detail.params as Readonly> | undefined; return IDENTITY[detail.name].every((key) => current?.[key] === target?.[key]); } +/** + * How opening a thread or pull request from another page should move: stay if it is focused, + * pop back to it when it is already under the current page, otherwise push. + */ +export function workspaceDetailOpenStep( + state: { readonly index: number; readonly routes: readonly RouteLike[] }, + detail: WorkspaceDetail, +): + | { readonly type: "stay" } + | { readonly type: "pop"; readonly count: number } + | { readonly type: "push" } { + const routes = state.routes.slice(0, state.index + 1); + const index = routes.findLastIndex((route) => workspaceDetailMatches(route, detail)); + if (index === state.index) return { type: "stay" }; + if (index >= 0) return { type: "pop", count: state.index - index }; + return { type: "push" }; +} + // UIKit pops a card by native Back before the router hears of it, so a row tapped meanwhile // must not reuse the card on its way out. let nativelyPoppingRoute: string | null = null; @@ -75,7 +93,7 @@ function workspaceDetailState< const home = live.find((route) => route.name === "Home") ?? { name: "Home" }; const page = live.find((route) => route.name !== "Home" && route.name !== "Settings"); const reused = - live.findLast((route) => showsDetail(route, detail)) ?? + live.findLast((route) => workspaceDetailMatches(route, detail)) ?? (page?.name === detail.name ? page : undefined); const routes = [ home, diff --git a/apps/mobile/src/native/useOpenPullRequest.ts b/apps/mobile/src/native/useOpenPullRequest.ts index ca6edda06a3b..1e81ea73fc68 100644 --- a/apps/mobile/src/native/useOpenPullRequest.ts +++ b/apps/mobile/src/native/useOpenPullRequest.ts @@ -1,7 +1,7 @@ import { useNavigation, useRoute, StackActions } from "@react-navigation/native"; import { nativePullRequestDestination } from "../lib/pullRequestNavigation"; import { Platform } from "react-native"; -import { useSelectWorkspaceDetail } from "../features/layout/workspace/useSelectWorkspaceDetail"; +import { useOpenWorkspaceDetail } from "../features/layout/useOpenWorkspaceDetail"; import { EnvironmentId, ThreadId } from "@t3tools/contracts"; import { useProjects, useThreadShell } from "../state/entities"; import { tryOpenExternalUrl } from "../lib/openExternalUrl"; @@ -14,7 +14,7 @@ type PullRequestContext = { readonly environmentId: string; readonly projectId: // Avoid useRoute so layouts without route context can open any thread's pull requests. export function useNavigateToPullRequest(options?: { readonly replace?: boolean }) { const navigation = useNavigation(); - const selectDetail = useSelectWorkspaceDetail(); + const openDetail = useOpenWorkspaceDetail(); const projects = useProjects(); const replace = options?.replace; return async (url: string, context: PullRequestContext | null) => { @@ -25,10 +25,13 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean ); if (!destination) return tryOpenExternalUrl(url, "pull-request"); const params = withKnownPullRequestSummary(destination); - // Android shows one page at a time; iOS opens it over the current page, so Back returns. - if (Platform.OS === "android") selectDetail({ name: "PullRequest", params }); - else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params)); - else navigation.dispatch(StackActions.push("PullRequest", params)); + // Android selects one detail; iOS reuses a PR already on the stack, otherwise pushes. + // `replace` still swaps the current page (e.g. dismissing a sheet into the PR). + if (replace && Platform.OS !== "android") { + navigation.dispatch(StackActions.replace("PullRequest", params)); + } else { + openDetail({ name: "PullRequest", params }); + } return true; }; } From e8583ac7547d53923f568098117d3b816eb5d815 Mon Sep 17 00:00:00 2001 From: wSedlacek Date: Sat, 7 Nov 2026 09:04:14 -0800 Subject: [PATCH 2/4] fix(mobile): match draft thread routes by draft identity Draft Thread params omit environmentId/threadId, so reuse treated every draft as the same screen and could pop to an older draft. --- .../workspace-detail-selection.test.ts | 22 +++++++++++++++++++ .../workspace/workspace-detail-selection.ts | 13 +++++++++++ 2 files changed, 35 insertions(+) diff --git a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts index 8217df0ebc88..81df549ff4ba 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts +++ b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts @@ -180,6 +180,28 @@ describe("workspaceDetailOpenStep", () => { ).toEqual({ type: "push" }); }); + it("pushes a new draft when a different draft is under the current page", () => { + const earlier = { + name: "Thread" as const, + params: { draft: { draftId: "new-task:earlier", title: "Earlier ask" } }, + }; + const next = { + name: "Thread" as const, + params: { draft: { draftId: "new-task:next", title: "Ask about #1" } }, + }; + const state = push(select(home(), earlier), "PullRequest", pullRequest.params); + expect(workspaceDetailOpenStep(state, next)).toEqual({ type: "push" }); + }); + + it("pops back to the same draft under a drilled-in page", () => { + const draft = { + name: "Thread" as const, + params: { draft: { draftId: "new-task:ask", title: "Ask about #1" } }, + }; + const state = push(select(home(), draft), "PullRequest", pullRequest.params); + expect(workspaceDetailOpenStep(state, draft)).toEqual({ type: "pop", count: 1 }); + }); + it("stays when the pull request is already focused", () => { const state = select(home(), pullRequest); expect(workspaceDetailOpenStep(state, pullRequest)).toEqual({ type: "stay" }); diff --git a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts index db135aebb605..64b0e815ca34 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts +++ b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts @@ -6,6 +6,7 @@ import type { Router, StackNavigationState, } from "@react-navigation/native"; +import { draftRouteIdentity, type NewTaskDraftRouteParams } from "../../threads/thread-route-state"; import { workspaceRoutesOf } from "../workspace-overlay-routes"; type RootParams = ReactNavigation.RootParamList; @@ -50,6 +51,18 @@ export function workspaceDetailMatches(route: RouteLike, detail: WorkspaceDetail if (route.name !== detail.name) return false; const current = route.params as Readonly> | undefined; const target = detail.params as Readonly> | undefined; + if (detail.name === "Thread") { + const currentDraft = current?.draft as NewTaskDraftRouteParams | undefined; + const targetDraft = target?.draft as NewTaskDraftRouteParams | undefined; + // Drafts omit top-level environmentId/threadId, so IDENTITY alone treats every draft as equal. + if (currentDraft || targetDraft) { + return ( + currentDraft !== undefined && + targetDraft !== undefined && + draftRouteIdentity(currentDraft) === draftRouteIdentity(targetDraft) + ); + } + } return IDENTITY[detail.name].every((key) => current?.[key] === target?.[key]); } From 1a276fe2123f58ab4a35728a1af7065547e5db32 Mon Sep 17 00:00:00 2001 From: wSedlacek Date: Sat, 7 Nov 2026 09:19:12 -0800 Subject: [PATCH 3/4] fix(mobile): reuse workspace details against the root stack Inspector thread previews use an independent navigator, so deciding stay/pop/push from local state missed a PR already under the host. --- .../features/layout/useOpenWorkspaceDetail.ts | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts b/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts index 14c60d8b6c14..3645e5b0e7eb 100644 --- a/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts +++ b/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts @@ -1,6 +1,7 @@ -import { StackActions, useNavigation } from "@react-navigation/native"; +import { StackActions, useNavigation, type NavigationAction } from "@react-navigation/native"; import { Platform } from "react-native"; +import { rootNavigationRef } from "../../lib/rootNavigation"; import { useSelectWorkspaceDetail } from "./workspace/useSelectWorkspaceDetail"; import { workspaceDetailOpenStep, @@ -9,10 +10,9 @@ import { } from "./workspace/workspace-detail-selection"; /** - * Opens a thread or pull request from another page. Android shows one detail page at a time, so - * it selects the detail; iOS pops back to it when that page is already on the stack (so composing - * from a drilled-in page returns there) and otherwise pushes over the current page. A `sheet` - * opens over the detail on Android and on its own over the current page on iOS. + * Opens a thread or PR from another page. Android selects the detail; iOS reuses it on the root + * stack (inspector previews are independent navigators) or else pushes. A `sheet` opens over the + * detail on Android and alone over the current page on iOS. */ export function useOpenWorkspaceDetail() { const navigation = useNavigation(); @@ -21,11 +21,16 @@ export function useOpenWorkspaceDetail() { if (Platform.OS === "android") selectDetail(detail, sheet); else if (sheet) navigation.navigate(sheet.name, sheet.params); else { - const state = navigation.getState(); + const root = rootNavigationRef.isReady() ? rootNavigationRef.getRootState() : undefined; + const state = root ?? navigation.getState(); const step = state ? workspaceDetailOpenStep(state, detail) : { type: "push" as const }; + const dispatch = (action: NavigationAction) => { + if (root?.key !== undefined) rootNavigationRef.dispatch({ ...action, target: root.key }); + else navigation.dispatch(action); + }; if (step.type === "stay") return; - if (step.type === "pop") navigation.dispatch(StackActions.pop(step.count)); - else navigation.dispatch(StackActions.push(detail.name, detail.params)); + if (step.type === "pop") dispatch(StackActions.pop(step.count)); + else dispatch(StackActions.push(detail.name, detail.params)); } }; } From 83bce958166fd61d3d6c6be1d7595cc48de6c8d4 Mon Sep 17 00:00:00 2001 From: maria-rcks Date: Wed, 11 Nov 2026 05:50:44 +0000 Subject: [PATCH 4/4] fix(mobile): skip the natively popping page when reusing a detail on iOS --- .../workspace/workspace-detail-selection.test.ts | 10 ++++++++++ .../layout/workspace/workspace-detail-selection.ts | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts index 81df549ff4ba..7ee23eff460e 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts +++ b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.test.ts @@ -202,6 +202,16 @@ describe("workspaceDetailOpenStep", () => { expect(workspaceDetailOpenStep(state, draft)).toEqual({ type: "pop", count: 1 }); }); + it("pushes instead of reusing the page native Back is popping", () => { + const state = select(home(), threadA); + setNativelyPoppingWorkspaceRoute(state.routes[1]!.key); + try { + expect(workspaceDetailOpenStep(state, threadA)).toEqual({ type: "push" }); + } finally { + setNativelyPoppingWorkspaceRoute(null); + } + }); + it("stays when the pull request is already focused", () => { const state = select(home(), pullRequest); expect(workspaceDetailOpenStep(state, pullRequest)).toEqual({ type: "stay" }); diff --git a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts index 64b0e815ca34..4638241758c1 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts +++ b/apps/mobile/src/features/layout/workspace/workspace-detail-selection.ts @@ -78,7 +78,9 @@ export function workspaceDetailOpenStep( | { readonly type: "pop"; readonly count: number } | { readonly type: "push" } { const routes = state.routes.slice(0, state.index + 1); - const index = routes.findLastIndex((route) => workspaceDetailMatches(route, detail)); + const index = routes.findLastIndex( + (route) => route.key !== nativelyPoppingRoute && workspaceDetailMatches(route, detail), + ); if (index === state.index) return { type: "stay" }; if (index >= 0) return { type: "pop", count: state.index - index }; return { type: "push" };