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:
1 parent
1f04f62b44
commit
2c6709d2ac
5 files changed
+34
-8
No files matched your search
+3
@@ -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,6 +11,7 @@ export default function TabletSectionLayout({ segment }: { segment: string }) {
|
||||
|
||||
return (
|
||||
<TabletSectionContext value={section}>
|
||||
<TabStackContext value={section?.group}>
|
||||
<Stack
|
||||
screenLayout={renderTabScreenContent}
|
||||
screenOptions={({ route }) => ({
|
||||
@@ -20,6 +22,7 @@ export default function TabletSectionLayout({ segment }: { segment: string }) {
|
||||
: tabStackScreenTitles[route.name],
|
||||
})}
|
||||
/>
|
||||
</TabStackContext>
|
||||
</TabletSectionContext>
|
||||
)
|
||||
}
|
||||
@@ -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,6 +13,7 @@ export default function HomeLayout() {
|
||||
const baseOptions = useStackScreenOptions({ title: "Home" })
|
||||
|
||||
return (
|
||||
<TabStackContext value="home">
|
||||
<Stack
|
||||
screenLayout={renderTabScreenContent}
|
||||
screenOptions={({ route }) => ({
|
||||
@@ -31,5 +33,6 @@ export default function HomeLayout() {
|
||||
: undefined,
|
||||
})}
|
||||
/>
|
||||
</TabStackContext>
|
||||
)
|
||||
}
|
||||
@@ -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,6 +15,7 @@ export default function LibraryLayout() {
|
||||
const baseOptions = useStackScreenOptions({ title: "Library" })
|
||||
|
||||
return (
|
||||
<TabStackContext value="library">
|
||||
<Stack
|
||||
screenLayout={renderTabScreenContent}
|
||||
screenOptions={({ route }) => ({
|
||||
@@ -35,5 +37,6 @@ export default function LibraryLayout() {
|
||||
: undefined,
|
||||
})}
|
||||
/>
|
||||
</TabStackContext>
|
||||
)
|
||||
}
|
||||
@@ -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,6 +13,7 @@ export default function SearchLayout() {
|
||||
const baseOptions = useStackScreenOptions({ title: "Search" })
|
||||
|
||||
return (
|
||||
<TabStackContext value="search">
|
||||
<Stack
|
||||
screenLayout={renderTabScreenContent}
|
||||
screenOptions={({ route }) => ({
|
||||
@@ -31,5 +33,6 @@ export default function SearchLayout() {
|
||||
: undefined,
|
||||
})}
|
||||
/>
|
||||
</TabStackContext>
|
||||
)
|
||||
}
|
||||
@@ -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<AppTab | undefined>(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,
|
||||
|
||||
Reference in new issue
Block a user