- 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
116 lines
4.1 KiB
TypeScript
116 lines
4.1 KiB
TypeScript
import { useQuery } from "convex/react"
|
|
import { Image } from "expo-image"
|
|
import { useState } from "react"
|
|
import { ScrollView, StyleSheet, Text, View } from "react-native"
|
|
import { SafeAreaView } from "react-native-safe-area-context"
|
|
|
|
import { MediaActionsSheet, type MediaAction } from "@/components/media-actions-sheet"
|
|
import { ScreenHeader } from "@/components/screen-header"
|
|
import { Button, LoadingView } from "@/components/ui/primitives"
|
|
import { contentBottomInset, SongRow } 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 { BrowseSong } from "@otunes/convex/types"
|
|
import { playlistCoverUrl } from "@/lib/urls"
|
|
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
export default function FavoritesScreen() {
|
|
const colors = useThemeColors()
|
|
const { config } = useServerConfig()
|
|
const favorites = useQuery(api.favorites.browseFavoritesPlaylistDetail)
|
|
const { playInOrder, playShuffled, playFromSong } = usePlayback()
|
|
const { openAddToPlaylist } = useAddToPlaylist()
|
|
const { isSongFavorite, toggleSongFavorite } = useFavorites()
|
|
const [actionSong, setActionSong] = useState<BrowseSong | null>(null)
|
|
|
|
if (favorites === undefined || !config) {
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader title="Favorites" />
|
|
<LoadingView />
|
|
</SafeAreaView>
|
|
)
|
|
}
|
|
|
|
if (favorites === null) {
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader title="Favorites" />
|
|
<View style={styles.empty}>
|
|
<Text style={{ color: colors.mutedForeground }}>Sign in to view your favorites.</Text>
|
|
</View>
|
|
</SafeAreaView>
|
|
)
|
|
}
|
|
|
|
const songIds = favorites.songs.map((song: BrowseSong) => song.id)
|
|
|
|
function handleSongAction(action: MediaAction) {
|
|
if (!actionSong) return
|
|
switch (action) {
|
|
case "playlist":
|
|
openAddToPlaylist([actionSong.id])
|
|
break
|
|
case "favorite":
|
|
void toggleSongFavorite(actionSong.id)
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader title="Favorites" />
|
|
<ScrollView contentContainerStyle={{ paddingBottom: contentBottomInset }}>
|
|
<View style={styles.hero}>
|
|
<Image
|
|
source={{ uri: playlistCoverUrl(config.serverUrl, "favorites") }}
|
|
style={styles.cover}
|
|
contentFit="cover"
|
|
/>
|
|
<Text style={[styles.title, { color: colors.foreground }]}>{favorites.title}</Text>
|
|
<View style={styles.actions}>
|
|
<Button title="Play" onPress={() => void playInOrder(songIds)} />
|
|
<Button title="Shuffle" variant="secondary" onPress={() => void playShuffled(songIds)} />
|
|
</View>
|
|
</View>
|
|
|
|
{favorites.songs.map((song: BrowseSong) => (
|
|
<SongRow
|
|
key={song.id}
|
|
song={song}
|
|
onPress={() => void playFromSong(song.id, songIds)}
|
|
onLongPress={() => setActionSong(song)}
|
|
/>
|
|
))}
|
|
</ScrollView>
|
|
|
|
<MediaActionsSheet
|
|
visible={Boolean(actionSong)}
|
|
title={actionSong?.title ?? ""}
|
|
favorited={actionSong ? isSongFavorite(actionSong.id) : true}
|
|
onClose={() => setActionSong(null)}
|
|
onAction={handleSongAction}
|
|
/>
|
|
</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: 200,
|
|
height: 200,
|
|
borderRadius: radius.lg,
|
|
backgroundColor: "#ddd",
|
|
},
|
|
title: { fontSize: 24, fontWeight: "800" },
|
|
actions: { flexDirection: "row", gap: spacing.sm, marginTop: spacing.sm },
|
|
})
|