Files
otunes/apps/mobile/app/(app)/_layout.tsx
T
joshuafhigginsandClaude Opus 5.5 Verified 886c8f0643 feat: playlist cover artwork
Playlists draw a 2x2 of their first albums automatically. Owners can pick
from random grids, reshuffle, or upload an image on web and mobile.
Covers are rendered once into Convex storage and redrawn only when the
playlist's albums change. Web and iPad sidebars show playlist covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 13:15:21 -05:00

145 lines
5.1 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 { 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>
<FavoritesProvider>
<AppIntentBridge />
<Stack screenOptions={stackOptions}>
<Stack.Screen
name="(tabs)"
options={{ headerShown: false, animation: "none" }}
/>
<Stack.Screen
name="settings"
options={{
headerShown: Platform.OS === "android",
title: "Settings",
presentation:
Platform.OS === "ios" ? "modal" : "fullScreenModal",
animation: "slide_from_bottom",
gestureEnabled: true,
gestureDirection: "vertical",
}}
/>
<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>
</AddToPlaylistProvider>
</PlaybackProvider>
</ConvexAuthRecovery>
)
}