Files
otunes/apps/web/hooks/use-media-actions.ts
T

155 lines
4.5 KiB
TypeScript

"use client"
import * as React from "react"
import { useConvex } from "convex/react"
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
import { useDownloads } from "@/components/downloads-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 { getCachedSong } from "@/lib/song-cache"
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"
import type { DownloadSource } from "@otunes/convex/client/download-queue"
/** The song's title for one song, else a count. */
function songsTitle(songIds: readonly string[]) {
if (songIds.length === 1) {
return getCachedSong(songIds[0])?.title ?? "1 song"
}
return `${songIds.length} songs`
}
export function useMediaActions() {
const { openAddToPlaylist, openAddArtistToPlaylist } = useAddToPlaylist()
const { favoriteSongs, toggleArtistFavorite } = useFavorites()
const { runQueueAction, createStation } = useQueueMediaActions()
const convex = useConvex()
const cacheScope = useCacheScope()
const { requestDownload, removeDownload } = useDownloads()
const runSongsAction = React.useCallback(
(
action: MediaContextAction,
songIds: Id<"songs">[],
stationArtist?: string,
downloadFrom?: DownloadSource
) => {
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
case "download":
void requestDownload({
kind: "songs",
songIds,
title: songsTitle(songIds),
})
break
case "remove-download":
void removeDownload({
kind: "songs",
songIds,
title: "",
from: downloadFrom,
})
break
}
},
[
createStation,
favoriteSongs,
openAddToPlaylist,
runQueueAction,
requestDownload,
removeDownload,
]
)
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.flat())
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,
downloadFrom,
}: {
selectedIds: ReadonlySet<string>
songs?: { id: string; artist: { id: string } }[]
artistId?: string
/** Removing a download only takes it out of this one. */
downloadFrom?: DownloadSource
}) {
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,
downloadFrom
)
},
[selectedIds, songs, artistId, downloadFrom, runSongsAction]
)
}