@@ -9,12 +9,7 @@ import type {
99import { EnvironmentId , ThreadId , type SidebarProjectGroupingMode } from "@t3tools/contracts" ;
1010import { useAtomValue } from "@effect/atom-react" ;
1111import { 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" ;
1813import { use , useDeferredValue , useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
1914import { Keyboard , Platform , useWindowDimensions , View } from "react-native" ;
2015import { KeyboardController , useKeyboardState } from "react-native-keyboard-controller" ;
@@ -41,6 +36,7 @@ import { scopedThreadKey } from "../../lib/scopedEntities";
4136import type { ThreadListFilters } from "../../persistence/mobile-preferences" ;
4237import { appAtomRegistry } from "../../state/atom-registry" ;
4338import { useProjects } from "../../state/entities" ;
39+ import { useThreadCwd } from "../../state/use-selected-thread-worktree" ;
4440import { environmentThreadShells } from "../../state/threads" ;
4541import { mobilePreferencesAtom } from "../../state/preferences" ;
4642import {
@@ -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 }
0 commit comments