diff --git a/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts b/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts index 64966c35844c..da95c2c2920c 100644 --- a/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts +++ b/apps/mobile/src/features/layout/useOpenWorkspaceDetail.ts @@ -1,13 +1,23 @@ -import { StackActions, useNavigation } from "@react-navigation/native"; +import { + CommonActions, + StackActions, + useNavigation, + type NavigationAction, +} from "@react-navigation/native"; import { Platform } from "react-native"; +import { rootNavigationRef } from "../../lib/rootNavigation"; 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` - * 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(); @@ -15,6 +25,22 @@ 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 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 === "push" || !state) { + dispatch(StackActions.push(detail.name, detail.params)); + return; + } + // Carry new params (e.g. a PR link's anchor) onto the reused page, as Android's select does. + const reused = state.routes[state.index - (step.type === "pop" ? step.count : 0)]; + dispatch({ ...CommonActions.replaceParams(detail.params), source: reused?.key }); + if (step.type === "pop") dispatch(StackActions.pop(step.count)); + } }; } 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..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 @@ -4,6 +4,7 @@ import { selectWorkspaceDetail, setNativelyPoppingWorkspaceRoute, workspaceDetailLinkAction, + workspaceDetailOpenStep, workspaceDetailRouter, type WorkspaceDetail, } from "./workspace-detail-selection"; @@ -139,3 +140,94 @@ 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("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("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" }); + }); + + 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..4638241758c1 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; @@ -46,13 +47,45 @@ 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; + 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]); } +/** + * 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) => 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" }; +} + // 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 +108,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 ed080ade2f96..385289248b5b 100644 --- a/apps/mobile/src/native/useOpenPullRequest.ts +++ b/apps/mobile/src/native/useOpenPullRequest.ts @@ -1,5 +1,7 @@ import { useNavigation, useRoute, StackActions } from "@react-navigation/native"; import { nativePullRequestDestination } from "../lib/pullRequestNavigation"; +import { Platform } from "react-native"; +import { useOpenWorkspaceDetail } from "../features/layout/useOpenWorkspaceDetail"; import { EnvironmentId, ThreadId } from "@t3tools/contracts"; import { useProjects, useThreadShell } from "../state/entities"; import { tryOpenExternalUrl } from "../lib/openExternalUrl"; @@ -15,6 +17,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 openDetail = useOpenWorkspaceDetail(); const projects = useProjects(); const replace = options?.replace; return async (url: string, context: PullRequestContext | null) => { @@ -28,8 +31,11 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean ...destination, ...(destination.navigationUrl ? { navigationId: nextLinkNavigationId() } : {}), }); + // Android pushes so Back returns to the chat; iOS reuses a PR already on the stack. if (replace) navigation.dispatch(StackActions.replace("PullRequest", params)); - else navigation.dispatch(StackActions.push("PullRequest", params)); + else if (Platform.OS === "android") + navigation.dispatch(StackActions.push("PullRequest", params)); + else openDetail({ name: "PullRequest", params }); return true; }; }