koel/resources/assets/js/composables/usePlaylistManagement.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

import { playlistStore } from '@/stores'
2024-05-19 05:49:42 +00:00
import { eventBus, getPlayableCollectionContentType } from '@/utils'
import { useErrorHandler, useMessageToaster } from '@/composables'
export const usePlaylistManagement = () => {
const { handleHttpError } = useErrorHandler('dialog')
const { toastSuccess } = useMessageToaster()
2024-05-19 05:49:42 +00:00
const inflect = (playables: Playable[]) => {
switch (getPlayableCollectionContentType(playables)) {
case 'songs':
return playables.length === 1 ? 'Song' : 'Songs'
case 'episodes':
return playables.length === 1 ? 'Episode' : 'Episodes'
default:
return playables.length === 1 ? 'Item' : 'Items'
}
}
const addToPlaylist = async (playlist: Playlist, playables: Playable[]) => {
if (playlist.is_smart || playables.length === 0) {
return
}
try {
2024-05-19 05:49:42 +00:00
await playlistStore.addContent(playlist, playables)
eventBus.emit('PLAYLIST_UPDATED', playlist)
2024-05-19 05:49:42 +00:00
toastSuccess(`${inflect(playables)} added into "${playlist.name}."`)
} catch (error: unknown) {
handleHttpError(error)
}
}
2024-05-19 05:49:42 +00:00
const removeFromPlaylist = async (playlist: Playlist, playables: Playable[]) => {
if (playlist.is_smart) {
return
}
try {
2024-05-19 05:49:42 +00:00
await playlistStore.removeContent(playlist, playables)
eventBus.emit('PLAYLIST_CONTENT_REMOVED', playlist, playables)
toastSuccess(`${inflect(playables)} removed from "${playlist.name}."`)
} catch (error: unknown) {
handleHttpError(error)
}
}
return {
2024-05-19 05:49:42 +00:00
addToPlaylist,
removeFromPlaylist,
}
}