- 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
203 lines
7.2 KiB
TypeScript
203 lines
7.2 KiB
TypeScript
import { useMutation, 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, 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 { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { BrowseSong } from "@otunes/convex/types"
|
|
import { showError, showSuccess } from "@/lib/toast"
|
|
import { playlistCoverUrl } from "@/lib/urls"
|
|
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
export default function PlaylistScreen() {
|
|
const { id } = useLocalSearchParams<{ id: string }>()
|
|
const router = useRouter()
|
|
const colors = useThemeColors()
|
|
const { config } = useServerConfig()
|
|
const playlist = useQuery(api.playlists.browsePlaylistDetail, {
|
|
id: id as Id<"playlists">,
|
|
})
|
|
const { playPlaylist, playPlaylistShuffled, playFromSong } = usePlayback()
|
|
const { openAddToPlaylist, openAddPlaylistToPlaylist } = useAddToPlaylist()
|
|
const { isSongFavorite, toggleSongFavorite } = useFavorites()
|
|
const subscribe = useMutation(api.playlists.subscribeToPlaylist)
|
|
const unsubscribe = useMutation(api.playlists.unsubscribeFromPlaylist)
|
|
const deletePlaylist = useMutation(api.playlists.deletePlaylist)
|
|
const removeSongs = useMutation(api.playlists.removeSongsFromPlaylist)
|
|
const [actionSong, setActionSong] = useState<BrowseSong | null>(null)
|
|
const [menuOpen, setMenuOpen] = useState(false)
|
|
|
|
if (playlist === undefined || !config) {
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader title="Playlist" />
|
|
<LoadingView />
|
|
</SafeAreaView>
|
|
)
|
|
}
|
|
|
|
if (playlist === null) {
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader title="Playlist" />
|
|
<View style={styles.empty}>
|
|
<Text style={{ color: colors.mutedForeground }}>Playlist not found.</Text>
|
|
</View>
|
|
</SafeAreaView>
|
|
)
|
|
}
|
|
|
|
const songIds = playlist.songs.map((song: BrowseSong) => song.id)
|
|
|
|
async function handleSubscribeToggle() {
|
|
if (!playlist?.id) return
|
|
try {
|
|
if (playlist.isSubscribed) {
|
|
await unsubscribe({ playlistId: playlist.id })
|
|
showSuccess("Unsubscribed")
|
|
} else {
|
|
await subscribe({ playlistId: playlist.id })
|
|
showSuccess("Subscribed")
|
|
}
|
|
} catch (error) {
|
|
showError(error instanceof Error ? error.message : "Failed to update subscription")
|
|
}
|
|
}
|
|
|
|
async function handleDelete() {
|
|
if (!playlist?.id) return
|
|
try {
|
|
await deletePlaylist({ playlistId: playlist.id })
|
|
showSuccess("Playlist deleted")
|
|
router.back()
|
|
} catch (error) {
|
|
showError(error instanceof Error ? error.message : "Failed to delete playlist")
|
|
}
|
|
}
|
|
|
|
function handleSongAction(action: MediaAction) {
|
|
if (!actionSong) return
|
|
const ids = [actionSong.id]
|
|
switch (action) {
|
|
case "playlist":
|
|
openAddToPlaylist(ids)
|
|
break
|
|
case "favorite":
|
|
void toggleSongFavorite(actionSong.id)
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader
|
|
title={playlist.title}
|
|
right={
|
|
<Pressable onPress={() => setMenuOpen(true)} hitSlop={8}>
|
|
<Ionicons name="ellipsis-horizontal" size={22} color={colors.foreground} />
|
|
</Pressable>
|
|
}
|
|
/>
|
|
<ScrollView contentContainerStyle={{ paddingBottom: contentBottomInset }}>
|
|
<View style={styles.hero}>
|
|
<Image
|
|
source={{ uri: playlistCoverUrl(config.serverUrl, playlist.id ?? "favorites") }}
|
|
style={styles.cover}
|
|
contentFit="cover"
|
|
/>
|
|
<Text style={[styles.title, { color: colors.foreground }]}>{playlist.title}</Text>
|
|
<Text style={{ color: colors.mutedForeground }}>{playlist.owner.name}</Text>
|
|
<View style={styles.actions}>
|
|
<Button title="Play" onPress={() => playlist.id && void playPlaylist(playlist.id)} />
|
|
<Button
|
|
title="Shuffle"
|
|
variant="secondary"
|
|
onPress={() => playlist.id && void playPlaylistShuffled(playlist.id)}
|
|
/>
|
|
</View>
|
|
{!playlist.isOwner && playlist.id ? (
|
|
<Button
|
|
title={playlist.isSubscribed ? "Unsubscribe" : "Subscribe"}
|
|
variant="ghost"
|
|
onPress={() => void handleSubscribeToggle()}
|
|
/>
|
|
) : null}
|
|
{playlist.isOwner && playlist.id ? (
|
|
<Button title="Delete Playlist" variant="destructive" onPress={() => void handleDelete()} />
|
|
) : null}
|
|
</View>
|
|
|
|
{playlist.songs.map((song: BrowseSong) => (
|
|
<SongRow
|
|
key={song.id}
|
|
song={song}
|
|
onPress={() => void playFromSong(song.id, songIds)}
|
|
onLongPress={() => setActionSong(song)}
|
|
trailing={
|
|
playlist.isOwner && playlist.id ? (
|
|
<Pressable
|
|
onPress={() =>
|
|
void removeSongs({ playlistId: playlist.id!, songIds: [song.id] }).then(() =>
|
|
showSuccess("Removed from playlist")
|
|
)
|
|
}
|
|
>
|
|
<Ionicons name="trash-outline" size={18} color={colors.destructive} />
|
|
</Pressable>
|
|
) : null
|
|
}
|
|
/>
|
|
))}
|
|
</ScrollView>
|
|
|
|
<MediaActionsSheet
|
|
visible={Boolean(actionSong)}
|
|
title={actionSong?.title ?? ""}
|
|
favorited={actionSong ? isSongFavorite(actionSong.id) : false}
|
|
onClose={() => setActionSong(null)}
|
|
onAction={handleSongAction}
|
|
/>
|
|
|
|
<MediaActionsSheet
|
|
visible={menuOpen}
|
|
title={playlist.title}
|
|
showFavorite={false}
|
|
onClose={() => setMenuOpen(false)}
|
|
onAction={(action) => {
|
|
if (action === "playlist" && playlist.id) {
|
|
void openAddPlaylistToPlaylist(playlist.id)
|
|
}
|
|
}}
|
|
/>
|
|
</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", textAlign: "center" },
|
|
actions: { flexDirection: "row", gap: spacing.sm, marginTop: spacing.sm },
|
|
})
|