Files
otunes/apps/mobile/app/(app)/album/[id].tsx
T
joshuafhiggins 73c7283d4d Add Expo mobile app and extract shared Convex package
- Replace mobile placeholder with Expo Router app (tabs, playback, auth, library)
- Move Convex backend to @otunes/convex for web and mobile reuse
- Update web app imports and API routes to use shared package
2026-08-02 04:14:01 +00:00

181 lines
6.5 KiB
TypeScript

import { useQuery } from "convex/react"
import { Image } from "expo-image"
import { useLocalSearchParams, useRouter } from "expo-router"
import { useState } from "react"
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
import { SafeAreaView } from "react-native-safe-area-context"
import { Ionicons } from "@expo/vector-icons"
import { MediaActionsSheet, type MediaAction } from "@/components/media-actions-sheet"
import { ScreenHeader } from "@/components/screen-header"
import { Button, LoadingView } from "@/components/ui/primitives"
import { contentBottomInset } from "@/components/player"
import { useAddToPlaylist } from "@/contexts/add-to-playlist"
import { useFavorites } from "@/contexts/favorites"
import { usePlayback } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseAlbumTrack } from "@otunes/convex/types"
import { formatDuration, formatMetadata } from "@/lib/format"
import { albumCoverUrl } from "@/lib/urls"
import { radius, spacing, useThemeColors } from "@/lib/theme"
export default function AlbumScreen() {
const { id } = useLocalSearchParams<{ id: string }>()
const router = useRouter()
const colors = useThemeColors()
const { config } = useServerConfig()
const album = useQuery(api.library.browseAlbumDetail, { id: id as Id<"albums"> })
const { playAlbum, playAlbumShuffled, playFromSong } = usePlayback()
const { isAlbumFavorite, toggleAlbumFavorite } = useFavorites()
const { openAddAlbumToPlaylist } = useAddToPlaylist()
const [actionsOpen, setActionsOpen] = useState(false)
if (album === undefined || !config) {
return (
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
<ScreenHeader title="Album" />
<LoadingView />
</SafeAreaView>
)
}
if (album === null) {
return (
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
<ScreenHeader title="Album" />
<View style={styles.empty}>
<Text style={{ color: colors.mutedForeground }}>Album not found.</Text>
</View>
</SafeAreaView>
)
}
const songIds = album.tracks.map((track: BrowseAlbumTrack) => track.id)
const metadata = formatMetadata(album.genre, album.year)
function handleAlbumAction(action: MediaAction) {
if (!album) return
switch (action) {
case "playlist":
void openAddAlbumToPlaylist(album.id)
break
case "favorite":
void toggleAlbumFavorite(album.id)
break
default:
break
}
}
return (
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
<ScreenHeader
title={album.title}
right={
<Pressable onPress={() => setActionsOpen(true)} hitSlop={8}>
<Ionicons name="ellipsis-horizontal" size={22} color={colors.foreground} />
</Pressable>
}
/>
<ScrollView contentContainerStyle={{ paddingBottom: contentBottomInset }}>
<View style={styles.hero}>
<Image
source={{ uri: albumCoverUrl(config.serverUrl, album.id) }}
style={styles.cover}
contentFit="cover"
/>
<Text style={[styles.title, { color: colors.foreground }]}>{album.title}</Text>
<Pressable onPress={() => router.push(`/artist/${album.artist.id}`)}>
<Text style={[styles.artist, { color: colors.mutedForeground }]}>{album.artist.name}</Text>
</Pressable>
{metadata ? (
<Text style={{ color: colors.mutedForeground }}>{metadata}</Text>
) : null}
<View style={styles.actions}>
<Button title="Play" onPress={() => void playAlbum(album.id)} />
<Button title="Shuffle" variant="secondary" onPress={() => void playAlbumShuffled(album.id)} />
<Pressable onPress={() => void toggleAlbumFavorite(album.id)} hitSlop={8}>
<Ionicons
name={isAlbumFavorite(album.id) ? "star" : "star-outline"}
size={24}
color={isAlbumFavorite(album.id) ? colors.primary : colors.foreground}
/>
</Pressable>
</View>
</View>
{album.tracks.map((track: BrowseAlbumTrack, index: number) => (
<Pressable
key={track.id}
style={[styles.trackRow, { borderBottomColor: colors.border }]}
onPress={() => void playFromSong(track.id, songIds)}
>
<Text style={[styles.trackNumber, { color: colors.mutedForeground }]}>
{track.track || index + 1}
</Text>
<View style={styles.trackMeta}>
<Text style={[styles.trackTitle, { color: colors.foreground }]} numberOfLines={1}>
{track.title}
</Text>
{track.artists ? (
<Text style={{ color: colors.mutedForeground }} numberOfLines={1}>
{track.artists.join(", ")}
</Text>
) : null}
</View>
<Text style={{ color: colors.mutedForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</Pressable>
))}
</ScrollView>
<MediaActionsSheet
visible={actionsOpen}
title={album.title}
favorited={isAlbumFavorite(album.id)}
showNavigation={false}
onClose={() => setActionsOpen(false)}
onAction={handleAlbumAction}
/>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
safe: { flex: 1 },
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
hero: {
alignItems: "center",
padding: spacing.lg,
gap: spacing.sm,
},
cover: {
width: 220,
height: 220,
borderRadius: radius.lg,
backgroundColor: "#ddd",
},
title: { fontSize: 24, fontWeight: "800", textAlign: "center" },
artist: { fontSize: 16, fontWeight: "600" },
actions: {
flexDirection: "row",
alignItems: "center",
gap: spacing.sm,
marginTop: spacing.sm,
},
trackRow: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingHorizontal: spacing.lg,
paddingVertical: spacing.sm,
borderBottomWidth: StyleSheet.hairlineWidth,
},
trackNumber: { width: 24, textAlign: "center" },
trackMeta: { flex: 1, minWidth: 0 },
trackTitle: { fontSize: 15, fontWeight: "500" },
})