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 <noreply@anthropic.com>
This commit is contained in:
joshuafhigginsandClaude Opus 5.5 committed 2026-11-20 02:00:36 -05:00 Verified
1 parent 1f04f62b44
commit 2c6709d2ac
5 files changed
+103 -77

No files matched your search

@@ -1,5 +1,6 @@
import { Stack } from "expo-router" import { Stack } from "expo-router"
import { TabStackContext } from "@/lib/app-navigation"
import { renderTabScreenContent } from "@/components/tab-screen-content" import { renderTabScreenContent } from "@/components/tab-screen-content"
import { useStackScreenOptions, tabStackScreenTitles } from "@/lib/navigation" import { useStackScreenOptions, tabStackScreenTitles } from "@/lib/navigation"
import { tabletSections, TabletSectionContext } from "@/lib/tablet-navigation" import { tabletSections, TabletSectionContext } from "@/lib/tablet-navigation"
@@ -10,16 +11,18 @@ export default function TabletSectionLayout({ segment }: { segment: string }) {
return ( return (
<TabletSectionContext value={section}> <TabletSectionContext value={section}>
<Stack <TabStackContext value={section?.group}>
screenLayout={renderTabScreenContent} <Stack
screenOptions={({ route }) => ({ screenLayout={renderTabScreenContent}
...options, screenOptions={({ route }) => ({
title: ...options,
route.name === "index" title:
? section?.title route.name === "index"
: tabStackScreenTitles[route.name], ? section?.title
})} : tabStackScreenTitles[route.name],
/> })}
/>
</TabStackContext>
</TabletSectionContext> </TabletSectionContext>
) )
} }
+22 -19
View File
@@ -1,6 +1,7 @@
import { renderTabScreenContent } from "@/components/tab-screen-content" import { renderTabScreenContent } from "@/components/tab-screen-content"
import { Stack } from "expo-router" import { Stack } from "expo-router"
import { TabStackContext } from "@/lib/app-navigation"
import { ProfileHeaderButton } from "@/components/profile-header-button" import { ProfileHeaderButton } from "@/components/profile-header-button"
import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation" import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation"
@@ -12,24 +13,26 @@ export default function HomeLayout() {
const baseOptions = useStackScreenOptions({ title: "Home" }) const baseOptions = useStackScreenOptions({ title: "Home" })
return ( return (
<Stack <TabStackContext value="home">
screenLayout={renderTabScreenContent} <Stack
screenOptions={({ route }) => ({ screenLayout={renderTabScreenContent}
...baseOptions, screenOptions={({ route }) => ({
title: tabStackScreenTitles[route.name] ?? "Home", ...baseOptions,
headerRight: title: tabStackScreenTitles[route.name] ?? "Home",
route.name === "index" ? () => <ProfileHeaderButton /> : undefined, headerRight:
unstable_headerRightItems: route.name === "index" ? () => <ProfileHeaderButton /> : undefined,
route.name === "index" unstable_headerRightItems:
? () => [ route.name === "index"
{ ? () => [
type: "custom", {
element: <ProfileHeaderButton />, type: "custom",
hidesSharedBackground: true, element: <ProfileHeaderButton />,
}, hidesSharedBackground: true,
] },
: undefined, ]
})} : undefined,
/> })}
/>
</TabStackContext>
) )
} }
@@ -2,6 +2,7 @@ import { useTabletLayout } from "@/hooks/use-tablet-layout"
import { renderTabScreenContent } from "@/components/tab-screen-content" import { renderTabScreenContent } from "@/components/tab-screen-content"
import { Stack } from "expo-router" import { Stack } from "expo-router"
import { TabStackContext } from "@/lib/app-navigation"
import { ProfileHeaderButton } from "@/components/profile-header-button" import { ProfileHeaderButton } from "@/components/profile-header-button"
import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation" import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation"
@@ -14,26 +15,28 @@ export default function LibraryLayout() {
const baseOptions = useStackScreenOptions({ title: "Library" }) const baseOptions = useStackScreenOptions({ title: "Library" })
return ( return (
<Stack <TabStackContext value="library">
screenLayout={renderTabScreenContent} <Stack
screenOptions={({ route }) => ({ screenLayout={renderTabScreenContent}
...baseOptions, screenOptions={({ route }) => ({
title: ...baseOptions,
tabStackScreenTitles[route.name] ?? title:
(tabletLayout ? "Recently Added" : "Library"), tabStackScreenTitles[route.name] ??
headerRight: (tabletLayout ? "Recently Added" : "Library"),
route.name === "index" ? () => <ProfileHeaderButton /> : undefined, headerRight:
unstable_headerRightItems: route.name === "index" ? () => <ProfileHeaderButton /> : undefined,
route.name === "index" unstable_headerRightItems:
? () => [ route.name === "index"
{ ? () => [
type: "custom", {
element: <ProfileHeaderButton />, type: "custom",
hidesSharedBackground: true, element: <ProfileHeaderButton />,
}, hidesSharedBackground: true,
] },
: undefined, ]
})} : undefined,
/> })}
/>
</TabStackContext>
) )
} }
+22 -19
View File
@@ -1,6 +1,7 @@
import { renderTabScreenContent } from "@/components/tab-screen-content" import { renderTabScreenContent } from "@/components/tab-screen-content"
import { Stack } from "expo-router" import { Stack } from "expo-router"
import { TabStackContext } from "@/lib/app-navigation"
import { ProfileHeaderButton } from "@/components/profile-header-button" import { ProfileHeaderButton } from "@/components/profile-header-button"
import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation" import { tabStackScreenTitles, useStackScreenOptions } from "@/lib/navigation"
@@ -12,24 +13,26 @@ export default function SearchLayout() {
const baseOptions = useStackScreenOptions({ title: "Search" }) const baseOptions = useStackScreenOptions({ title: "Search" })
return ( return (
<Stack <TabStackContext value="search">
screenLayout={renderTabScreenContent} <Stack
screenOptions={({ route }) => ({ screenLayout={renderTabScreenContent}
...baseOptions, screenOptions={({ route }) => ({
title: tabStackScreenTitles[route.name] ?? "Search", ...baseOptions,
headerRight: title: tabStackScreenTitles[route.name] ?? "Search",
route.name === "index" ? () => <ProfileHeaderButton /> : undefined, headerRight:
unstable_headerRightItems: route.name === "index" ? () => <ProfileHeaderButton /> : undefined,
route.name === "index" unstable_headerRightItems:
? () => [ route.name === "index"
{ ? () => [
type: "custom", {
element: <ProfileHeaderButton />, type: "custom",
hidesSharedBackground: true, element: <ProfileHeaderButton />,
}, hidesSharedBackground: true,
] },
: undefined, ]
})} : undefined,
/> })}
/>
</TabStackContext>
) )
} }
+22 -8
View File
@@ -1,5 +1,5 @@
import { useRouter, useSegments, type Href } from "expo-router" import { useRouter, useSegments, type Href } from "expo-router"
import { useCallback } from "react" import { createContext, use, useCallback } from "react"
import { tabletSections } from "@/lib/tablet-navigation" 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 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<AppTab | undefined>(undefined)
function getActiveTabFromRef(): AppTab { function getActiveTabFromRef(): AppTab {
return getActiveTab(activeSegmentsRef.current) return getActiveTab(activeSegmentsRef.current)
} }
@@ -41,8 +48,15 @@ export function AppNavigationSync() {
return null return null
} }
/** Tab-aware navigation: paths resolve inside the caller's tab stack, falling back to the active tab outside one. */
export function useAppNavigation() { export function useAppNavigation() {
const router = useRouter() const router = useRouter()
const stackTab = use(TabStackContext)
const currentTab = useCallback(
() => stackTab ?? getActiveTabFromRef(),
[stackTab]
)
const push = useCallback( const push = useCallback(
(path: string) => { (path: string) => {
@@ -53,26 +67,26 @@ export function useAppNavigation() {
return return
} }
router.push(tabPath(getActiveTabFromRef(), path)) router.push(tabPath(currentTab(), path))
}, },
[router] [router, currentTab]
) )
const replace = useCallback( const replace = useCallback(
(path: string) => { (path: string) => {
router.replace(tabPath(getActiveTabFromRef(), path)) router.replace(tabPath(currentTab(), path))
}, },
[router] [router, currentTab]
) )
const href = useCallback( const href = useCallback(
(path: string) => tabPath(getActiveTabFromRef(), path), (path: string) => tabPath(currentTab(), path),
[] [currentTab]
) )
return { return {
get tab() { get tab() {
return getActiveTabFromRef() return currentTab()
}, },
href, href,
push, push,