2022-10-24 15:27:17 +00:00
|
|
|
import { playlistStore } from '@/stores'
|
2024-05-19 05:49:42 +00:00
|
|
|
import { eventBus, getPlayableCollectionContentType } from '@/utils'
|
2024-04-23 11:24:29 +00:00
|
|
|
import { useErrorHandler, useMessageToaster } from '@/composables'
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
export const usePlaylistManagement = () => {
|
2024-04-23 11:24:29 +00:00
|
|
|
const { handleHttpError } = useErrorHandler('dialog')
|
2022-11-18 17:45:38 +00:00
|
|
|
const { toastSuccess } = useMessageToaster()
|
2022-10-24 15:27:17 +00:00
|
|
|
|
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[]) => {
|
2024-10-13 17:37:01 +00:00
|
|
|
if (playlist.is_smart || playables.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
try {
|
2024-05-19 05:49:42 +00:00
|
|
|
await playlistStore.addContent(playlist, playables)
|
2022-10-24 15:27:17 +00:00
|
|
|
eventBus.emit('PLAYLIST_UPDATED', playlist)
|
2024-05-19 05:49:42 +00:00
|
|
|
toastSuccess(`${inflect(playables)} added into "${playlist.name}."`)
|
2024-04-23 11:24:29 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
handleHttpError(error)
|
2022-10-24 15:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
const removeFromPlaylist = async (playlist: Playlist, playables: Playable[]) => {
|
2024-10-13 17:37:01 +00:00
|
|
|
if (playlist.is_smart) {
|
|
|
|
return
|
|
|
|
}
|
2022-10-24 15:27:17 +00:00
|
|
|
|
|
|
|
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}."`)
|
2024-04-23 11:24:29 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
handleHttpError(error)
|
2022-10-24 15:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-05-19 05:49:42 +00:00
|
|
|
addToPlaylist,
|
2024-10-13 17:37:01 +00:00
|
|
|
removeFromPlaylist,
|
2022-10-24 15:27:17 +00:00
|
|
|
}
|
|
|
|
}
|