koel/resources/assets/js/components/screens/AlbumListScreen.vue

92 lines
2.5 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<section id="albumsWrapper">
2022-07-16 15:44:45 +00:00
<ScreenHeader layout="collapsed">
2022-04-15 14:24:30 +00:00
Albums
2022-12-02 16:17:37 +00:00
<template #controls>
<ViewModeSwitch v-model="viewMode" />
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
</ScreenHeader>
2022-04-15 14:24:30 +00:00
2022-07-11 17:30:51 +00:00
<div
ref="scroller"
v-koel-overflow-fade
2022-07-11 17:30:51 +00:00
:class="`as-${viewMode}`"
class="albums main-scroll-wrap"
data-testid="album-list"
@scroll="scrolling"
>
2022-07-30 15:08:20 +00:00
<template v-if="showSkeletons">
2022-12-02 16:17:37 +00:00
<AlbumCardSkeleton v-for="i in 10" :key="i" :layout="itemLayout" />
2022-07-30 15:08:20 +00:00
</template>
<template v-else>
2022-12-02 16:17:37 +00:00
<AlbumCard v-for="album in albums" :key="album.id" :album="album" :layout="itemLayout" />
<ToTopButton />
2022-07-30 15:08:20 +00:00
</template>
2022-04-15 14:24:30 +00:00
</div>
</section>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { computed, ref, toRef, watch } from 'vue'
2022-04-15 14:24:30 +00:00
import { albumStore, preferenceStore as preferences } from '@/stores'
import { useInfiniteScroll, useMessageToaster, useRouter } from '@/composables'
import { logger } from '@/utils'
2022-04-15 14:24:30 +00:00
import AlbumCard from '@/components/album/AlbumCard.vue'
2022-07-30 15:08:20 +00:00
import AlbumCardSkeleton from '@/components/ui/skeletons/ArtistAlbumCardSkeleton.vue'
import ScreenHeader from '@/components/ui/ScreenHeader.vue'
import ViewModeSwitch from '@/components/ui/ViewModeSwitch.vue'
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const viewMode = ref<ArtistAlbumViewMode>('thumbnails')
const albums = toRef(albumStore.state, 'albums')
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const {
ToTopButton,
scroller,
scrolling,
makeScrollable
2022-06-10 10:47:46 +00:00
} = useInfiniteScroll(async () => await fetchAlbums())
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
watch(viewMode, () => (preferences.albumsViewMode = viewMode.value))
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
let initialized = false
2022-07-30 15:08:20 +00:00
const loading = ref(false)
2022-06-10 10:47:46 +00:00
const page = ref<number | null>(1)
2022-07-30 15:08:20 +00:00
const itemLayout = computed<ArtistAlbumCardLayout>(() => viewMode.value === 'thumbnails' ? 'full' : 'compact')
2022-06-10 10:47:46 +00:00
const moreAlbumsAvailable = computed(() => page.value !== null)
2022-07-30 15:08:20 +00:00
const showSkeletons = computed(() => loading.value && albums.value.length === 0)
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
const fetchAlbums = async () => {
2022-07-30 15:08:20 +00:00
if (loading.value || !moreAlbumsAvailable.value) return
2022-06-10 10:47:46 +00:00
2022-07-30 15:08:20 +00:00
loading.value = true
2022-07-22 21:56:13 +00:00
page.value = await albumStore.paginate(page.value!)
2022-07-30 15:08:20 +00:00
loading.value = false
2022-06-10 10:47:46 +00:00
}
2022-11-18 18:56:21 +00:00
useRouter().onScreenActivated('Albums', async () => {
if (!initialized) {
2022-07-11 17:30:51 +00:00
viewMode.value = preferences.albumsViewMode || 'thumbnails'
2022-06-10 10:47:46 +00:00
initialized = true
try {
await makeScrollable()
} catch (error) {
logger.error(error)
useMessageToaster().toastError('Failed to load albums.')
initialized = false
}
2022-04-15 14:24:30 +00:00
}
})
</script>
<style lang="scss">
#albumsWrapper {
.albums {
@include artist-album-wrapper();
}
}
</style>
2022-07-04 15:57:08 +00:00
`