From 2c6709d2ac0d7d2ffed31e722d3b2811d27f0fc4 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Fri, 20 Nov 2026 02:00:36 -0500 Subject: [PATCH] fix(mobile): open library albums in the library tab Album cards built their link from a global active-tab ref read at render time. Cards on the library page could render before the ref caught up with the tab switch, so their links pointed at the home tab. Each tab stack now provides its own tab through context, and navigation resolves against it. Co-Authored-By: Claude Opus 5.5 --- .../_layout.tsx | 23 +++++----- apps/mobile/app/(app)/(tabs)/home/_layout.tsx | 41 +++++++++-------- .../app/(app)/(tabs)/library/_layout.tsx | 45 ++++++++++--------- .../app/(app)/(tabs)/search/_layout.tsx | 41 +++++++++-------- apps/mobile/lib/app-navigation.ts | 30 +++++++++---- 5 files changed, 103 insertions(+), 77 deletions(-) diff --git a/apps/mobile/app/(app)/(tabs)/(albums,artists,songs,playlists,favorites,recent,downloads)/_layout.tsx b/apps/mobile/app/(app)/(tabs)/(albums,artists,songs,playlists,favorites,recent,downloads)/_layout.tsx index 9453ea3..3b3fa6e 100644 --- a/apps/mobile/app/(app)/(tabs)/(albums,artists,songs,playlists,favorites,recent,downloads)/_layout.tsx +++ b/apps/mobile/app/(app)/(tabs)/(albums,artists,songs,playlists,favorites,recent,downloads)/_layout.tsx @@ -1,5 +1,6 @@ import { Stack } from "expo-router" +import { TabStackContext } from "@/lib/app-navigation" import { renderTabScreenContent } from "@/components/tab-screen-content" import { useStackScreenOptions, tabStackScreenTitles } from "@/lib/navigation" import { tabletSections, TabletSectionContext } from "@/lib/tablet-navigation" @@ -10,16 +11,18 @@ export default function TabletSectionLayout({ segment }: { segment: string }) { return ( - ({ - ...options, - title: - route.name === "index" - ? section?.title - : tabStackScreenTitles[route.name], - })} - /> + + ({ + ...options, + title: + route.name === "index" + ? section?.title + : tabStackScreenTitles[route.name], + })} + /> + ) } diff --git a/apps/mobile/app/(app)/(tabs)/home/_layout.tsx b/apps/mobile/app/(app)/(tabs)/home/_layout.tsx index 75da6ca..40072a4 100644 --- a/apps/mobile/app/(app)/(tabs)/home/_layout.tsx +++ b/apps/mobile/app/(app)/(tabs)/home/_layout.tsx @@ -1,6 +1,7 @@ import { renderTabScreenContent } from "@/components/tab-screen-content" import { Stack } from "expo-router" +import { TabStackContext } from "@/lib/app-navigation" import { ProfileHeaderButton } from "@/components/profile-header-button" import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation" @@ -12,24 +13,26 @@ export default function HomeLayout() { const baseOptions = useStackScreenOptions({ title: "Home" }) return ( - ({ - ...baseOptions, - title: tabStackScreenTitles[route.name] ?? "Home", - headerRight: - route.name === "index" ? () => : undefined, - unstable_headerRightItems: - route.name === "index" - ? () => [ - { - type: "custom", - element: , - hidesSharedBackground: true, - }, - ] - : undefined, - })} - /> + + ({ + ...baseOptions, + title: tabStackScreenTitles[route.name] ?? "Home", + headerRight: + route.name === "index" ? () => : undefined, + unstable_headerRightItems: + route.name === "index" + ? () => [ + { + type: "custom", + element: , + hidesSharedBackground: true, + }, + ] + : undefined, + })} + /> + ) } diff --git a/apps/mobile/app/(app)/(tabs)/library/_layout.tsx b/apps/mobile/app/(app)/(tabs)/library/_layout.tsx index 50e70aa..48d626f 100644 --- a/apps/mobile/app/(app)/(tabs)/library/_layout.tsx +++ b/apps/mobile/app/(app)/(tabs)/library/_layout.tsx @@ -2,6 +2,7 @@ import { useTabletLayout } from "@/hooks/use-tablet-layout" import { renderTabScreenContent } from "@/components/tab-screen-content" import { Stack } from "expo-router" +import { TabStackContext } from "@/lib/app-navigation" import { ProfileHeaderButton } from "@/components/profile-header-button" import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation" @@ -14,26 +15,28 @@ export default function LibraryLayout() { const baseOptions = useStackScreenOptions({ title: "Library" }) return ( - ({ - ...baseOptions, - title: - tabStackScreenTitles[route.name] ?? - (tabletLayout ? "Recently Added" : "Library"), - headerRight: - route.name === "index" ? () => : undefined, - unstable_headerRightItems: - route.name === "index" - ? () => [ - { - type: "custom", - element: , - hidesSharedBackground: true, - }, - ] - : undefined, - })} - /> + + ({ + ...baseOptions, + title: + tabStackScreenTitles[route.name] ?? + (tabletLayout ? "Recently Added" : "Library"), + headerRight: + route.name === "index" ? () => : undefined, + unstable_headerRightItems: + route.name === "index" + ? () => [ + { + type: "custom", + element: , + hidesSharedBackground: true, + }, + ] + : undefined, + })} + /> + ) } diff --git a/apps/mobile/app/(app)/(tabs)/search/_layout.tsx b/apps/mobile/app/(app)/(tabs)/search/_layout.tsx index 0b1efce..056472d 100644 --- a/apps/mobile/app/(app)/(tabs)/search/_layout.tsx +++ b/apps/mobile/app/(app)/(tabs)/search/_layout.tsx @@ -1,6 +1,7 @@ import { renderTabScreenContent } from "@/components/tab-screen-content" import { Stack } from "expo-router" +import { TabStackContext } from "@/lib/app-navigation" import { ProfileHeaderButton } from "@/components/profile-header-button" import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation" @@ -12,24 +13,26 @@ export default function SearchLayout() { const baseOptions = useStackScreenOptions({ title: "Search" }) return ( - ({ - ...baseOptions, - title: tabStackScreenTitles[route.name] ?? "Search", - headerRight: - route.name === "index" ? () => : undefined, - unstable_headerRightItems: - route.name === "index" - ? () => [ - { - type: "custom", - element: , - hidesSharedBackground: true, - }, - ] - : undefined, - })} - /> + + ({ + ...baseOptions, + title: tabStackScreenTitles[route.name] ?? "Search", + headerRight: + route.name === "index" ? () => : undefined, + unstable_headerRightItems: + route.name === "index" + ? () => [ + { + type: "custom", + element: , + hidesSharedBackground: true, + }, + ] + : undefined, + })} + /> + ) } diff --git a/apps/mobile/lib/app-navigation.ts b/apps/mobile/lib/app-navigation.ts index 2cbf052..4fad583 100644 --- a/apps/mobile/lib/app-navigation.ts +++ b/apps/mobile/lib/app-navigation.ts @@ -1,5 +1,5 @@ import { useRouter, useSegments, type Href } from "expo-router" -import { useCallback } from "react" +import { createContext, use, useCallback } from "react" import { tabletSections } from "@/lib/tablet-navigation" @@ -30,6 +30,13 @@ export function tabPath(tab: AppTab, path: string): Href { return `/(app)/(tabs)/${tab}${normalized}` as Href } +/** + * The tab whose stack a screen lives in. Each tab stack layout provides it so + * links resolve against the screen's own tab, even when the global segments + * ref has not caught up with a tab switch yet. + */ +export const TabStackContext = createContext(undefined) + function getActiveTabFromRef(): AppTab { return getActiveTab(activeSegmentsRef.current) } @@ -41,8 +48,15 @@ export function AppNavigationSync() { return null } +/** Tab-aware navigation: paths resolve inside the caller's tab stack, falling back to the active tab outside one. */ export function useAppNavigation() { const router = useRouter() + const stackTab = use(TabStackContext) + + const currentTab = useCallback( + () => stackTab ?? getActiveTabFromRef(), + [stackTab] + ) const push = useCallback( (path: string) => { @@ -53,26 +67,26 @@ export function useAppNavigation() { return } - router.push(tabPath(getActiveTabFromRef(), path)) + router.push(tabPath(currentTab(), path)) }, - [router] + [router, currentTab] ) const replace = useCallback( (path: string) => { - router.replace(tabPath(getActiveTabFromRef(), path)) + router.replace(tabPath(currentTab(), path)) }, - [router] + [router, currentTab] ) const href = useCallback( - (path: string) => tabPath(getActiveTabFromRef(), path), - [] + (path: string) => tabPath(currentTab(), path), + [currentTab] ) return { get tab() { - return getActiveTabFromRef() + return currentTab() }, href, push,