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