--------- Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com> Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/14
98 lines
3.1 KiB
TypeScript
98 lines
3.1 KiB
TypeScript
import { useConvexAuth as useLocalConvexAuth } from "@convex-dev/auth/react"
|
|
import { useConvexAuth as useServerConvexAuth } 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 { invalidateSession } = useInvalidateAuthSession()
|
|
const stackOptions = useStackScreenOptions()
|
|
|
|
useEffect(() => {
|
|
if (!localAuth?.isAuthenticated || serverAuth?.isAuthenticated) {
|
|
return
|
|
}
|
|
|
|
const timer = setTimeout(() => {
|
|
void invalidateSession()
|
|
}, AUTH_HANDSHAKE_TIMEOUT_MS)
|
|
|
|
return () => clearTimeout(timer)
|
|
}, [
|
|
invalidateSession,
|
|
localAuth?.isAuthenticated,
|
|
serverAuth?.isAuthenticated,
|
|
])
|
|
|
|
if (!config) {
|
|
return <Redirect href="/server" />
|
|
}
|
|
|
|
if (!localAuth || localAuth.isLoading) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
if (!localAuth.isAuthenticated) {
|
|
return <Redirect href="/signin" />
|
|
}
|
|
|
|
if (!serverAuth || serverAuth.isLoading || serverAuth.isRefreshing) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
if (!serverAuth.isAuthenticated) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
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>
|
|
)
|
|
}
|