import { ref, computed, onMounted, watch } from 'vue' import { orderBy } from 'lodash-es' import type { Album, AlbumGenre, Genre, Artist, Playlist } from '../types' import { useSubsonicApi } from '../subsonicApi' import { reloadToken } from '../reload' import { useMainStore } from '../store/main' import { useRadioStore } from '../store/radio' import { AlbumList } from '../components/AlbumList.vine' import { PlaylistList } from '../components/PlaylistList.vine' import { ArtistList } from '../components/ArtistList.vine' export const DiscoverView = () => { const subsonicApi = useSubsonicApi(), mainStore = useMainStore(), radioStore = useRadioStore(), lastGenre = ref(null), result = ref({ genres: [] as Genre[], mood: [] as Album[], favartists: [] as Artist[], favalbums: [] as Album[], playlists: [] as Playlist[], played: [] as Album[], most: [] as Album[], recent: [] as Album[], random: [] as Album[], }), empty = computed(() => Object.values(result.value).findIndex(x => x.length > 0) === -1), clearState = () => Object.assign(result.value, { recent: [], played: [], mood: [], most: [], favalbums: [], favartists: [], genres: [], playlists: [], random: [], }), fetchData = async () => { if (mainStore.isLoading) return try { mainStore.isLoading = true subsonicApi.getGenres() .then((genres: Genre[]) => { const genreNames = genres.map((genre: Genre) => ({ ...genre, id: genre.name })) result.value.genres = orderBy(genreNames, 'albumCount', 'desc') }) subsonicApi.getFavourites() .then(favourites => { result.value.favartists = favourites.artists.slice(0, 16) result.value.favalbums = favourites.albums.slice(0, 16) }) subsonicApi.getAlbums('recently-played', 24) .then(async played => { result.value.played = played if (played.length === 0) return const lastPlayed = played[0] lastGenre.value = (lastPlayed as Album).genres[0]! if (!lastGenre.value) return const shuffled = true const albumsByGenre = await subsonicApi.getAlbumsByGenre(lastGenre.value.name, 32, 0, shuffled) result.value.mood = albumsByGenre }) subsonicApi.getAlbums('recently-added', 32) .then(recent => { result.value.recent = recent }) subsonicApi.getPlaylists() .then(playlists => { result.value.playlists = playlists.slice(0, 10) }) subsonicApi.getAlbums('random', 24) .then(random => { result.value.random = random }) subsonicApi.getAlbums('most-played', 24) .then(most => { result.value.most = most }) } finally { mainStore.isLoading = false } }, radioRecentlyPlayed = () => radioStore.shuffleRecentlyPlayed(), radioMostPlayed = () => radioStore.shuffleMostPlayed(), radioFavouriteAlbums = () => radioStore.shuffleFavouriteAlbums(), radioFavouriteArtists = () => radioStore.shuffleFavouriteArtists(), radioRecentlyAdded = () => radioStore.shuffleRecentlyAdded(), luckyRadio = () => radioStore.luckyRadio(), radioMood = () => ( (lastGenre.value) ? radioStore.shuffleMood(lastGenre.value.name) : undefined ) onMounted(fetchData) watch( reloadToken, () => (clearState() && fetchData()) ) return vine`
{{ item.name }}
Current mood
Favurited albums
Favurited artists
Playlists
Most Played
Recently played
Recently added
Get lucky
` }