Stack.Toolbar turns the header on, which stacked a second title bar above the custom one on Android. Render it on iOS only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
6.3 KiB
TypeScript
167 lines
6.3 KiB
TypeScript
import { router, type Href } from "expo-router"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import {
|
|
incomingMediaLink,
|
|
subscribeIncomingMediaLink,
|
|
takeIncomingMediaLink,
|
|
} from "@/lib/incoming-media-link"
|
|
import { useConvexAuth as useLocalConvexAuth } from "@convex-dev/auth/react"
|
|
import {
|
|
useConvexAuth as useServerConvexAuth,
|
|
useConvexConnectionState,
|
|
} from "convex/react"
|
|
import { Stack } from "expo-router"
|
|
import { Platform } from "react-native"
|
|
import { useEffect, useSyncExternalStore } from "react"
|
|
|
|
import { AppIntentBridge } from "@/components/app-intent-bridge"
|
|
import { ConvexAuthRecovery } from "@/components/convex-auth-recovery"
|
|
import { AddToPlaylistProvider } from "@/contexts/add-to-playlist"
|
|
import { DownloadsProvider } from "@/contexts/downloads"
|
|
import { useInvalidateAuthSession } from "@/contexts/convex-auth-session"
|
|
import { FavoritesProvider } from "@/contexts/favorites"
|
|
import { PlaybackProvider } from "@/contexts/playback"
|
|
import { useStackScreenOptions } from "@/lib/navigation"
|
|
import { AppNavigationSync } from "@/lib/app-navigation"
|
|
|
|
const AUTH_HANDSHAKE_TIMEOUT_MS = 15_000
|
|
|
|
export default function AppLayout() {
|
|
const localAuth = useLocalConvexAuth()
|
|
const serverAuth = useServerConvexAuth()
|
|
const { isWebSocketConnected } = useConvexConnectionState()
|
|
const { invalidateSession } = useInvalidateAuthSession()
|
|
const stackOptions = useStackScreenOptions()
|
|
const { config } = useServerConfig()
|
|
const pendingDestination = useSyncExternalStore(
|
|
subscribeIncomingMediaLink,
|
|
() => (config ? incomingMediaLink(config.serverUrl) : null),
|
|
() => null
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (!config || !pendingDestination) return
|
|
const destination = takeIncomingMediaLink(config.serverUrl)
|
|
if (destination) router.replace(destination as Href)
|
|
}, [config, pendingDestination])
|
|
|
|
useEffect(() => {
|
|
if (
|
|
!localAuth?.isAuthenticated ||
|
|
serverAuth?.isAuthenticated ||
|
|
!isWebSocketConnected
|
|
) {
|
|
return
|
|
}
|
|
|
|
// Only a reachable server that never accepts the session counts as a
|
|
// failed handshake; offline launches keep the session and cached data.
|
|
const timer = setTimeout(() => {
|
|
void invalidateSession()
|
|
}, AUTH_HANDSHAKE_TIMEOUT_MS)
|
|
|
|
return () => clearTimeout(timer)
|
|
}, [
|
|
invalidateSession,
|
|
isWebSocketConnected,
|
|
localAuth?.isAuthenticated,
|
|
serverAuth?.isAuthenticated,
|
|
])
|
|
|
|
// Convex holds queries until the stored token is confirmed, so the app can
|
|
// render cached data while the server handshake is still in progress.
|
|
|
|
return (
|
|
<ConvexAuthRecovery onInvalidate={invalidateSession}>
|
|
<PlaybackProvider>
|
|
<AppNavigationSync />
|
|
<AddToPlaylistProvider>
|
|
<DownloadsProvider>
|
|
<FavoritesProvider>
|
|
<AppIntentBridge />
|
|
<Stack screenOptions={stackOptions}>
|
|
<Stack.Screen
|
|
name="(tabs)"
|
|
options={{ headerShown: false, animation: "none" }}
|
|
/>
|
|
<Stack.Screen
|
|
name="settings"
|
|
options={{
|
|
// iOS gets a native header with a close button; Android
|
|
// draws its own title bar in the screen.
|
|
headerShown: Platform.OS === "ios",
|
|
title: "Settings",
|
|
headerLargeTitle: false,
|
|
headerTransparent: false,
|
|
headerShadowVisible: false,
|
|
headerStyle: { backgroundColor: "#000" },
|
|
// Matches the inline title size of Apple Music's settings.
|
|
headerTitleStyle: { fontSize: 20, fontWeight: "700" },
|
|
presentation:
|
|
Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
|
animation: "slide_from_bottom",
|
|
gestureEnabled: true,
|
|
gestureDirection: "vertical",
|
|
}}
|
|
>
|
|
{/* Stack.Toolbar forces the header on, so keep it iOS-only. */}
|
|
{Platform.OS === "ios" ? (
|
|
<Stack.Toolbar placement="right">
|
|
<Stack.Toolbar.Button
|
|
icon="xmark"
|
|
accessibilityLabel="Close settings"
|
|
onPress={() => router.back()}
|
|
/>
|
|
</Stack.Toolbar>
|
|
) : null}
|
|
</Stack.Screen>
|
|
<Stack.Screen
|
|
name="create-account"
|
|
options={{
|
|
title: "Create account",
|
|
presentation: "modal",
|
|
headerLargeTitle: false,
|
|
headerTransparent: false,
|
|
headerStyle: { backgroundColor: "#000" },
|
|
headerTintColor: "#fff",
|
|
contentStyle: { backgroundColor: "#000" },
|
|
}}
|
|
/>
|
|
<Stack.Screen
|
|
name="playlist-cover"
|
|
options={{
|
|
title: "Cover",
|
|
headerTitleAlign: "center",
|
|
presentation:
|
|
Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
|
animation: "slide_from_bottom",
|
|
headerLargeTitle: false,
|
|
headerTransparent: false,
|
|
headerShadowVisible: false,
|
|
headerStyle: { backgroundColor: "#000" },
|
|
headerTintColor: "#fff",
|
|
contentStyle: { backgroundColor: "#000" },
|
|
}}
|
|
/>
|
|
<Stack.Screen
|
|
name="player"
|
|
options={{
|
|
headerShown: false,
|
|
// Keep the library visible behind the custom swipe animation.
|
|
presentation: "transparentModal",
|
|
contentStyle: { backgroundColor: "transparent" },
|
|
animation: "slide_from_bottom",
|
|
gestureEnabled: true,
|
|
gestureDirection: "vertical",
|
|
fullScreenGestureEnabled: true,
|
|
}}
|
|
/>
|
|
</Stack>
|
|
</FavoritesProvider>
|
|
</DownloadsProvider>
|
|
</AddToPlaylistProvider>
|
|
</PlaybackProvider>
|
|
</ConvexAuthRecovery>
|
|
)
|
|
}
|