fix(web): unfavorite from track list menus (#133)
This commit was merged in pull request #133.
This commit is contained in:
1 parent
2fc127660e
commit
48aef3df62
8 files changed
+52
-30
No files matched your search
@@ -114,6 +114,7 @@ export function AlbumView({
|
||||
}) {
|
||||
const {
|
||||
isSongFavorite,
|
||||
areSongsFavorite,
|
||||
isAlbumFavorite,
|
||||
toggleSongFavorite,
|
||||
toggleAlbumFavorite,
|
||||
@@ -154,6 +155,7 @@ export function AlbumView({
|
||||
handlePointerDown,
|
||||
prepareContextMenu,
|
||||
} = useTrackSelection(album.tracks, validHighlightedSongId)
|
||||
const selectionFavorited = areSongsFavorite(selectedIds)
|
||||
|
||||
const selectionPositions = React.useMemo(
|
||||
() =>
|
||||
@@ -413,6 +415,7 @@ export function AlbumView({
|
||||
isFavorite={isSongFavorite(track.id)}
|
||||
selectedCount={selectedCount}
|
||||
selectionStationArtistId={album.artist.id}
|
||||
selectionFavorited={selectionFavorited}
|
||||
isNowPlaying={currentSongId === track.id}
|
||||
onPointerDown={handlePointerDown}
|
||||
onSelect={handleRowSelect}
|
||||
|
||||
@@ -37,7 +37,8 @@ type FavoritesContextValue = {
|
||||
) => Promise<boolean>
|
||||
toggleAlbumFavorite: (albumId: Id<"albums">) => Promise<void>
|
||||
toggleArtistFavorite: (artistId: Id<"artists">) => Promise<void>
|
||||
favoriteSongs: (songIds: Id<"songs">[]) => Promise<void>
|
||||
/** True when `songIds` is non-empty and every song is favorited. */
|
||||
areSongsFavorite: (songIds: Iterable<string>) => boolean
|
||||
}
|
||||
|
||||
const FavoritesContext = React.createContext<FavoritesContextValue | null>(null)
|
||||
@@ -130,26 +131,16 @@ export function FavoritesProvider({ children }: { children: React.ReactNode }) {
|
||||
[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"
|
||||
)
|
||||
const areSongsFavorite = React.useCallback(
|
||||
(ids: Iterable<string>) => {
|
||||
let any = false
|
||||
for (const id of ids) {
|
||||
if (!songIds.has(id)) return false
|
||||
any = true
|
||||
}
|
||||
return any
|
||||
},
|
||||
[favoriteSongsMutation]
|
||||
[songIds]
|
||||
)
|
||||
|
||||
const toggleSongsFavorite = React.useCallback(
|
||||
@@ -232,7 +223,7 @@ export function FavoritesProvider({ children }: { children: React.ReactNode }) {
|
||||
toggleSongsFavorite,
|
||||
toggleAlbumFavorite,
|
||||
toggleArtistFavorite,
|
||||
favoriteSongs,
|
||||
areSongsFavorite,
|
||||
}),
|
||||
[
|
||||
isSongFavorite,
|
||||
@@ -242,7 +233,7 @@ export function FavoritesProvider({ children }: { children: React.ReactNode }) {
|
||||
toggleSongsFavorite,
|
||||
toggleAlbumFavorite,
|
||||
toggleArtistFavorite,
|
||||
favoriteSongs,
|
||||
areSongsFavorite,
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@@ -48,7 +48,8 @@ export function OtherDownloadsView() {
|
||||
[songs]
|
||||
)
|
||||
|
||||
const { isSongFavorite, toggleSongFavorite } = useFavorites()
|
||||
const { isSongFavorite, areSongsFavorite, toggleSongFavorite } =
|
||||
useFavorites()
|
||||
const { playFromSong, playInOrder, playShuffled } = usePlaybackActions()
|
||||
const {
|
||||
selectedIds,
|
||||
@@ -62,6 +63,7 @@ export function OtherDownloadsView() {
|
||||
() => stationArtistId(songs, selectedIds),
|
||||
[songs, selectedIds]
|
||||
)
|
||||
const selectionFavorited = areSongsFavorite(selectedIds)
|
||||
const selectionPositions = useMemo(
|
||||
() =>
|
||||
getSelectionRangePositions(
|
||||
@@ -140,6 +142,7 @@ export function OtherDownloadsView() {
|
||||
isFavorite={isSongFavorite(songs[index].id)}
|
||||
selectedCount={selectedCount}
|
||||
selectionStationArtistId={selectedStationArtistId}
|
||||
selectionFavorited={selectionFavorited}
|
||||
onPointerDown={handlePointerDown}
|
||||
onSelect={handleRowSelect}
|
||||
onPlay={handleRowPlay}
|
||||
|
||||
@@ -74,8 +74,12 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
currentUser === undefined
|
||||
? playlist.isOwner
|
||||
: currentUser?._id === playlist.owner.id
|
||||
const { isSongFavorite, toggleSongFavorite, toggleSongsFavorite } =
|
||||
useFavorites()
|
||||
const {
|
||||
isSongFavorite,
|
||||
areSongsFavorite,
|
||||
toggleSongFavorite,
|
||||
toggleSongsFavorite,
|
||||
} = useFavorites()
|
||||
const currentSongId = useCurrentSongId()
|
||||
const {
|
||||
playInOrder,
|
||||
@@ -143,6 +147,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
() => stationArtistId(songs, selectedIds),
|
||||
[songs, selectedIds]
|
||||
)
|
||||
const selectionFavorited = areSongsFavorite(selectedIds)
|
||||
|
||||
const selectionPositions = React.useMemo(
|
||||
() =>
|
||||
@@ -465,6 +470,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
onAction={handlePlaylistMediaAction}
|
||||
playlistLabel={ADD_TO_ANOTHER_PLAYLIST}
|
||||
showFavorite={!isFavorites}
|
||||
favorited={areSongsFavorite(playlistSongIds)}
|
||||
downloaded={playlistDownloaded}
|
||||
trackRowTrigger={false}
|
||||
triggerVariant="secondary"
|
||||
@@ -522,6 +528,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
selectionPosition={selectionPositions[index]}
|
||||
selectedCount={selectedCount}
|
||||
selectionStationArtistId={selectedStationArtistId}
|
||||
selectionFavorited={selectionFavorited}
|
||||
playlistLabel={ADD_TO_ANOTHER_PLAYLIST}
|
||||
downloadCollection={downloadRef}
|
||||
showFavoriteInMenu={!isFavorites}
|
||||
|
||||
@@ -244,7 +244,7 @@ export function QueuePanel() {
|
||||
prepareContextMenu,
|
||||
clearSelection,
|
||||
} = useTrackSelection(selectionItems)
|
||||
const { isSongFavorite } = useFavorites()
|
||||
const { isSongFavorite, areSongsFavorite } = useFavorites()
|
||||
const { runSongsAction } = useMediaActions()
|
||||
|
||||
/** The rows a menu acts on: the selection, else just the clicked row. */
|
||||
@@ -268,6 +268,11 @@ export function QueuePanel() {
|
||||
),
|
||||
[selectableQueue, selectedIds]
|
||||
)
|
||||
const selectionFavorited = areSongsFavorite(
|
||||
selectableQueue.flatMap((item) =>
|
||||
item.song && selectedIds.has(item.selectionId) ? [item.song.id] : []
|
||||
)
|
||||
)
|
||||
|
||||
const handleMediaAction = React.useCallback(
|
||||
(action: MediaContextAction, selectionId: string) => {
|
||||
@@ -453,6 +458,7 @@ export function QueuePanel() {
|
||||
isFavorite={isSongFavorite(item.song.id)}
|
||||
selectedCount={selectedCount}
|
||||
selectionStationArtistId={selectedStationArtistId}
|
||||
selectionFavorited={selectionFavorited}
|
||||
removeLabel="Remove from Queue"
|
||||
selectionPosition={selectionPositions[index]}
|
||||
isDragging={draggingIndices.has(index)}
|
||||
|
||||
@@ -47,7 +47,8 @@ const SONGS_PAGE_SIZE = 50
|
||||
const estimateSongRowSize = () => 56
|
||||
|
||||
export function SongsView() {
|
||||
const { isSongFavorite, toggleSongFavorite } = useFavorites()
|
||||
const { isSongFavorite, areSongsFavorite, toggleSongFavorite } =
|
||||
useFavorites()
|
||||
const { playFromLibrarySong } = usePlaybackActions()
|
||||
const [sortKey, setSortKey] = useState<SortKey>("title")
|
||||
const [sortDirection, setSortDirection] = useState<SortDirection>("asc")
|
||||
@@ -98,6 +99,7 @@ export function SongsView() {
|
||||
() => stationArtistId(songs, selectedIds),
|
||||
[songs, selectedIds]
|
||||
)
|
||||
const selectionFavorited = areSongsFavorite(selectedIds)
|
||||
|
||||
const selectionPositions = useMemo(
|
||||
() =>
|
||||
@@ -222,6 +224,7 @@ export function SongsView() {
|
||||
isFavorite={isSongFavorite(songs[index].id)}
|
||||
selectedCount={selectedCount}
|
||||
selectionStationArtistId={selectedStationArtistId}
|
||||
selectionFavorited={selectionFavorited}
|
||||
onPointerDown={handlePointerDown}
|
||||
onSelect={handleRowSelect}
|
||||
onPlay={handleRowPlay}
|
||||
|
||||
@@ -68,6 +68,8 @@ export type TrackRowProps = {
|
||||
selectedCount?: number
|
||||
/** Station artist for the selection when this row is selected. */
|
||||
selectionStationArtistId?: string
|
||||
/** Whether every selected song is favorited, when this row is selected. */
|
||||
selectionFavorited?: boolean
|
||||
playlistLabel?: string
|
||||
showFavoriteInMenu?: boolean
|
||||
/** Adds a destructive menu item calling `onRemove`. */
|
||||
@@ -268,6 +270,7 @@ function TrackRowComponent({
|
||||
isDragging = false,
|
||||
selectedCount = 0,
|
||||
selectionStationArtistId,
|
||||
selectionFavorited,
|
||||
playlistLabel,
|
||||
showFavoriteInMenu = true,
|
||||
removeLabel,
|
||||
@@ -345,7 +348,10 @@ function TrackRowComponent({
|
||||
onAction: (action) => onMediaAction(action, rowId),
|
||||
playlistLabel,
|
||||
showFavorite: showFavoriteInMenu,
|
||||
favorited: isFavorite,
|
||||
favorited:
|
||||
inRange && selectedCount > 0
|
||||
? (selectionFavorited ?? isFavorite)
|
||||
: isFavorite,
|
||||
navigation: {
|
||||
artist: { id: song.artist.id },
|
||||
album: { id: song.album.id },
|
||||
|
||||
@@ -26,7 +26,8 @@ function songsTitle(songIds: readonly string[]) {
|
||||
|
||||
export function useMediaActions() {
|
||||
const { openAddToPlaylist, openAddArtistToPlaylist } = useAddToPlaylist()
|
||||
const { favoriteSongs, toggleArtistFavorite } = useFavorites()
|
||||
const { areSongsFavorite, toggleSongsFavorite, toggleArtistFavorite } =
|
||||
useFavorites()
|
||||
const { runQueueAction, createStation } = useQueueMediaActions()
|
||||
const convex = useConvex()
|
||||
const cacheScope = useCacheScope()
|
||||
@@ -44,7 +45,8 @@ export function useMediaActions() {
|
||||
if (stationArtist) void createStation(stationArtist)
|
||||
break
|
||||
case "favorite":
|
||||
void favoriteSongs(songIds)
|
||||
// Unfavorite only when every song already is, matching the menu label.
|
||||
void toggleSongsFavorite(songIds, areSongsFavorite(songIds))
|
||||
break
|
||||
case "playlist":
|
||||
openAddToPlaylist(songIds)
|
||||
@@ -73,7 +75,8 @@ export function useMediaActions() {
|
||||
},
|
||||
[
|
||||
createStation,
|
||||
favoriteSongs,
|
||||
areSongsFavorite,
|
||||
toggleSongsFavorite,
|
||||
openAddToPlaylist,
|
||||
runQueueAction,
|
||||
requestDownload,
|
||||
|
||||
Reference in new issue
Block a user