105 lines
3.3 KiB
TypeScript
105 lines
3.3 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { useConvex } from "convex/react"
|
|
|
|
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import type { MediaContextAction } from "@/components/media-context-menu"
|
|
import { useCacheScope } from "@/components/persistent-cache-provider"
|
|
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
|
import { stationArtistId } from "@/lib/station-artist"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
|
|
|
|
export function useMediaActions() {
|
|
const { openAddToPlaylist, openAddArtistToPlaylist } = useAddToPlaylist()
|
|
const { favoriteSongs, toggleArtistFavorite } = useFavorites()
|
|
const { runQueueAction, createStation } = useQueueMediaActions()
|
|
const convex = useConvex()
|
|
const cacheScope = useCacheScope()
|
|
|
|
const runSongsAction = React.useCallback(
|
|
(
|
|
action: MediaContextAction,
|
|
songIds: Id<"songs">[],
|
|
stationArtist?: string
|
|
) => {
|
|
switch (action) {
|
|
case "create-station":
|
|
if (stationArtist) void createStation(stationArtist)
|
|
break
|
|
case "favorite":
|
|
void favoriteSongs(songIds)
|
|
break
|
|
case "playlist":
|
|
openAddToPlaylist(songIds)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle":
|
|
void runQueueAction(action, songIds)
|
|
break
|
|
}
|
|
},
|
|
[createStation, favoriteSongs, openAddToPlaylist, runQueueAction]
|
|
)
|
|
|
|
const runArtistAction = React.useCallback(
|
|
async (action: MediaContextAction, artistId: string) => {
|
|
switch (action) {
|
|
case "favorite":
|
|
void toggleArtistFavorite(artistId as Id<"artists">)
|
|
break
|
|
case "playlist":
|
|
void openAddArtistToPlaylist(artistId as Id<"artists">)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle": {
|
|
const songIds = await fetchCachedQuery(
|
|
convex,
|
|
cacheScope,
|
|
api.playlists.getOrderedArtistSongIds,
|
|
{ artistId: artistId as Id<"artists"> }
|
|
)
|
|
void runQueueAction(action, songIds)
|
|
break
|
|
}
|
|
}
|
|
},
|
|
[convex, cacheScope, toggleArtistFavorite, openAddArtistToPlaylist, runQueueAction]
|
|
)
|
|
|
|
return { runSongsAction, runArtistAction }
|
|
}
|
|
|
|
/**
|
|
* Handles a track row menu action, targeting the current selection or else the
|
|
* row's song. The station artist is `artistId` when given, otherwise the artist
|
|
* every targeted song in `songs` shares.
|
|
*/
|
|
export function useTrackListMediaAction({
|
|
selectedIds,
|
|
songs,
|
|
artistId,
|
|
}: {
|
|
selectedIds: ReadonlySet<string>
|
|
songs?: { id: string; artist: { id: string } }[]
|
|
artistId?: string
|
|
}) {
|
|
const { runSongsAction } = useMediaActions()
|
|
|
|
return React.useCallback(
|
|
(action: MediaContextAction, songId: string) => {
|
|
const targetIds = selectedIds.size > 0 ? Array.from(selectedIds) : [songId]
|
|
const stationArtist =
|
|
artistId ??
|
|
(songs ? stationArtistId(songs, new Set(targetIds)) : undefined)
|
|
runSongsAction(action, targetIds as Id<"songs">[], stationArtist)
|
|
},
|
|
[selectedIds, songs, artistId, runSongsAction]
|
|
)
|
|
}
|