fix: memoize home page, cache some data, fix headers on loading views
This commit is contained in:
1 parent
c511b9e537
commit
73ee87b5d1
17 files changed
+311
-64
No files matched your search
+17
-3
@@ -9,11 +9,18 @@
|
||||
"ios": {
|
||||
"supportsTablet": true,
|
||||
"bundleIdentifier": "com.abunchofknowitalls.otunes",
|
||||
"infoPlist": {}
|
||||
"infoPlist": {
|
||||
"ITSAppUsesNonExemptEncryption": false
|
||||
}
|
||||
},
|
||||
"android": {
|
||||
"package": "com.abunchofknowitalls.otunes",
|
||||
"predictiveBackGestureEnabled": false
|
||||
"predictiveBackGestureEnabled": false,
|
||||
"permissions": [
|
||||
"android.permission.MODIFY_AUDIO_SETTINGS",
|
||||
"android.permission.FOREGROUND_SERVICE",
|
||||
"android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
|
||||
]
|
||||
},
|
||||
"plugins": [
|
||||
"expo-router",
|
||||
@@ -28,6 +35,13 @@
|
||||
],
|
||||
"experiments": {
|
||||
"typedRoutes": true
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"router": {},
|
||||
"eas": {
|
||||
"projectId": "98b83584-d757-4da0-9bec-050c8336643f"
|
||||
}
|
||||
},
|
||||
"owner": "joshuafhiggins"
|
||||
}
|
||||
}
|
||||
@@ -38,14 +38,34 @@ export default function AlbumScreen() {
|
||||
useMediaMenuActions()
|
||||
|
||||
if (album === undefined || !config) {
|
||||
return <LoadingView />
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<LoadingView />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if (album === null) {
|
||||
return (
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>Album not found.</Text>
|
||||
</View>
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>
|
||||
Album not found.
|
||||
</Text>
|
||||
</View>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,14 +28,34 @@ export default function ArtistScreen() {
|
||||
const { handleArtistAction, isArtistFavorite: isArtistFav } = useMediaMenuActions()
|
||||
|
||||
if (data === undefined || !config) {
|
||||
return <LoadingView />
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<LoadingView />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if (data === null) {
|
||||
return (
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>Artist not found.</Text>
|
||||
</View>
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>
|
||||
Artist not found.
|
||||
</Text>
|
||||
</View>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+24
-1
@@ -17,7 +17,30 @@ export default function PlaylistsTabScreen() {
|
||||
const { openCreatePlaylist } = useAddToPlaylist()
|
||||
|
||||
if (playlists === undefined) {
|
||||
return <LoadingView />
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "Playlists",
|
||||
headerLargeTitle: true,
|
||||
headerRight: () => (
|
||||
<Pressable
|
||||
onPress={openCreatePlaylist}
|
||||
accessibilityLabel="New playlist"
|
||||
hitSlop={4}
|
||||
>
|
||||
<SymbolView
|
||||
name="plus"
|
||||
size={24}
|
||||
tintColor={colors.foreground}
|
||||
/>
|
||||
</Pressable>
|
||||
),
|
||||
}}
|
||||
/>
|
||||
<LoadingView />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -1,27 +1,23 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { memo, useRef } from "react"
|
||||
import { ScrollView, StyleSheet } from "react-native"
|
||||
|
||||
import { ContentCarousel } from "@/components/media-cards"
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { tabContentBottomInset } from "@/components/player"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import type { BrowseAlbum, PlaylistSummary } from "@otunes/convex/types"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
export default function HomeScreen() {
|
||||
const colors = useThemeColors()
|
||||
const recentlyAdded = useQuery(api.library.browseRecentlyAdded)
|
||||
const recentlyPlayed = useQuery(api.library.browseRecentlyPlayed)
|
||||
|
||||
if (recentlyAdded === undefined || recentlyPlayed === undefined) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
const HomeCarousels = memo(function HomeCarousels({
|
||||
recentlyAdded,
|
||||
recentlyPlayed,
|
||||
}: {
|
||||
recentlyAdded: BrowseAlbum[]
|
||||
recentlyPlayed: (BrowseAlbum | PlaylistSummary)[]
|
||||
}) {
|
||||
return (
|
||||
<ScrollView
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={[styles.content, { paddingBottom: tabContentBottomInset }]}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
<>
|
||||
{recentlyPlayed.length > 0 ? (
|
||||
<ContentCarousel
|
||||
title="Recently Played"
|
||||
@@ -34,6 +30,40 @@ export default function HomeScreen() {
|
||||
href="/recently-added"
|
||||
content={recentlyAdded}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
})
|
||||
|
||||
export default function HomeScreen() {
|
||||
const colors = useThemeColors()
|
||||
const recentlyAdded = useQuery(api.library.browseRecentlyAdded)
|
||||
const recentlyPlayed = useQuery(api.library.browseRecentlyPlayed)
|
||||
const cachedData = useRef<{
|
||||
recentlyAdded?: BrowseAlbum[]
|
||||
recentlyPlayed?: (BrowseAlbum | PlaylistSummary)[]
|
||||
}>({})
|
||||
|
||||
if (recentlyAdded !== undefined) {
|
||||
cachedData.current.recentlyAdded = recentlyAdded
|
||||
}
|
||||
if (recentlyPlayed !== undefined) {
|
||||
cachedData.current.recentlyPlayed = recentlyPlayed
|
||||
}
|
||||
|
||||
const added = recentlyAdded ?? cachedData.current.recentlyAdded
|
||||
const played = recentlyPlayed ?? cachedData.current.recentlyPlayed
|
||||
|
||||
if (added === undefined || played === undefined) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={[styles.content, { paddingBottom: tabContentBottomInset }]}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
<HomeCarousels recentlyAdded={added} recentlyPlayed={played} />
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -36,14 +36,34 @@ export default function PlaylistScreen() {
|
||||
const removeSongs = useMutation(api.playlists.removeSongsFromPlaylist)
|
||||
|
||||
if (playlist === undefined || !config) {
|
||||
return <LoadingView />
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<LoadingView />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if (playlist === null) {
|
||||
return (
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>Playlist not found.</Text>
|
||||
</View>
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>
|
||||
Playlist not found.
|
||||
</Text>
|
||||
</View>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -22,16 +22,34 @@ export default function FavoritesScreen() {
|
||||
const { handleSongAction } = useMediaMenuActions()
|
||||
|
||||
if (favorites === undefined || !config) {
|
||||
return <LoadingView />
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<LoadingView />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
if (favorites === null) {
|
||||
return (
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>
|
||||
Sign in to view your favorites.
|
||||
</Text>
|
||||
</View>
|
||||
<>
|
||||
<Stack.Screen
|
||||
options={{
|
||||
title: "",
|
||||
headerLargeTitle: false,
|
||||
}}
|
||||
/>
|
||||
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
||||
<Text style={{ color: colors.mutedForeground }}>
|
||||
Sign in to view your favorites.
|
||||
</Text>
|
||||
</View>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from "../../home/browse/playlists"
|
||||
@@ -12,7 +12,7 @@ import { api } from "@otunes/convex/_generated/api"
|
||||
import { appleMusic, spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const links = [
|
||||
{ title: "Playlists", href: "/playlists", icon: "music.note.list" as const },
|
||||
{ title: "Playlists", href: "/browse/playlists", icon: "music.note.list" as const },
|
||||
{ title: "Artists", href: "/browse/artists", icon: "mic.fill" as const },
|
||||
{ title: "Albums", href: "/browse/albums", icon: "square.stack" as const },
|
||||
{ title: "Songs", href: "/browse/songs", icon: "music.note" as const },
|
||||
|
||||
@@ -12,6 +12,7 @@ import { FavoritesProvider } from "@/contexts/favorites"
|
||||
import { PlaybackProvider } from "@/contexts/playback"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
import { useStackScreenOptions } from "@/lib/navigation"
|
||||
import { AppNavigationSync } from "@/lib/app-navigation"
|
||||
|
||||
const AUTH_HANDSHAKE_TIMEOUT_MS = 15_000
|
||||
|
||||
@@ -61,6 +62,7 @@ export default function AppLayout() {
|
||||
return (
|
||||
<ConvexAuthRecovery onInvalidate={invalidateSession}>
|
||||
<PlaybackProvider>
|
||||
<AppNavigationSync />
|
||||
<AddToPlaylistProvider>
|
||||
<FavoritesProvider>
|
||||
<Stack screenOptions={stackOptions}>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
import { Image, type ImageContentFit, type ImageStyle } from "expo-image"
|
||||
import { memo } from "react"
|
||||
import { StyleProp, StyleSheet, View, type ViewStyle } from "react-native"
|
||||
|
||||
import { useThemeColors } from "@/lib/theme"
|
||||
@@ -18,7 +19,7 @@ function placeholderIconSize(style: ImageStyle): number {
|
||||
return 48
|
||||
}
|
||||
|
||||
export function CoverImage({
|
||||
export const CoverImage = memo(function CoverImage({
|
||||
uri,
|
||||
hasCover,
|
||||
style,
|
||||
@@ -67,4 +68,4 @@ export function CoverImage({
|
||||
cachePolicy="disk"
|
||||
/>
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
import { memo, useCallback } from "react"
|
||||
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
|
||||
|
||||
import { CoverImage } from "@/components/cover-image"
|
||||
@@ -12,7 +13,7 @@ import { carouselCardWidth, radius, spacing, useThemeColors, useTwoColumnCardWid
|
||||
|
||||
type MediaItem = BrowseAlbum | BrowseArtist | PlaylistSummary
|
||||
|
||||
export function AlbumCard({
|
||||
export const AlbumCard = memo(function AlbumCard({
|
||||
album,
|
||||
width = carouselCardWidth,
|
||||
}: {
|
||||
@@ -23,12 +24,15 @@ export function AlbumCard({
|
||||
const { config } = useServerConfig()
|
||||
const colors = useThemeColors()
|
||||
const { handleAlbumAction, isAlbumFavorite } = useMediaMenuActions()
|
||||
const onPress = useCallback(() => {
|
||||
push(`/album/${album.id}`)
|
||||
}, [push, album.id])
|
||||
if (!config) return null
|
||||
|
||||
const card = (
|
||||
<Pressable
|
||||
style={[styles.card, { width }]}
|
||||
onPress={() => push(`/album/${album.id}`)}
|
||||
onPress={onPress}
|
||||
>
|
||||
<CoverImage
|
||||
uri={albumCoverUrl(config.serverUrl, album.id)}
|
||||
@@ -54,9 +58,9 @@ export function AlbumCard({
|
||||
{card}
|
||||
</MediaContextMenu>
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
export function ArtistCard({
|
||||
export const ArtistCard = memo(function ArtistCard({
|
||||
artist,
|
||||
width = carouselCardWidth,
|
||||
}: {
|
||||
@@ -67,12 +71,15 @@ export function ArtistCard({
|
||||
const { config } = useServerConfig()
|
||||
const colors = useThemeColors()
|
||||
const { handleArtistAction, isArtistFavorite } = useMediaMenuActions()
|
||||
const onPress = useCallback(() => {
|
||||
push(`/artist/${artist.id}`)
|
||||
}, [push, artist.id])
|
||||
if (!config) return null
|
||||
|
||||
const card = (
|
||||
<Pressable
|
||||
style={[styles.card, { width }]}
|
||||
onPress={() => push(`/artist/${artist.id}`)}
|
||||
onPress={onPress}
|
||||
>
|
||||
<CoverImage
|
||||
uri={artistCoverUrl(config.serverUrl, artist.id)}
|
||||
@@ -94,9 +101,9 @@ export function ArtistCard({
|
||||
{card}
|
||||
</MediaContextMenu>
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
export function PlaylistCard({
|
||||
export const PlaylistCard = memo(function PlaylistCard({
|
||||
playlist,
|
||||
width = carouselCardWidth,
|
||||
}: {
|
||||
@@ -107,12 +114,15 @@ export function PlaylistCard({
|
||||
const { config } = useServerConfig()
|
||||
const colors = useThemeColors()
|
||||
const { handlePlaylistAction } = useMediaMenuActions()
|
||||
const onPress = useCallback(() => {
|
||||
push(`/playlist/${playlist.id}`)
|
||||
}, [push, playlist.id])
|
||||
if (!config) return null
|
||||
|
||||
const card = (
|
||||
<Pressable
|
||||
style={[styles.card, { width }]}
|
||||
onPress={() => push(`/playlist/${playlist.id}`)}
|
||||
onPress={onPress}
|
||||
>
|
||||
<CoverImage
|
||||
uri={playlistCoverUrl(config.serverUrl, playlist.id)}
|
||||
@@ -137,13 +147,19 @@ export function PlaylistCard({
|
||||
{card}
|
||||
</MediaContextMenu>
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
function renderMediaItem(item: MediaItem, width: number) {
|
||||
const CarouselCard = memo(function CarouselCard({
|
||||
item,
|
||||
width,
|
||||
}: {
|
||||
item: MediaItem
|
||||
width: number
|
||||
}) {
|
||||
if ("owner" in item) return <PlaylistCard playlist={item} width={width} />
|
||||
if ("artist" in item) return <AlbumCard album={item} width={width} />
|
||||
return <ArtistCard artist={item} width={width} />
|
||||
}
|
||||
})
|
||||
|
||||
export function MediaGrid({ content }: { content: MediaItem[] }) {
|
||||
const cardWidth = useTwoColumnCardWidth()
|
||||
@@ -152,14 +168,14 @@ export function MediaGrid({ content }: { content: MediaItem[] }) {
|
||||
<View style={styles.grid}>
|
||||
{content.map((item) => (
|
||||
<View key={item.id} style={{ width: cardWidth }}>
|
||||
{renderMediaItem(item, cardWidth)}
|
||||
<CarouselCard item={item} width={cardWidth} />
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
export function ContentCarousel({
|
||||
function ContentCarouselBody({
|
||||
title,
|
||||
href,
|
||||
content,
|
||||
@@ -170,12 +186,17 @@ export function ContentCarousel({
|
||||
}) {
|
||||
const { push } = useAppNavigation()
|
||||
const colors = useThemeColors()
|
||||
const onHeaderPress = useCallback(() => {
|
||||
if (href) {
|
||||
push(href)
|
||||
}
|
||||
}, [href, push])
|
||||
|
||||
return (
|
||||
<View style={styles.section}>
|
||||
<Pressable
|
||||
disabled={!href}
|
||||
onPress={() => href && push(href)}
|
||||
onPress={onHeaderPress}
|
||||
style={styles.sectionHeader}
|
||||
>
|
||||
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>{title}</Text>
|
||||
@@ -187,13 +208,17 @@ export function ContentCarousel({
|
||||
contentContainerStyle={styles.carouselContent}
|
||||
>
|
||||
{content.map((item) => (
|
||||
<View key={item.id}>{renderMediaItem(item, carouselCardWidth)}</View>
|
||||
<View key={item.id}>
|
||||
<CarouselCard item={item} width={carouselCardWidth} />
|
||||
</View>
|
||||
))}
|
||||
</ScrollView>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
export const ContentCarousel = memo(ContentCarouselBody)
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
card: {
|
||||
gap: spacing.xs,
|
||||
|
||||
@@ -85,7 +85,13 @@ type PlaybackContextValue = {
|
||||
skipPrevious: () => Promise<void>
|
||||
}
|
||||
|
||||
type PlaybackActionsValue = Pick<
|
||||
PlaybackContextValue,
|
||||
"addToQueueNext" | "addToQueueLast" | "playShuffled"
|
||||
>
|
||||
|
||||
const PlaybackContext = createContext<PlaybackContextValue | null>(null)
|
||||
const PlaybackActionsContext = createContext<PlaybackActionsValue | null>(null)
|
||||
|
||||
export function usePlayback() {
|
||||
const context = useContext(PlaybackContext)
|
||||
@@ -95,6 +101,14 @@ export function usePlayback() {
|
||||
return context
|
||||
}
|
||||
|
||||
export function usePlaybackActions() {
|
||||
const context = useContext(PlaybackActionsContext)
|
||||
if (!context) {
|
||||
throw new Error("usePlaybackActions must be used within PlaybackProvider")
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
const convex = useConvex()
|
||||
const { config } = useServerConfig()
|
||||
@@ -589,6 +603,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
[convex, playShuffledMutation, setIsPlaying]
|
||||
)
|
||||
|
||||
const actionsValue = useMemo<PlaybackActionsValue>(
|
||||
() => ({
|
||||
addToQueueNext,
|
||||
addToQueueLast,
|
||||
playShuffled,
|
||||
}),
|
||||
[addToQueueNext, addToQueueLast, playShuffled]
|
||||
)
|
||||
|
||||
const value = useMemo<PlaybackContextValue>(
|
||||
() => ({
|
||||
queueSongIds: playback?.queueSongIds ?? [],
|
||||
@@ -675,6 +698,8 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
)
|
||||
|
||||
return (
|
||||
<PlaybackContext.Provider value={value}>{children}</PlaybackContext.Provider>
|
||||
<PlaybackActionsContext.Provider value={actionsValue}>
|
||||
<PlaybackContext.Provider value={value}>{children}</PlaybackContext.Provider>
|
||||
</PlaybackActionsContext.Provider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"cli": {
|
||||
"version": ">= 18.12.3",
|
||||
"appVersionSource": "remote"
|
||||
},
|
||||
"build": {
|
||||
"development": {
|
||||
"developmentClient": true,
|
||||
"distribution": "internal"
|
||||
},
|
||||
"preview": {
|
||||
"distribution": "internal"
|
||||
},
|
||||
"production": {
|
||||
"autoIncrement": true
|
||||
}
|
||||
},
|
||||
"submit": {
|
||||
"production": {}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { useCallback } from "react"
|
||||
import type { MediaAction } from "@/components/media-context-menu"
|
||||
import { useAddToPlaylist } from "@/contexts/add-to-playlist"
|
||||
import { useFavorites } from "@/contexts/favorites"
|
||||
import { usePlayback } from "@/contexts/playback"
|
||||
import { usePlaybackActions } from "@/contexts/playback"
|
||||
import { useAppNavigation } from "@/lib/app-navigation"
|
||||
import { showSuccess } from "@/lib/toast"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
@@ -30,7 +30,7 @@ export function useMediaMenuActions() {
|
||||
toggleArtistFavorite,
|
||||
toggleSongFavorite,
|
||||
} = useFavorites()
|
||||
const { addToQueueNext, addToQueueLast, playShuffled } = usePlayback()
|
||||
const { addToQueueNext, addToQueueLast, playShuffled } = usePlaybackActions()
|
||||
const favoriteSongs = useMutation(api.favorites.favoriteSongs)
|
||||
|
||||
const runQueueAction = useCallback(
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { useRouter, useSegments, type Href } from "expo-router"
|
||||
import { useCallback } from "react"
|
||||
|
||||
export const APP_TABS = ["home", "library", "search"] as const
|
||||
export type AppTab = (typeof APP_TABS)[number]
|
||||
|
||||
const TAB_SET = new Set<string>(APP_TABS)
|
||||
|
||||
const activeSegmentsRef: { current: string[] } = { current: [] }
|
||||
|
||||
export function getActiveTab(segments: string[]): AppTab {
|
||||
for (const segment of segments) {
|
||||
if (TAB_SET.has(segment)) {
|
||||
@@ -20,15 +23,39 @@ export function tabPath(tab: AppTab, path: string): Href {
|
||||
return `/(app)/(tabs)/${tab}${normalized}` as Href
|
||||
}
|
||||
|
||||
function getActiveTabFromRef(): AppTab {
|
||||
return getActiveTab(activeSegmentsRef.current)
|
||||
}
|
||||
|
||||
/** Mount once in the app shell so navigation helpers can read the active tab without re-rendering consumers. */
|
||||
export function AppNavigationSync() {
|
||||
const segments = useSegments()
|
||||
activeSegmentsRef.current = segments as string[]
|
||||
return null
|
||||
}
|
||||
|
||||
export function useAppNavigation() {
|
||||
const router = useRouter()
|
||||
const segments = useSegments()
|
||||
const tab = getActiveTab(segments)
|
||||
|
||||
const push = useCallback((path: string) => {
|
||||
router.push(tabPath(getActiveTabFromRef(), path))
|
||||
}, [router])
|
||||
|
||||
const replace = useCallback((path: string) => {
|
||||
router.replace(tabPath(getActiveTabFromRef(), path))
|
||||
}, [router])
|
||||
|
||||
const href = useCallback(
|
||||
(path: string) => tabPath(getActiveTabFromRef(), path),
|
||||
[]
|
||||
)
|
||||
|
||||
return {
|
||||
tab,
|
||||
href: (path: string) => tabPath(tab, path),
|
||||
push: (path: string) => router.push(tabPath(tab, path)),
|
||||
replace: (path: string) => router.replace(tabPath(tab, path)),
|
||||
get tab() {
|
||||
return getActiveTabFromRef()
|
||||
},
|
||||
href,
|
||||
push,
|
||||
replace,
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,9 @@ export const tabStackScreenTitles: Record<string, string> = {
|
||||
"browse/albums": "Albums",
|
||||
"browse/artists": "Artists",
|
||||
"browse/songs": "Songs",
|
||||
"browse/playlists": "Playlists",
|
||||
"recently-played": "Recently Played",
|
||||
"recently-added": "Recently Added",
|
||||
playlists: "Playlists",
|
||||
settings: "Settings",
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user