- 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
205 lines
6.0 KiB
TypeScript
205 lines
6.0 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { useMutation, useQuery } from "convex/react"
|
|
import { toast } from "sonner"
|
|
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
|
|
type FavoritesContextValue = {
|
|
isSongFavorite: (songId: string) => boolean
|
|
isAlbumFavorite: (albumId: string) => boolean
|
|
isArtistFavorite: (artistId: string) => boolean
|
|
toggleSongFavorite: (songId: Id<"songs">) => Promise<void>
|
|
toggleSongsFavorite: (songIds: Id<"songs">[], favorited: boolean) => Promise<void>
|
|
toggleAlbumFavorite: (albumId: Id<"albums">) => Promise<void>
|
|
toggleArtistFavorite: (artistId: Id<"artists">) => Promise<void>
|
|
favoriteSongs: (songIds: Id<"songs">[]) => Promise<void>
|
|
}
|
|
|
|
const FavoritesContext = React.createContext<FavoritesContextValue | null>(null)
|
|
|
|
export function useFavorites() {
|
|
const context = React.useContext(FavoritesContext)
|
|
if (!context) {
|
|
throw new Error("useFavorites must be used within FavoritesProvider")
|
|
}
|
|
return context
|
|
}
|
|
|
|
export function FavoritesProvider({ children }: { children: React.ReactNode }) {
|
|
const favorites = useQuery(api.favorites.getUserFavorites)
|
|
const favoriteSong = useMutation(api.favorites.favoriteSong)
|
|
const favoriteSongsMutation = useMutation(api.favorites.favoriteSongs)
|
|
const unfavoriteSong = useMutation(api.favorites.unfavoriteSong)
|
|
const unfavoriteSongs = useMutation(api.favorites.unfavoriteSongs)
|
|
const favoriteAlbum = useMutation(api.favorites.favoriteAlbum)
|
|
const unfavoriteAlbum = useMutation(api.favorites.unfavoriteAlbum)
|
|
const favoriteArtist = useMutation(api.favorites.favoriteArtist)
|
|
const unfavoriteArtist = useMutation(api.favorites.unfavoriteArtist)
|
|
|
|
const songIds = React.useMemo(
|
|
() => new Set(favorites?.favoriteSongs.map(String) ?? []),
|
|
[favorites?.favoriteSongs]
|
|
)
|
|
const albumIds = React.useMemo(
|
|
() => new Set(favorites?.favoriteAlbums.map(String) ?? []),
|
|
[favorites?.favoriteAlbums]
|
|
)
|
|
const artistIds = React.useMemo(
|
|
() => new Set(favorites?.favoriteArtists.map(String) ?? []),
|
|
[favorites?.favoriteArtists]
|
|
)
|
|
|
|
const isSongFavorite = React.useCallback(
|
|
(songId: string) => songIds.has(songId),
|
|
[songIds]
|
|
)
|
|
const isAlbumFavorite = React.useCallback(
|
|
(albumId: string) => albumIds.has(albumId),
|
|
[albumIds]
|
|
)
|
|
const isArtistFavorite = React.useCallback(
|
|
(artistId: string) => artistIds.has(artistId),
|
|
[artistIds]
|
|
)
|
|
|
|
const toggleSongFavorite = React.useCallback(
|
|
async (songId: Id<"songs">) => {
|
|
try {
|
|
if (songIds.has(songId)) {
|
|
await unfavoriteSong({ songId })
|
|
toast.success("Removed from favorites")
|
|
} else {
|
|
await favoriteSong({ songId })
|
|
toast.success("Added to favorites")
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[songIds, favoriteSong, unfavoriteSong]
|
|
)
|
|
|
|
const favoriteSongs = React.useCallback(
|
|
async (ids: Id<"songs">[]) => {
|
|
const unique = [...new Set(ids)]
|
|
if (unique.length === 0) {
|
|
return
|
|
}
|
|
try {
|
|
await favoriteSongsMutation({ songIds: unique })
|
|
toast.success(
|
|
unique.length > 1
|
|
? `Favorited ${unique.length} songs`
|
|
: "Added to favorites"
|
|
)
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[favoriteSongsMutation]
|
|
)
|
|
|
|
const toggleSongsFavorite = React.useCallback(
|
|
async (ids: Id<"songs">[], favorited: boolean) => {
|
|
const unique = [...new Set(ids)]
|
|
if (unique.length === 0) {
|
|
return
|
|
}
|
|
try {
|
|
if (favorited) {
|
|
await unfavoriteSongs({ songIds: unique })
|
|
toast.success(
|
|
unique.length > 1
|
|
? `Removed ${unique.length} songs from favorites`
|
|
: "Removed from favorites"
|
|
)
|
|
} else {
|
|
await favoriteSongsMutation({ songIds: unique })
|
|
toast.success(
|
|
unique.length > 1
|
|
? `Favorited ${unique.length} songs`
|
|
: "Added to favorites"
|
|
)
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[favoriteSongsMutation, unfavoriteSongs]
|
|
)
|
|
|
|
const toggleAlbumFavorite = React.useCallback(
|
|
async (albumId: Id<"albums">) => {
|
|
try {
|
|
if (albumIds.has(albumId)) {
|
|
await unfavoriteAlbum({ albumId })
|
|
toast.success("Removed album from favorites")
|
|
} else {
|
|
await favoriteAlbum({ albumId })
|
|
toast.success("Added album to favorites")
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[albumIds, favoriteAlbum, unfavoriteAlbum]
|
|
)
|
|
|
|
const toggleArtistFavorite = React.useCallback(
|
|
async (artistId: Id<"artists">) => {
|
|
try {
|
|
if (artistIds.has(artistId)) {
|
|
await unfavoriteArtist({ artistId })
|
|
toast.success("Removed artist from favorites")
|
|
} else {
|
|
await favoriteArtist({ artistId })
|
|
toast.success("Added artist to favorites")
|
|
}
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
}
|
|
},
|
|
[artistIds, favoriteArtist, unfavoriteArtist]
|
|
)
|
|
|
|
const value = React.useMemo(
|
|
() => ({
|
|
isSongFavorite,
|
|
isAlbumFavorite,
|
|
isArtistFavorite,
|
|
toggleSongFavorite,
|
|
toggleSongsFavorite,
|
|
toggleAlbumFavorite,
|
|
toggleArtistFavorite,
|
|
favoriteSongs,
|
|
}),
|
|
[
|
|
isSongFavorite,
|
|
isAlbumFavorite,
|
|
isArtistFavorite,
|
|
toggleSongFavorite,
|
|
toggleSongsFavorite,
|
|
toggleAlbumFavorite,
|
|
toggleArtistFavorite,
|
|
favoriteSongs,
|
|
]
|
|
)
|
|
|
|
return (
|
|
<FavoritesContext.Provider value={value}>{children}</FavoritesContext.Provider>
|
|
)
|
|
}
|