Skip to content

Commit 314bc88

Browse files
Fix Workspaces tab reset by scanning all TabNavigators
1 parent 4a6c1b3 commit 314bc88

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

src/hooks/useRestoreWorkspacesTabOnNavigate.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,26 @@ function useRestoreWorkspacesTabOnNavigate() {
3535
return {};
3636
}
3737

38-
// Look inside TabNavigator for WORKSPACE_NAVIGATOR
39-
const rootTabRoute = rootState?.routes.findLast((route) => route.name === NAVIGATORS.TAB_NAVIGATOR);
40-
const rootTabState = getTabState(rootTabRoute);
41-
const workspaceNavigatorRoute = rootTabState?.routes?.find((route) => route.name === NAVIGATORS.WORKSPACE_NAVIGATOR);
38+
// Multiple TAB_NAVIGATOR instances can coexist in the root stack — when navigation from
39+
// inside an RHP targets a tab, linkTo PUSHes a fresh TabNavigator above the modal, and that
40+
// new instance's WORKSPACE_NAVIGATOR slot starts empty. Older instances kept alive by
41+
// ensureTabNavigatorRoutes still hold the previous workspace state, so flatten every
42+
// workspace route from every TabNavigator in stack order and take the most recent one.
43+
const lastWorkspaceRoute = (rootState?.routes ?? [])
44+
.filter((route) => route.name === NAVIGATORS.TAB_NAVIGATOR)
45+
.flatMap((tabNavigatorRoute) => {
46+
const workspaceNavigatorRoute = getTabState(tabNavigatorRoute)?.routes?.find((route) => route.name === NAVIGATORS.WORKSPACE_NAVIGATOR);
47+
const workspaceNavigatorState = workspaceNavigatorRoute?.state ?? (workspaceNavigatorRoute?.key ? getPreservedNavigatorState(workspaceNavigatorRoute.key) : undefined);
48+
return workspaceNavigatorState?.routes?.filter((route) => isWorkspaceNavigatorRouteName(route.name)) ?? [];
49+
})
50+
.at(-1);
4251

43-
if (workspaceNavigatorRoute) {
44-
const workspaceNavigatorState = workspaceNavigatorRoute.state ?? (workspaceNavigatorRoute.key ? getPreservedNavigatorState(workspaceNavigatorRoute.key) : undefined);
45-
const lastWorkspaceRoute = workspaceNavigatorState?.routes?.findLast((route) => isWorkspaceNavigatorRouteName(route.name));
46-
if (lastWorkspaceRoute) {
47-
const tabState = lastWorkspaceRoute.state ?? (lastWorkspaceRoute.key ? getPreservedNavigatorState(lastWorkspaceRoute.key) : undefined);
48-
return {lastWorkspacesTabNavigatorRoute: lastWorkspaceRoute, workspacesTabState: tabState, topmostFullScreenRoute};
49-
}
50-
return {topmostFullScreenRoute};
52+
if (lastWorkspaceRoute) {
53+
const tabState = lastWorkspaceRoute.state ?? (lastWorkspaceRoute.key ? getPreservedNavigatorState(lastWorkspaceRoute.key) : undefined);
54+
return {lastWorkspacesTabNavigatorRoute: lastWorkspaceRoute, workspacesTabState: tabState, topmostFullScreenRoute};
5155
}
5256

53-
// Fall back to session storage when no route exists in the navigation tree
57+
// Fall back to session storage when no workspace route exists anywhere in the navigation tree.
5458
const sessionRoute = getWorkspacesTabStateFromSessionStorage()
5559
?.routes?.findLast((route) => route.name === NAVIGATORS.WORKSPACE_NAVIGATOR)
5660
?.state?.routes?.findLast((route) => isWorkspaceNavigatorRouteName(route.name));

0 commit comments

Comments
 (0)