diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx index c3b46cf8e24c..48d7ca7a3a1c 100644 --- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx +++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx @@ -100,7 +100,7 @@ import { workspaceTabColumns, workspaceTabNavigationState, } from "../workspace/workspace-tab-navigation"; -import type { WorkspaceTab } from "../workspace/workspace-tabs-state"; +import { workspaceTabOf, type WorkspaceTab } from "../workspace/workspace-tabs-state"; import { workspaceRoutesOf } from "./workspace-overlay-routes"; import { SettingsWorkspaceLayout } from "../settings/SettingsWorkspaceLayout"; @@ -535,10 +535,7 @@ function AdaptiveWorkspaceLayoutContent( if (Platform.OS === "android") Keyboard.dismiss(); setFileInspectorPreferredVisible(false); if (layout.primarySidebarOverlaysContent) setPrimarySidebarVisible(false); - workspace?.dispatch({ - type: "select", - tab: detail.name === "Thread" ? "threads" : "pullRequests", - }); + workspace?.dispatch({ type: "select", tab: workspaceTabOf(detail.name) }); startOpeningDetail(detail); navigation.dispatch(selectWorkspaceDetail(detail, sheet)); }); diff --git a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx index bd3f04d9d6b4..0623644fd394 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx @@ -337,7 +337,7 @@ function usePullRequestToolbarHeight(bottomInset: number) { } export function PullRequestRouteScreen({ route }: StaticScreenProps) { - const returnToList = useAndroidWorkspaceBack("pullRequests"); + const returnToList = useAndroidWorkspaceBack(); const { layout } = useAdaptiveWorkspaceLayout(); const sidebarHeaderItem = useWorkspaceSidebarHeaderItem(); const navigation = useNavigation(); diff --git a/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx b/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx index b2f51a9bd0ab..7a443e03baad 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx @@ -1,7 +1,6 @@ import { StackActions, useNavigation } from "@react-navigation/native"; import { useState } from "react"; -import { Platform, View } from "react-native"; -import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail"; +import { View } from "react-native"; import { AppText as Text } from "../../components/ui/AppText"; import { PullRequestCardGroup, PullRequestCardRow } from "./PullRequestControls"; import { pullRequestStatus } from "./pullRequestStatus"; @@ -14,7 +13,6 @@ export function PullRequestStackCard(props: { readonly state: PullRequestStackState; }) { const navigation = useNavigation(); - const selectDetail = useSelectWorkspaceDetail(); const [expanded, setExpanded] = useState(false); const stack = props.state.stack; if (!stack) { @@ -67,9 +65,7 @@ export function PullRequestStackCard(props: { ? undefined : () => { const params = { ...props.params, number: layer.number }; - if (Platform.OS === "android") - selectDetail({ name: "PullRequest", params }); - else navigation.dispatch(StackActions.push("PullRequest", params)); + navigation.dispatch(StackActions.push("PullRequest", params)); } } last={index === stack.layers.length - 1} diff --git a/apps/mobile/src/features/pull-requests/usePullRequestThreads.ts b/apps/mobile/src/features/pull-requests/usePullRequestThreads.ts index e1d9d8715106..1418c7f8cb45 100644 --- a/apps/mobile/src/features/pull-requests/usePullRequestThreads.ts +++ b/apps/mobile/src/features/pull-requests/usePullRequestThreads.ts @@ -19,8 +19,6 @@ import { formatComposerContextReference } from "@t3tools/shared/composerContextR import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pull-requests"; import { useRef, useState } from "react"; -import { Platform } from "react-native"; -import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail"; import { pullRequestComposerContext } from "../../lib/composerContext"; import { scopedThreadKey } from "../../lib/scopedEntities"; import { uuidv4 } from "../../lib/uuid"; @@ -76,16 +74,13 @@ export function usePullRequestThreads( const canAsk = !connected || canOperate; const canCheckout = useEnvironmentScope(target.environmentId, AuthSourceControlWriteScope); const navigation = useNavigation(); - const selectDetail = useSelectWorkspaceDetail(); const pending = useRef(false); const previousPrompts = useRef(new Map()); const openThread = (thread: LinkedThread) => { openThreadRoute({ environmentId: params.environmentId, threadId: thread.id }); }; - // Android shows one page at a time; iOS opens the thread over the pull request, so Back returns. const openThreadRoute = (destination: ReactNavigation.RootParamList["Thread"]) => { - if (Platform.OS === "android") selectDetail({ name: "Thread", params: destination }); - else navigation.dispatch(StackActions.push("Thread", destination)); + navigation.dispatch(StackActions.push("Thread", destination)); }; const [busy, setBusy] = useState(false); const ask = async (thread?: LinkedThread, task: PullRequestAgentTask = "ask") => { diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx index a21fcf039aca..f2036b06d41d 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx @@ -157,7 +157,7 @@ export function NewTaskDraftScreen(props: { const flow = useNewTaskFlow(); const navigation = useNavigation(); const isFocused = useIsFocused(); - const goBack = useWorkspaceBackNavigation("threads"); + const goBack = useWorkspaceBackNavigation(); const currentRoute = useRoute(); const flowKey = newTaskFlowKey(currentRoute); const { getShare, isLoading: isIncomingShareInboxLoading } = useIncomingShare(); diff --git a/apps/mobile/src/features/threads/ThreadHeader.tsx b/apps/mobile/src/features/threads/ThreadHeader.tsx index acdbaea83efa..cb8d605292a3 100644 --- a/apps/mobile/src/features/threads/ThreadHeader.tsx +++ b/apps/mobile/src/features/threads/ThreadHeader.tsx @@ -19,7 +19,7 @@ export function ThreadHeader( readonly onOpenFilesInspector: () => void; }, ) { - const goBack = useWorkspaceBackNavigation("threads"); + const goBack = useWorkspaceBackNavigation(); const { layout, panes, toggleAuxiliaryPane, composerKeyboardActive } = useAdaptiveWorkspaceLayout(); const { onOpenTerminal, onMergeBack } = props.gitControls; diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index 737e1aff90ad..3db4152c8aa4 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -126,7 +126,7 @@ function firstRouteParam(value: string | string[] | undefined): string | null { } function ThreadRouteFallbackLayout(props: { readonly children: ReactNode }) { - const goBack = useWorkspaceBackNavigation("threads"); + const goBack = useWorkspaceBackNavigation(); const { layout } = useAdaptiveWorkspaceLayout(); const { themeVariables } = useAppearancePreferences(); if (Platform.OS !== "android") @@ -239,7 +239,7 @@ export function ThreadRouteScreen(props: ThreadRouteScreenProps) { } function LiveThreadRouteController(props: ThreadRouteScreenProps) { - useAndroidWorkspaceBack("threads"); + useAndroidWorkspaceBack(); const connectionsReady = useConnectionsReady(); const { connectionState } = useRemoteConnectionStatus(); const { selectedThread } = useThreadSelection(); diff --git a/apps/mobile/src/features/workspace/AndroidWorkspaceTabsProvider.tsx b/apps/mobile/src/features/workspace/AndroidWorkspaceTabsProvider.tsx index 5fc9b50097ba..3612561ede6c 100644 --- a/apps/mobile/src/features/workspace/AndroidWorkspaceTabsProvider.tsx +++ b/apps/mobile/src/features/workspace/AndroidWorkspaceTabsProvider.tsx @@ -3,6 +3,7 @@ import { createContext, use, type ReactNode } from "react"; import { Platform } from "react-native"; import { androidWorkspaceSettingsOpen } from "./android-workspace-tabs"; import { WorkspaceTabsProvider } from "./WorkspaceTabsContext"; +import { workspaceTabOf } from "./workspace-tabs-state"; interface AndroidWorkspaceRoot { /** @@ -35,7 +36,10 @@ export function AndroidWorkspaceTabsProvider(props: { settingsIndex < 0 ? null : (props.state.routes[settingsIndex + 1]?.name ?? null); const root = { key, settingsOpen, settingsRoute, settingsPage }; return Platform.OS === "android" ? ( - + // A cold-start link lands on a page with no list selection; start on that page's list. + route.name !== "Home")?.name)} + > {props.children} ) : ( diff --git a/apps/mobile/src/features/workspace/WorkspaceTabsContext.tsx b/apps/mobile/src/features/workspace/WorkspaceTabsContext.tsx index 6faa8c73d05e..8ca56b8d50a9 100644 --- a/apps/mobile/src/features/workspace/WorkspaceTabsContext.tsx +++ b/apps/mobile/src/features/workspace/WorkspaceTabsContext.tsx @@ -52,8 +52,8 @@ function resolveWorkspaceTabsSelection( // One model per provider. Handlers on frozen screens keep stale closures, so // they read the current selection from the registry at call time. -function createWorkspaceTabsModel() { - const stored = Atom.make(initialWorkspaceTabsState()); +function createWorkspaceTabsModel(selectedTab?: WorkspaceTab) { + const stored = Atom.make(initialWorkspaceTabsState(selectedTab)); const scrollOffsets: Record = { threads: 0, pullRequests: 0, @@ -75,9 +75,10 @@ function createWorkspaceTabsModel() { export function WorkspaceTabsProvider(props: { readonly children: ReactNode; readonly settingsOpen?: boolean; + readonly initialTab?: WorkspaceTab; }) { const registry = use(RegistryContext); - const [model] = useState(createWorkspaceTabsModel); + const [model] = useState(() => createWorkspaceTabsModel(props.initialTab)); const state = useAtomValue(model.resolved); const context = { state, diff --git a/apps/mobile/src/features/workspace/useAndroidWorkspaceNavigation.ts b/apps/mobile/src/features/workspace/useAndroidWorkspaceNavigation.ts index 92e157a45f04..fff3e5d80739 100644 --- a/apps/mobile/src/features/workspace/useAndroidWorkspaceNavigation.ts +++ b/apps/mobile/src/features/workspace/useAndroidWorkspaceNavigation.ts @@ -1,50 +1,29 @@ import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native"; import { useCallback } from "react"; import { BackHandler, Keyboard, Platform } from "react-native"; -import { - resetAndroidWorkspace, - returnToAndroidWorkspaceList, -} from "../../lib/android-workspace-navigation"; -import { useWorkspaceTabs } from "./WorkspaceTabsContext"; -import type { WorkspaceTab } from "./workspace-tabs-state"; +import { returnToAndroidWorkspaceList } from "../../lib/android-workspace-navigation"; -function useWorkspaceListNavigation(tab: WorkspaceTab) { +export function useWorkspaceBackNavigation() { const navigation = useNavigation(); - const dispatch = useWorkspaceTabs()?.dispatch; return () => { - if (Platform.OS === "android") { - dispatch?.({ type: "select", tab }); - resetAndroidWorkspace(navigation); - } else if (navigation.canGoBack()) navigation.goBack(); + if (Platform.OS === "android") returnToAndroidWorkspaceList(navigation); + else if (navigation.canGoBack()) navigation.goBack(); else navigation.dispatch(StackActions.replace("Home")); }; } -export function useWorkspaceBackNavigation(tab: WorkspaceTab) { - const navigation = useNavigation(); - const dispatch = useWorkspaceTabs()?.dispatch; - const returnToList = useWorkspaceListNavigation(tab); - return () => { - if (Platform.OS === "android") { - returnToAndroidWorkspaceList(navigation, () => dispatch?.({ type: "select", tab })); - } else returnToList(); - }; -} - -export function useAndroidWorkspaceBack(tab: WorkspaceTab) { - const dispatch = useWorkspaceTabs()?.dispatch; - const goBack = useWorkspaceBackNavigation(tab); +export function useAndroidWorkspaceBack() { + const goBack = useWorkspaceBackNavigation(); useFocusEffect( useCallback(() => { if (Platform.OS !== "android") return; - dispatch?.({ type: "select", tab }); const subscription = BackHandler.addEventListener("hardwareBackPress", () => { if (Keyboard.isVisible()) Keyboard.dismiss(); else goBack(); return true; }); return () => subscription.remove(); - }, [goBack, dispatch, tab]), + }, [goBack]), ); return goBack; } diff --git a/apps/mobile/src/features/workspace/workspace-tabs-state.ts b/apps/mobile/src/features/workspace/workspace-tabs-state.ts index f216fb9f13ca..91d29674f682 100644 --- a/apps/mobile/src/features/workspace/workspace-tabs-state.ts +++ b/apps/mobile/src/features/workspace/workspace-tabs-state.ts @@ -38,9 +38,16 @@ function emptyTab(): WorkspaceTabState { return { query: "", searchMode: "resting", scrollOffset: 0, searchEntryOffset: 0 }; } -export function initialWorkspaceTabsState(): WorkspaceTabsState { +/** The list a workspace page belongs to. */ +export function workspaceTabOf(routeName: string | undefined): WorkspaceTab { + return routeName === "PullRequest" ? "pullRequests" : "threads"; +} + +export function initialWorkspaceTabsState( + selectedTab: WorkspaceTab = "threads", +): WorkspaceTabsState { return { - selectedTab: "threads", + selectedTab, selectionRevision: 0, tabs: { threads: emptyTab(), diff --git a/apps/mobile/src/lib/android-workspace-navigation.test.ts b/apps/mobile/src/lib/android-workspace-navigation.test.ts index 51f9f3b6a89a..568e2d88786b 100644 --- a/apps/mobile/src/lib/android-workspace-navigation.test.ts +++ b/apps/mobile/src/lib/android-workspace-navigation.test.ts @@ -85,13 +85,12 @@ function names(state: ReturnType) { } describe("Android workspace navigation", () => { - it("returns to a distinct previous thread, otherwise to the workspace list", () => { + it("returns to the previous page, otherwise to the workspace list", () => { const parent = withThread(); let state = apply( parent, StackActions.push("Thread", { ...thread.params, threadId: "child-a" }), ); - let listSelected = false; const navigation = { dispatch: (action: RootStackResetAction) => { state = apply(state, action); @@ -102,39 +101,41 @@ describe("Android workspace navigation", () => { state = apply(state, StackActions.pop()); }, }; - const back = () => - returnToAndroidWorkspaceList(navigation, () => { - listSelected = true; - }); + const back = () => returnToAndroidWorkspaceList(navigation); back(); expect(state.routes).toEqual(parent.routes); expect(state.index).toBe(parent.index); - expect(listSelected).toBe(false); back(); expect(state.routes).toEqual([parent.routes[0]]); expect(state.index).toBe(0); - expect(listSelected).toBe(true); state = apply( parent, StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }), ); - listSelected = false; back(); expect(state.routes).toEqual(parent.routes); expect(state.index).toBe(parent.index); - expect(listSelected).toBe(false); + + const pullRequestOverThread = legacyHistory(); + state = pullRequestOverThread; + back(); + expect(state.routes).toEqual(pullRequestOverThread.routes.slice(0, 2)); // Confirming a Git action from the inline inspector can duplicate the current thread. state = apply(parent, StackActions.push("Thread", thread.params)); back(); - expect({ routes: names(state), index: state.index, listSelected }).toEqual({ - routes: ["Home"], - index: 0, - listSelected: true, - }); + expect({ routes: names(state), index: state.index }).toEqual({ routes: ["Home"], index: 0 }); + + const child = { ...thread.params, threadId: "child-a" }; + state = apply( + apply(parent, StackActions.push("Thread", child)), + StackActions.push("Thread", child), + ); + back(); + expect(state.routes).toEqual(parent.routes); }); it("opens a linked thread above the current one so Back returns to it", () => { diff --git a/apps/mobile/src/lib/android-workspace-navigation.ts b/apps/mobile/src/lib/android-workspace-navigation.ts index 3c2a995e41a6..67eb797c00fb 100644 --- a/apps/mobile/src/lib/android-workspace-navigation.ts +++ b/apps/mobile/src/lib/android-workspace-navigation.ts @@ -1,15 +1,14 @@ import type { NavigationState } from "@react-navigation/native"; import { resetRootStack, type RootStackNavigation } from "./root-stack-reset"; -function androidWorkspaceBackReturnsToThread(state: NavigationState) { - const current = state.routes[state.index]; - const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined; - return ( - current?.name === "Thread" && - state.routes[state.index - 1]?.name === "Thread" && - params !== undefined && - !parentThreadIsBelow(state, params) - ); +// Back follows history; Git confirmations can stack copies of the current thread, so skip those too. +function androidWorkspaceBackTarget(state: NavigationState) { + let index = state.index; + const params = state.routes[index]?.params as ReactNavigation.RootParamList["Thread"] | undefined; + if (state.routes[index]?.name === "Thread" && params) { + while (parentThreadIsBelow({ ...state, index }, params)) index -= 1; + } + return index - 1; } /** The root stack with only the workspace list. */ @@ -59,12 +58,12 @@ export function linkedThreadStep( export function returnToAndroidWorkspaceList( navigation: RootStackNavigation & { readonly goBack: () => void }, - selectTab: () => void, ) { const state = navigation.getState(); - if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack(); - selectTab(); - resetAndroidWorkspace(navigation); + const index = state ? androidWorkspaceBackTarget(state) : -1; + if (!state || index < 0) return resetAndroidWorkspace(navigation); + if (index === state.index - 1) return navigation.goBack(); + resetRootStack(navigation, () => ({ ...state, index, routes: state.routes.slice(0, index + 1) })); } // In the split view these destinations swap the detail pane beside the sidebar instead of diff --git a/apps/mobile/src/native/useOpenPullRequest.ts b/apps/mobile/src/native/useOpenPullRequest.ts index 755f0b965ee0..a7819147279a 100644 --- a/apps/mobile/src/native/useOpenPullRequest.ts +++ b/apps/mobile/src/native/useOpenPullRequest.ts @@ -1,7 +1,5 @@ import { useNavigation, useRoute, StackActions } from "@react-navigation/native"; import { nativePullRequestDestination } from "../lib/pullRequestNavigation"; -import { Platform } from "react-native"; -import { useSelectWorkspaceDetail } from "../features/workspace/useSelectWorkspaceDetail"; import { EnvironmentId, ThreadId } from "@t3tools/contracts"; import { useProjects, useThreadShell } from "../state/entities"; import { tryOpenExternalUrl } from "../lib/openExternalUrl"; @@ -13,7 +11,6 @@ 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 projects = useProjects(); const replace = options?.replace; return async (url: string, context: PullRequestContext | null) => { @@ -23,9 +20,7 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean serverConfigs.has(environmentId), ); if (!params) return tryOpenExternalUrl(url, "pull-request"); - // 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)); + if (replace) navigation.dispatch(StackActions.replace("PullRequest", params)); else navigation.dispatch(StackActions.push("PullRequest", params)); return true; };