294 lines
8.8 KiB
TypeScript
294 lines
8.8 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { useMutation } from "convex/react"
|
|
import { toast } from "sonner"
|
|
|
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
|
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<boolean>
|
|
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 = useCachedQuery(api.favorites.getUserFavorites)
|
|
const favoriteSong = useMutation(api.favorites.favoriteSong).withOptimisticUpdate(
|
|
(store, { songId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current && !current.favoriteSongs.includes(songId)) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteSongs: [...current.favoriteSongs, songId],
|
|
})
|
|
}
|
|
}
|
|
)
|
|
const favoriteSongsMutation = useMutation(
|
|
api.favorites.favoriteSongs
|
|
).withOptimisticUpdate((store, { songIds }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteSongs: [...new Set([...current.favoriteSongs, ...songIds])],
|
|
})
|
|
}
|
|
})
|
|
const unfavoriteSong = useMutation(
|
|
api.favorites.unfavoriteSong
|
|
).withOptimisticUpdate((store, { songId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteSongs: current.favoriteSongs.filter((id) => id !== songId),
|
|
})
|
|
}
|
|
})
|
|
const unfavoriteSongs = useMutation(
|
|
api.favorites.unfavoriteSongs
|
|
).withOptimisticUpdate((store, { songIds }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current) {
|
|
const removed = new Set(songIds)
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteSongs: current.favoriteSongs.filter((id) => !removed.has(id)),
|
|
})
|
|
}
|
|
})
|
|
const favoriteAlbum = useMutation(
|
|
api.favorites.favoriteAlbum
|
|
).withOptimisticUpdate((store, { albumId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current && !current.favoriteAlbums.includes(albumId)) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteAlbums: [...current.favoriteAlbums, albumId],
|
|
})
|
|
}
|
|
})
|
|
const unfavoriteAlbum = useMutation(
|
|
api.favorites.unfavoriteAlbum
|
|
).withOptimisticUpdate((store, { albumId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteAlbums: current.favoriteAlbums.filter((id) => id !== albumId),
|
|
})
|
|
}
|
|
})
|
|
const favoriteArtist = useMutation(
|
|
api.favorites.favoriteArtist
|
|
).withOptimisticUpdate((store, { artistId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current && !current.favoriteArtists.includes(artistId)) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteArtists: [...current.favoriteArtists, artistId],
|
|
})
|
|
}
|
|
})
|
|
const unfavoriteArtist = useMutation(
|
|
api.favorites.unfavoriteArtist
|
|
).withOptimisticUpdate((store, { artistId }) => {
|
|
const current = store.getQuery(api.favorites.getUserFavorites, {})
|
|
if (current) {
|
|
store.setQuery(api.favorites.getUserFavorites, {}, {
|
|
...current,
|
|
favoriteArtists: current.favoriteArtists.filter(
|
|
(id) => id !== artistId
|
|
),
|
|
})
|
|
}
|
|
})
|
|
|
|
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 true
|
|
}
|
|
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"
|
|
)
|
|
}
|
|
return true
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update favorites"
|
|
)
|
|
return false
|
|
}
|
|
},
|
|
[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>
|
|
)
|
|
}
|