- Cache queries and songs across mobile and web - Show cached content and skeletons during loading - Move shared playback and cache utilities into Convex client
104 lines
3.4 KiB
TypeScript
104 lines
3.4 KiB
TypeScript
import { useConvexAuth as useLocalConvexAuth } from "@convex-dev/auth/react"
|
|
import {
|
|
useConvexAuth as useServerConvexAuth,
|
|
useConvexConnectionState,
|
|
} from "convex/react"
|
|
import { Redirect, Stack } from "expo-router"
|
|
import { Platform } from "react-native"
|
|
import { useEffect } from "react"
|
|
|
|
import { ConvexAuthRecovery } from "@/components/convex-auth-recovery"
|
|
import { LoadingView } from "@/components/ui/primitives"
|
|
import { AddToPlaylistProvider } from "@/contexts/add-to-playlist"
|
|
import { useInvalidateAuthSession } from "@/contexts/convex-auth-session"
|
|
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
|
|
|
|
export default function AppLayout() {
|
|
const { config } = useServerConfig()
|
|
const localAuth = useLocalConvexAuth()
|
|
const serverAuth = useServerConvexAuth()
|
|
const { isWebSocketConnected } = useConvexConnectionState()
|
|
const { invalidateSession } = useInvalidateAuthSession()
|
|
const stackOptions = useStackScreenOptions()
|
|
|
|
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,
|
|
])
|
|
|
|
if (!config) {
|
|
return <Redirect href="/server" />
|
|
}
|
|
|
|
if (!localAuth || localAuth.isLoading) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
if (!localAuth.isAuthenticated) {
|
|
return <Redirect href="/signin" />
|
|
}
|
|
|
|
// 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>
|
|
<FavoritesProvider>
|
|
<Stack screenOptions={stackOptions}>
|
|
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
|
<Stack.Screen
|
|
name="settings"
|
|
options={{
|
|
headerShown: false,
|
|
presentation: Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
|
animation: "slide_from_bottom",
|
|
gestureEnabled: true,
|
|
gestureDirection: "vertical",
|
|
}}
|
|
/>
|
|
<Stack.Screen
|
|
name="player"
|
|
options={{
|
|
headerShown: false,
|
|
presentation: Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
|
animation: "slide_from_bottom",
|
|
gestureEnabled: true,
|
|
gestureDirection: "vertical",
|
|
fullScreenGestureEnabled: true,
|
|
}}
|
|
/>
|
|
</Stack>
|
|
</FavoritesProvider>
|
|
</AddToPlaylistProvider>
|
|
</PlaybackProvider>
|
|
</ConvexAuthRecovery>
|
|
)
|
|
}
|