diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx index 19ffe072fb58..3a32e3352361 100644 --- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx +++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx @@ -112,7 +112,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"; import { WorkspaceThreadSidebar } from "./workspace/WorkspaceThreadSidebar"; @@ -570,10 +570,7 @@ function AdaptiveWorkspaceLayoutContent( setFileInspectorPreferredVisible(false); setInspectorSheetRoute(null); if (layout.variant === "medium") 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/layout/workspace/AndroidWorkspaceTabsProvider.tsx b/apps/mobile/src/features/layout/workspace/AndroidWorkspaceTabsProvider.tsx index 5fc9b50097ba..3612561ede6c 100644 --- a/apps/mobile/src/features/layout/workspace/AndroidWorkspaceTabsProvider.tsx +++ b/apps/mobile/src/features/layout/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/layout/workspace/WorkspaceTabsContext.tsx b/apps/mobile/src/features/layout/workspace/WorkspaceTabsContext.tsx index 9ecb6fa933fa..c0724aba9084 100644 --- a/apps/mobile/src/features/layout/workspace/WorkspaceTabsContext.tsx +++ b/apps/mobile/src/features/layout/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/layout/workspace/useAndroidWorkspaceNavigation.ts b/apps/mobile/src/features/layout/workspace/useAndroidWorkspaceNavigation.ts index dfc8e13d162c..0165634f0a31 100644 --- a/apps/mobile/src/features/layout/workspace/useAndroidWorkspaceNavigation.ts +++ b/apps/mobile/src/features/layout/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/layout/workspace/workspace-tabs-state.ts b/apps/mobile/src/features/layout/workspace/workspace-tabs-state.ts index 3598894115dc..3f6754c679c5 100644 --- a/apps/mobile/src/features/layout/workspace/workspace-tabs-state.ts +++ b/apps/mobile/src/features/layout/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/features/pull-requests/PullRequestRouteScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx index f68236379203..844bac3910f3 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx @@ -358,7 +358,7 @@ function usePullRequestToolbarHeight(bottomInset: number) { } export function PullRequestRouteScreen({ route }: StaticScreenProps) { - const returnToList = useAndroidWorkspaceBack("pullRequests"); + const returnToList = useAndroidWorkspaceBack(); const { layout } = useAdaptiveWorkspaceLayout(); const navigation = useNavigation(); const insets = useSafeAreaInsets(); diff --git a/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx b/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx index 0b2637b7c6b6..6e69e267e3fe 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestStackCard.tsx @@ -1,6 +1,6 @@ +import { StackActions, useNavigation } from "@react-navigation/native"; import { useState } from "react"; import { View } from "react-native"; -import { useOpenWorkspaceDetail } from "../layout/useOpenWorkspaceDetail"; import { AppText as Text } from "../../components/ui/AppText"; import { PullRequestCardGroup, PullRequestCardRow } from "./PullRequestControls"; import { pullRequestLayerParams } from "./pullRequestStack"; @@ -14,7 +14,7 @@ export function PullRequestStackCard(props: { readonly params: PullRequestParams; readonly state: PullRequestStackState; }) { - const openDetail = useOpenWorkspaceDetail(); + const navigation = useNavigation(); const [expanded, setExpanded] = useState(false); const stack = props.state.stack; if (!stack) { @@ -66,12 +66,14 @@ export function PullRequestStackCard(props: { selected ? undefined : () => - openDetail({ - name: "PullRequest", - params: withKnownPullRequestSummary( - pullRequestLayerParams(props.params, layer.number), + navigation.dispatch( + StackActions.push( + "PullRequest", + withKnownPullRequestSummary( + pullRequestLayerParams(props.params, layer.number), + ), ), - }) + ) } 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 85a77a254967..d79682f94488 100644 --- a/apps/mobile/src/features/pull-requests/usePullRequestThreads.ts +++ b/apps/mobile/src/features/pull-requests/usePullRequestThreads.ts @@ -3,7 +3,12 @@ import { visibleLinkedThreads } from "@t3tools/client-runtime/state/threads"; import { AsyncResult } from "effect/reactivity"; import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import * as Dialog from "../../components/ui/Dialog"; -import { useNavigationState, useRoute } from "@react-navigation/native"; +import { + StackActions, + useNavigation, + useNavigationState, + useRoute, +} from "@react-navigation/native"; import { threadPullRequestLinkMode } from "@t3tools/client-runtime/thread-pull-request-compatibility"; import { AuthOrchestrationOperateScope, @@ -16,7 +21,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 { useOpenWorkspaceDetail } from "../layout/useOpenWorkspaceDetail"; import { pullRequestComposerContext } from "../../lib/composerContext"; import { scopedThreadKey } from "../../lib/scopedEntities"; import { uuidv4 } from "../../lib/uuid"; @@ -82,14 +86,15 @@ export function usePullRequestThreads( const canOperate = useEnvironmentScope(target.environmentId, AuthOrchestrationOperateScope); const canAsk = !connected || canOperate; const canCheckout = useEnvironmentScope(target.environmentId, AuthSourceControlWriteScope); - const openDetail = useOpenWorkspaceDetail(); + const navigation = useNavigation(); const pending = useRef(false); const previousPrompts = useRef(new Map()); const openThread = (thread: LinkedThread) => { openThreadRoute({ environmentId: params.environmentId, threadId: thread.id }); }; - const openThreadRoute = (destination: ReactNavigation.RootParamList["Thread"]) => - openDetail({ name: "Thread", params: destination }); + const openThreadRoute = (destination: ReactNavigation.RootParamList["Thread"]) => { + navigation.dispatch(StackActions.push("Thread", destination)); + }; const [busy, setBusy] = useState(false); const ask = async (thread?: LinkedThread, task: PullRequestAgentTask = "ask") => { if (!pr || !canAsk || pending.current) return; diff --git a/apps/mobile/src/features/threads/NewTaskDraftRouteScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftRouteScreen.tsx index 4b77761b600a..abb35e793edc 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftRouteScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftRouteScreen.tsx @@ -90,7 +90,7 @@ const idleBranchCheckoutAtom = Atom.make(AsyncResult.initial) { const currentRoute = useRoute(); - const goBack = useWorkspaceBackNavigation("threads"); + const goBack = useWorkspaceBackNavigation(); const params = route.params ?? {}; const draftId = Array.isArray(params.draftId) ? params.draftId[0] : params.draftId; const projects = useProjects(); diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx index 0fe4f8e267a2..1c90d5fa2df1 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx @@ -163,7 +163,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 d0045bf5f284..0388bfb29929 100644 --- a/apps/mobile/src/features/threads/ThreadHeader.tsx +++ b/apps/mobile/src/features/threads/ThreadHeader.tsx @@ -22,7 +22,7 @@ export function ThreadHeader(props: { readonly inspectorMode: ThreadInspectorMode | null; readonly onOpenFilesInspector: () => void; }) { - const goBack = useWorkspaceBackNavigation("threads"); + const goBack = useWorkspaceBackNavigation(); const { toggleAuxiliaryPane, startNewTask } = useAdaptiveWorkspaceLayout(); const { panes } = useWorkspacePaneState(); const gitMenu = useThreadGitMenuDefinition(props.gitControls); diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index 6205fd128179..4091aefd5457 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -136,7 +136,7 @@ function firstRouteParam(value: string | string[] | undefined): string | null { const InspectorHeaderShownContext = createContext(false); function ThreadRouteFallbackLayout(props: { readonly children: ReactNode }) { - const goBack = useWorkspaceBackNavigation("threads"); + const goBack = useWorkspaceBackNavigation(); const inspectorHeaderShown = use(InspectorHeaderShownContext); return ( ) { } 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 eed0adbd8e59..170fcc3acc66 100644 --- a/apps/mobile/src/lib/android-workspace-navigation.ts +++ b/apps/mobile/src/lib/android-workspace-navigation.ts @@ -2,15 +2,14 @@ import type { NavigationState } from "@react-navigation/native"; import { isSettingsRoute } from "../features/settings/settings-navigation"; 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. */ @@ -60,12 +59,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) })); } // Split view: Thread, PullRequest, and a Settings page picked from the list swap the detail pane diff --git a/apps/mobile/src/native/useOpenPullRequest.ts b/apps/mobile/src/native/useOpenPullRequest.ts index 6608b7b5493d..ed080ade2f96 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/layout/workspace/useSelectWorkspaceDetail"; import { EnvironmentId, ThreadId } from "@t3tools/contracts"; import { useProjects, useThreadShell } from "../state/entities"; import { tryOpenExternalUrl } from "../lib/openExternalUrl"; @@ -17,7 +15,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) => { @@ -31,9 +28,7 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean ...destination, ...(destination.navigationUrl ? { navigationId: nextLinkNavigationId() } : {}), }); - // 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; };