Skip to content

Commit 3727278

Browse files
fix(mobile): Files reveal waits for a working directory and Back keeps the choice
The layout's render-time inspector reveal now gates Files on the same cwd resolution the Files screen uses, so a thread without one no longer hides the sidebar beside "Files unavailable". Reveals are remembered per route key, pruned to the routes still on the stack, so Review, then Files, then Back to Review keeps the user's choice. The focused route name comes from workspaceLocationFromState instead of a second navigation-state subscription. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 677ff55 commit 3727278

4 files changed

Lines changed: 73 additions & 29 deletions

File tree

‎apps/mobile/src/Stack.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,12 @@ export function workspaceLocationFromState(state: NavigationState) {
482482
? ({ ...state, routes, index: routes.length - 1 } as NavigationState)
483483
: state;
484484
const path = getPathFromState(effectiveState, navigationPathConfig);
485+
const route = effectiveState.routes[effectiveState.index];
485486
return {
486487
pathname: path.startsWith("/") ? path : `/${path}`,
487-
routeKey: effectiveState.routes[effectiveState.index]?.key,
488+
routeKey: route?.key,
489+
routeName: route?.name,
490+
routeKeys: effectiveState.routes.map((stackRoute) => stackRoute.key),
488491
};
489492
}
490493

@@ -518,6 +521,8 @@ function RootStackLayout(props: {
518521
<AdaptiveWorkspaceLayout
519522
pathname={workspaceLocation.pathname}
520523
workspaceRouteKey={workspaceLocation.routeKey}
524+
workspaceRouteName={workspaceLocation.routeName}
525+
workspaceRouteKeys={workspaceLocation.routeKeys}
521526
selectedPullRequestKey={selectedPullRequestKey(props.state)}
522527
>
523528
{props.children}

‎apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import type {
99
import { EnvironmentId, ThreadId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
1010
import { useAtomValue } from "@effect/atom-react";
1111
import { useFocusEffect } from "@react-navigation/native";
12-
import {
13-
NavigationContext,
14-
NavigationRouteContext,
15-
useNavigation,
16-
useNavigationState,
17-
} from "@react-navigation/native";
12+
import { NavigationContext, NavigationRouteContext, useNavigation } from "@react-navigation/native";
1813
import { use, useDeferredValue, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
1914
import { Keyboard, Platform, useWindowDimensions, View } from "react-native";
2015
import { KeyboardController, useKeyboardState } from "react-native-keyboard-controller";
@@ -41,6 +36,7 @@ import { scopedThreadKey } from "../../lib/scopedEntities";
4136
import type { ThreadListFilters } from "../../persistence/mobile-preferences";
4237
import { appAtomRegistry } from "../../state/atom-registry";
4338
import { useProjects } from "../../state/entities";
39+
import { useThreadCwd } from "../../state/use-selected-thread-worktree";
4440
import { environmentThreadShells } from "../../state/threads";
4541
import { mobilePreferencesAtom } from "../../state/preferences";
4642
import {
@@ -132,6 +128,8 @@ export function AdaptiveWorkspaceLayout(props: {
132128
readonly children: ReactNode;
133129
readonly pathname: string;
134130
readonly workspaceRouteKey: string | undefined;
131+
readonly workspaceRouteName: string | undefined;
132+
readonly workspaceRouteKeys: ReadonlyArray<string>;
135133
readonly selectedPullRequestKey?: string | null;
136134
}) {
137135
const preferencesResult = useAtomValue(mobilePreferencesAtom);
@@ -158,6 +156,8 @@ function AdaptiveWorkspaceLayoutContent(
158156
readonly children: ReactNode;
159157
readonly pathname: string;
160158
readonly workspaceRouteKey: string | undefined;
159+
readonly workspaceRouteName: string | undefined;
160+
readonly workspaceRouteKeys: ReadonlyArray<string>;
161161
readonly selectedPullRequestKey?: string | null;
162162
} & {
163163
readonly projectGroupingMode: SidebarProjectGroupingMode;
@@ -312,31 +312,49 @@ function AdaptiveWorkspaceLayoutContent(
312312
const activeThread = parseActiveThreadPath(pathname);
313313
const environmentId = activeThread?.environmentId ?? null;
314314
const threadId = activeThread?.threadId ?? null;
315-
const selectedThreadKey = (() => {
315+
const selectedThreadRef = (() => {
316316
if (environmentId === null || threadId === null) {
317317
return null;
318318
}
319319
try {
320-
return scopedThreadKey(EnvironmentId.make(environmentId), ThreadId.make(threadId));
320+
return {
321+
environmentId: EnvironmentId.make(environmentId),
322+
threadId: ThreadId.make(threadId),
323+
};
321324
} catch {
322325
return null;
323326
}
324327
})();
325-
const workspaceRouteName = useNavigationState(
326-
(state) => state?.routes.find((route) => route.key === props.workspaceRouteKey)?.name,
328+
const selectedThreadKey =
329+
selectedThreadRef === null
330+
? null
331+
: scopedThreadKey(selectedThreadRef.environmentId, selectedThreadRef.threadId);
332+
const isFilesRoute = props.workspaceRouteName === "ThreadFiles";
333+
// Same resolution the Files screen uses: without a cwd it shows "Files unavailable" and
334+
// registers no inspector, so revealing would only hide the sidebar beside that screen.
335+
const filesCwd = useThreadCwd(isFilesRoute && fileInspector.supported ? selectedThreadRef : null);
336+
// Opening a review or the files route reveals its inspector once per route and thread.
337+
// Routes still on the stack remember their reveal, so going back to one keeps the user's choice.
338+
const revealsInspector =
339+
props.workspaceRouteName === "ThreadReview" || (isFilesRoute && filesCwd !== null);
340+
const [revealedInspectors, setRevealedInspectors] = useState<ReadonlyMap<string, string>>(
341+
() => new Map(),
327342
);
328-
// Opening a review or the files route reveals its inspector once per route
329-
// and thread. Coming back to that route from a screen pushed over it keeps
330-
// the user's choice, so the last revealed key is not cleared on other routes.
331-
const inspectorRevealKey =
332-
(workspaceRouteName === "ThreadReview" ||
333-
(workspaceRouteName === "ThreadFiles" && fileInspector.supported)) &&
334-
selectedThreadKey !== null
335-
? `${props.workspaceRouteKey}:${selectedThreadKey}`
336-
: null;
337-
const [revealedInspectorKey, setRevealedInspectorKey] = useState<string | null>(null);
338-
if (inspectorRevealKey !== null && inspectorRevealKey !== revealedInspectorKey) {
339-
setRevealedInspectorKey(inspectorRevealKey);
343+
if (
344+
revealsInspector &&
345+
props.workspaceRouteKey !== undefined &&
346+
selectedThreadKey !== null &&
347+
revealedInspectors.get(props.workspaceRouteKey) !== selectedThreadKey
348+
) {
349+
const next = new Map<string, string>();
350+
for (const routeKey of props.workspaceRouteKeys) {
351+
const revealedThreadKey = revealedInspectors.get(routeKey);
352+
if (revealedThreadKey !== undefined) {
353+
next.set(routeKey, revealedThreadKey);
354+
}
355+
}
356+
next.set(props.workspaceRouteKey, selectedThreadKey);
357+
setRevealedInspectors(next);
340358
setFocusedAuxiliaryPaneRole("inspector");
341359
setFileInspectorPreferredVisible(true);
342360
}

‎apps/mobile/src/state/use-selected-thread-worktree.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
12
import { useMemo } from "react";
23

3-
import { useSelectedThreadWorktreePath } from "./use-thread-detail";
4+
import { useProject, useQuietThreadShell } from "./entities";
5+
import { useSelectedThreadWorktreePath, useThreadWorktreePath } from "./use-thread-detail";
46
import { useThreadSelection } from "./use-thread-selection";
57
import { resolvePreferredThreadWorktreePath } from "../features/terminal/terminalLaunchContext";
68

@@ -22,3 +24,20 @@ export function useSelectedThreadWorktree() {
2224
selectedThreadCwd: selectedThreadWorktreePath ?? selectedThreadProject?.workspaceRoot ?? null,
2325
};
2426
}
27+
28+
/** The selected-thread cwd resolution for a thread that is not the current route's. */
29+
export function useThreadCwd(ref: ScopedThreadRef | null): string | null {
30+
const shell = useQuietThreadShell(ref);
31+
const detailWorktreePath = useThreadWorktreePath(ref);
32+
const project = useProject(
33+
shell === null ? null : { environmentId: shell.environmentId, projectId: shell.projectId },
34+
);
35+
return (
36+
resolvePreferredThreadWorktreePath({
37+
threadShellWorktreePath: shell?.worktreePath ?? null,
38+
threadDetailWorktreePath: detailWorktreePath,
39+
}) ??
40+
project?.workspaceRoot ??
41+
null
42+
);
43+
}

‎apps/mobile/src/state/use-thread-detail.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ export function useSelectedThreadVisibleTurnItems(): OrchestrationV2ThreadProjec
9292
const EMPTY_WORKTREE_PATH_ATOM = Atom.make<string | null>(null);
9393
const EMPTY_PENDING_REQUESTS_ATOM = Atom.make<PendingThreadRequests | null>(null);
9494

95-
export function useSelectedThreadWorktreePath() {
96-
const { selectedThreadDetailRef } = useThreadSelection();
95+
export function useThreadWorktreePath(ref: ScopedThreadRef | null) {
9796
return useAtomValue(
98-
selectedThreadDetailRef === null
99-
? EMPTY_WORKTREE_PATH_ATOM
100-
: environmentThreadDetails.worktreePathAtom(selectedThreadDetailRef),
97+
ref === null ? EMPTY_WORKTREE_PATH_ATOM : environmentThreadDetails.worktreePathAtom(ref),
10198
);
10299
}
103100

101+
export function useSelectedThreadWorktreePath() {
102+
const { selectedThreadDetailRef } = useThreadSelection();
103+
return useThreadWorktreePath(selectedThreadDetailRef);
104+
}
105+
104106
export function useSelectedThreadPendingRequests() {
105107
const { selectedThreadDetailRef } = useThreadSelection();
106108
return useAtomValue(

0 commit comments

Comments
 (0)