77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { toast } from "sonner"
|
|
|
|
import { usePlaybackActions } from "@/components/playback-provider"
|
|
import type { MediaContextAction } from "@/components/media-context-menu"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
|
|
export type QueueMediaAction = Extract<
|
|
MediaContextAction,
|
|
"play-next" | "play-last" | "shuffle"
|
|
>
|
|
|
|
function queueActionMessage(action: QueueMediaAction, count: number) {
|
|
switch (action) {
|
|
case "play-next":
|
|
return count > 1
|
|
? `Queued ${count} songs to play next`
|
|
: "Queued to play next"
|
|
case "play-last":
|
|
return count > 1
|
|
? `Queued ${count} songs to play last`
|
|
: "Queued to play last"
|
|
case "shuffle":
|
|
return count > 1 ? `Playing ${count} songs shuffled` : "Playing shuffled"
|
|
}
|
|
}
|
|
|
|
export function useQueueMediaActions() {
|
|
const { addToQueueNext, addToQueueLast, playShuffled, playArtistShuffled } =
|
|
usePlaybackActions()
|
|
|
|
const runQueueAction = React.useCallback(
|
|
async (action: QueueMediaAction, songIds: Id<"songs">[]) => {
|
|
if (songIds.length === 0) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
switch (action) {
|
|
case "play-next":
|
|
await addToQueueNext(songIds)
|
|
break
|
|
case "play-last":
|
|
await addToQueueLast(songIds)
|
|
break
|
|
case "shuffle":
|
|
await playShuffled(songIds)
|
|
break
|
|
}
|
|
toast.success(queueActionMessage(action, songIds.length))
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to update queue"
|
|
)
|
|
}
|
|
},
|
|
[addToQueueNext, addToQueueLast, playShuffled]
|
|
)
|
|
|
|
const createStation = React.useCallback(
|
|
async (artistId: string) => {
|
|
try {
|
|
await playArtistShuffled(artistId as Id<"artists">)
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to create station"
|
|
)
|
|
}
|
|
},
|
|
[playArtistShuffled]
|
|
)
|
|
|
|
return { runQueueAction, createStation }
|
|
}
|