import { ref, computed, watch } from 'vue' import { useRouter, useRoute } from 'vue-router' import type { Album, AlbumSort } from '../types' import { useSubsonicApi } from '../subsonicApi' import { useFavouriteStore } from '../store/favourite' import { usePlayerStore } from '../store/player' import { useCacheStore } from '../store/cache' import { useMainStore } from '../store/main' import { useRadioStore } from '../store/radio' import { AlbumList } from '../components/AlbumList.vine' import { TrackList } from '../components/TrackList.vine' export const AlbumView = (props: { id?: string | null, sort?: AlbumSort }) => { const subsonicApi = useSubsonicApi(), router = useRouter(), route = useRoute(), mainStore = useMainStore(), favouriteStore = useFavouriteStore(), playerStore = usePlayerStore(), cacheStore = useCacheStore(), radioStore = useRadioStore(), sort = ref(props.sort ?? 'recently-added'), albums = ref([]), album = ref(null), cached = ref(false), hasMoreAlbums = ref(true), isFavourite = computed(() => (!!album.value && favouriteStore.get('album', album.value.id))), isPlaying = computed(() => playerStore.isPlaying), sortOptions = [ [ 'recently-added', 'Recently added'], [ 'most-played', 'Most played' ], [ 'recently-played', 'Recently played' ], [ 'random', 'Random' ], [ 'a-z', 'Alphabetically' ], ], fetchAlbums = async() => { try { mainStore.isLoading = true const response = await subsonicApi.getAlbums(sort.value, 30, albums.value.length) albums.value.push(...response) hasMoreAlbums.value = response.length >= 30 } finally { mainStore.isLoading = false } }, fetchAlbum = async() => { if (!props.id) return try { mainStore.isLoading = true album.value = await subsonicApi.getAlbumDetails(props.id) if (album.value) cached.value = await cacheStore.isCached(album.value) } finally { mainStore.isLoading = false } }, playNow = () => { if (!album.value) return const currentTrack = playerStore.track, isAlbumTrack = !!currentTrack && ( currentTrack.albumId === album.value.id || album.value.tracks?.some(t => t.id === currentTrack.id) ) if (isAlbumTrack) return playerStore.playPause() if (album.value.tracks?.length) return playerStore.playNow(album.value.tracks) }, setNextInQueue = () => ( (album.value) ? playerStore.setNextInQueue(album.value.tracks!) : undefined ), addToQueue = () => ( (album.value) ? playerStore.addToQueue(album.value.tracks!) : undefined ), toggleFavourite = async () => (!!album.value && favouriteStore.toggle('album', album.value.id)), cacheAlbum = async() => { if (!album.value) return await cacheStore.cacheAlbum(album.value) cached.value = true }, clearAlbumCache = async() => { if (!album.value) return await cacheStore.clearAlbumCache(album.value) cached.value = false await fetchAlbum() }, shuffleNow = async() => { if (!album.value?.tracks?.length) return await radioStore.shuffleAlbum(album.value.tracks) }, radioNow = async() => { if (!album.value?.tracks?.length || !album.value?.artists?.length) return await radioStore.radioAlbum(album.value.tracks, album.value.artists) } watch( () => [ sort.value, props.id ], () => { if (props.id) { fetchAlbum() } else { albums.value = [] hasMoreAlbums.value = true fetchAlbums() } }, { immediate: true } ) return vine` ` }