- 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
251 lines
7.7 KiB
TypeScript
251 lines
7.7 KiB
TypeScript
import { useConvex, useMutation, useQuery } from "convex/react"
|
|
import { useRouter } from "expo-router"
|
|
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useMemo,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react"
|
|
import { Modal, Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
|
|
|
|
import { Button, Input } from "@/components/ui/primitives"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { OwnedPlaylistForAdd } from "@otunes/convex/types"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import { showError, showSuccess } from "@/lib/toast"
|
|
import { useThemeColors } from "@/lib/theme"
|
|
|
|
type AddToPlaylistContextValue = {
|
|
openAddToPlaylist: (songIds: Id<"songs">[]) => void
|
|
openAddAlbumToPlaylist: (albumId: Id<"albums">) => Promise<void>
|
|
openAddPlaylistToPlaylist: (playlistId: Id<"playlists">) => Promise<void>
|
|
openAddArtistToPlaylist: (artistId: Id<"artists">) => Promise<void>
|
|
openCreatePlaylist: () => void
|
|
}
|
|
|
|
const AddToPlaylistContext = createContext<AddToPlaylistContextValue | null>(null)
|
|
|
|
export function useAddToPlaylist() {
|
|
const context = useContext(AddToPlaylistContext)
|
|
if (!context) {
|
|
throw new Error("useAddToPlaylist must be used within AddToPlaylistProvider")
|
|
}
|
|
return context
|
|
}
|
|
|
|
export function AddToPlaylistProvider({ children }: { children: ReactNode }) {
|
|
const convex = useConvex()
|
|
const router = useRouter()
|
|
const colors = useThemeColors()
|
|
const ownedPlaylists = useQuery(api.playlists.listOwnedPlaylists)
|
|
const addSongs = useMutation(api.playlists.addSongsToPlaylist)
|
|
const createPlaylist = useMutation(api.playlists.createPlaylist)
|
|
|
|
const [open, setOpen] = useState(false)
|
|
const [createOpen, setCreateOpen] = useState(false)
|
|
const [pendingSongIds, setPendingSongIds] = useState<Id<"songs">[]>([])
|
|
const [newPlaylistName, setNewPlaylistName] = useState("")
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const openAddToPlaylist = useCallback((songIds: Id<"songs">[]) => {
|
|
setPendingSongIds(songIds)
|
|
setOpen(true)
|
|
}, [])
|
|
|
|
const openAddAlbumToPlaylist = useCallback(
|
|
async (albumId: Id<"albums">) => {
|
|
const songIds = await convex.query(api.playlists.getAlbumSongIds, {
|
|
albumId,
|
|
})
|
|
openAddToPlaylist(songIds)
|
|
},
|
|
[convex, openAddToPlaylist]
|
|
)
|
|
|
|
const openAddPlaylistToPlaylist = useCallback(
|
|
async (playlistId: Id<"playlists">) => {
|
|
const songIds = await convex.query(api.playlists.getPlaylistSongIds, {
|
|
playlistId,
|
|
})
|
|
openAddToPlaylist(songIds)
|
|
},
|
|
[convex, openAddToPlaylist]
|
|
)
|
|
|
|
const openAddArtistToPlaylist = useCallback(
|
|
async (artistId: Id<"artists">) => {
|
|
const songIds = await convex.query(
|
|
api.playlists.getOrderedArtistSongIds,
|
|
{ artistId }
|
|
)
|
|
openAddToPlaylist(songIds)
|
|
},
|
|
[convex, openAddToPlaylist]
|
|
)
|
|
|
|
const openCreatePlaylist = useCallback(() => {
|
|
setNewPlaylistName("")
|
|
setCreateOpen(true)
|
|
}, [])
|
|
|
|
const handleAddToPlaylist = useCallback(
|
|
async (playlistId: Id<"playlists">, title: string, existing: Id<"songs">[]) => {
|
|
const existingSet = new Set(existing)
|
|
const toAdd = pendingSongIds.filter((id) => !existingSet.has(id))
|
|
if (toAdd.length === 0) {
|
|
showSuccess(`All selected songs are already in ${title}`)
|
|
setOpen(false)
|
|
return
|
|
}
|
|
try {
|
|
await addSongs({ playlistId, songIds: toAdd })
|
|
showSuccess(
|
|
toAdd.length > 1
|
|
? `Added ${toAdd.length} songs to ${title}`
|
|
: `Added to ${title}`
|
|
)
|
|
setOpen(false)
|
|
} catch (error) {
|
|
showError(error instanceof Error ? error.message : "Failed to add to playlist")
|
|
}
|
|
},
|
|
[addSongs, pendingSongIds]
|
|
)
|
|
|
|
const handleCreatePlaylist = useCallback(async () => {
|
|
const name = newPlaylistName.trim()
|
|
if (!name) return
|
|
setSubmitting(true)
|
|
try {
|
|
const playlistId = await createPlaylist({ name })
|
|
if (pendingSongIds.length > 0) {
|
|
await addSongs({ playlistId, songIds: pendingSongIds })
|
|
}
|
|
showSuccess("Playlist created")
|
|
setCreateOpen(false)
|
|
setOpen(false)
|
|
router.push(`/playlist/${playlistId}`)
|
|
} catch (error) {
|
|
showError(error instanceof Error ? error.message : "Failed to create playlist")
|
|
} finally {
|
|
setSubmitting(false)
|
|
}
|
|
}, [newPlaylistName, createPlaylist, pendingSongIds, addSongs, router])
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
openAddToPlaylist,
|
|
openAddAlbumToPlaylist,
|
|
openAddPlaylistToPlaylist,
|
|
openAddArtistToPlaylist,
|
|
openCreatePlaylist,
|
|
}),
|
|
[
|
|
openAddToPlaylist,
|
|
openAddAlbumToPlaylist,
|
|
openAddPlaylistToPlaylist,
|
|
openAddArtistToPlaylist,
|
|
openCreatePlaylist,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<AddToPlaylistContext.Provider value={value}>
|
|
{children}
|
|
|
|
<Modal visible={open} animationType="slide" transparent onRequestClose={() => setOpen(false)}>
|
|
<Pressable style={styles.overlay} onPress={() => setOpen(false)}>
|
|
<Pressable
|
|
style={[styles.sheet, { backgroundColor: colors.card, borderColor: colors.border }]}
|
|
onPress={(event) => event.stopPropagation()}
|
|
>
|
|
<Text style={[styles.title, { color: colors.foreground }]}>Add to Playlist</Text>
|
|
<ScrollView style={styles.list}>
|
|
{ownedPlaylists?.map((playlist: OwnedPlaylistForAdd) => (
|
|
<Pressable
|
|
key={playlist.id}
|
|
style={styles.row}
|
|
onPress={() =>
|
|
void handleAddToPlaylist(playlist.id, playlist.title, playlist.songIds)
|
|
}
|
|
>
|
|
<Text style={{ color: colors.foreground }}>{playlist.title}</Text>
|
|
</Pressable>
|
|
))}
|
|
</ScrollView>
|
|
<Button title="New Playlist" variant="secondary" onPress={() => { setOpen(false); openCreatePlaylist() }} />
|
|
</Pressable>
|
|
</Pressable>
|
|
</Modal>
|
|
|
|
<Modal visible={createOpen} animationType="fade" transparent onRequestClose={() => setCreateOpen(false)}>
|
|
<Pressable style={styles.overlay} onPress={() => setCreateOpen(false)}>
|
|
<Pressable
|
|
style={[styles.createSheet, { backgroundColor: colors.card, borderColor: colors.border }]}
|
|
onPress={(event) => event.stopPropagation()}
|
|
>
|
|
<Text style={[styles.title, { color: colors.foreground }]}>New Playlist</Text>
|
|
<Input
|
|
value={newPlaylistName}
|
|
onChangeText={setNewPlaylistName}
|
|
placeholder="Playlist name"
|
|
autoFocus
|
|
/>
|
|
<View style={styles.createActions}>
|
|
<Button title="Cancel" variant="ghost" onPress={() => setCreateOpen(false)} />
|
|
<Button
|
|
title="Create"
|
|
loading={submitting}
|
|
disabled={!newPlaylistName.trim()}
|
|
onPress={() => void handleCreatePlaylist()}
|
|
/>
|
|
</View>
|
|
</Pressable>
|
|
</Pressable>
|
|
</Modal>
|
|
</AddToPlaylistContext.Provider>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
overlay: {
|
|
flex: 1,
|
|
backgroundColor: "rgba(0,0,0,0.45)",
|
|
justifyContent: "flex-end",
|
|
},
|
|
sheet: {
|
|
maxHeight: "60%",
|
|
borderTopLeftRadius: 16,
|
|
borderTopRightRadius: 16,
|
|
borderWidth: 1,
|
|
padding: 16,
|
|
gap: 12,
|
|
},
|
|
createSheet: {
|
|
margin: 24,
|
|
borderRadius: 16,
|
|
borderWidth: 1,
|
|
padding: 16,
|
|
gap: 12,
|
|
},
|
|
title: {
|
|
fontSize: 18,
|
|
fontWeight: "700",
|
|
},
|
|
list: {
|
|
maxHeight: 280,
|
|
},
|
|
row: {
|
|
paddingVertical: 14,
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
borderBottomColor: "#e4e4e7",
|
|
},
|
|
createActions: {
|
|
flexDirection: "row",
|
|
justifyContent: "flex-end",
|
|
gap: 8,
|
|
},
|
|
})
|