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

184 lines
4.8 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2024-04-04 22:20:42 +00:00
<ScreenBase>
<template #header>
<ScreenHeader :layout="headerLayout">
All Songs
<ControlsToggle v-model="showingControls" />
<template #thumbnail>
<ThumbnailStack :thumbnails="thumbnails" />
</template>
<template v-if="totalSongCount" #meta>
<span>{{ pluralize(totalSongCount, 'song') }}</span>
<span>{{ totalDuration }}</span>
</template>
<template #controls>
<div class="controls w-full min-h-[32px] flex justify-between items-center gap-4">
<SongListControls
v-if="totalSongCount && (!isPhone || showingControls)"
:config="config"
@play-all="playAll"
@play-selected="playSelected"
/>
<label v-if="isPlus" class="text-k-text-secondary inline-flex items-center text-base">
<CheckBox v-model="ownSongsOnly" />
<span>Own songs only</span>
</label>
</div>
</template>
</ScreenHeader>
</template>
<SongListSkeleton v-if="showSkeletons" class="-m-6" />
<template v-else>
<SongList
2024-03-18 22:04:01 +00:00
v-if="songs?.length > 0"
ref="songList"
2024-04-04 22:20:42 +00:00
class="-m-6"
@sort="sort"
@scroll-breakpoint="onScrollBreakpoint"
@press:enter="onPressEnter"
@scrolled-to-end="fetchSongs"
/>
<ScreenEmptyState v-else>
<template #icon>
<Icon :icon="faVolumeOff" />
</template>
Your library is empty.
<a
v-if="isPlus && ownSongsOnly"
2024-03-18 22:04:01 +00:00
class="d-block secondary"
role="button"
@click.prevent="showSongsFromOthers"
>
Show public songs from other users?
</a>
</ScreenEmptyState>
</template>
2024-04-04 22:20:42 +00:00
</ScreenBase>
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { faVolumeOff } from '@fortawesome/free-solid-svg-icons'
import { computed, ref, toRef, watch } from 'vue'
import { pluralize, secondsToHumanReadable } from '@/utils'
2022-06-10 10:47:46 +00:00
import { commonStore, queueStore, songStore } from '@/stores'
2024-03-15 15:09:50 +00:00
import { playbackService } from '@/services'
2024-04-04 22:20:42 +00:00
import {
2024-04-23 21:01:27 +00:00
useErrorHandler,
2024-04-04 22:20:42 +00:00
useKoelPlus,
2024-04-23 21:01:27 +00:00
useLocalStorage,
2024-04-04 22:20:42 +00:00
useRouter,
useSongList,
2024-04-23 21:01:27 +00:00
useSongListControls
2024-04-04 22:20:42 +00:00
} from '@/composables'
2022-04-21 16:06:45 +00:00
import ScreenHeader from '@/components/ui/ScreenHeader.vue'
2022-07-30 15:08:20 +00:00
import SongListSkeleton from '@/components/ui/skeletons/SongListSkeleton.vue'
2024-04-04 22:20:42 +00:00
import CheckBox from '@/components/ui/form/CheckBox.vue'
import ScreenEmptyState from '@/components/ui/ScreenEmptyState.vue'
2024-04-04 22:20:42 +00:00
import ScreenBase from '@/components/screens/ScreenBase.vue'
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
const totalSongCount = toRef(commonStore.state, 'song_count')
const totalDuration = computed(() => secondsToHumanReadable(commonStore.state.song_length))
2022-06-10 10:47:46 +00:00
2022-04-15 17:00:08 +00:00
const {
SongList,
2022-06-10 10:47:46 +00:00
ControlsToggle,
2022-07-16 09:52:39 +00:00
ThumbnailStack,
headerLayout,
thumbnails,
2022-04-21 16:06:45 +00:00
songs,
2022-04-15 17:00:08 +00:00
songList,
showingControls,
isPhone,
onPressEnter,
2022-04-15 17:00:08 +00:00
playSelected,
2022-07-16 09:52:39 +00:00
onScrollBreakpoint
2024-03-25 22:59:38 +00:00
} = useSongList(toRef(songStore.state, 'songs'), { type: 'Songs' }, { sortable: true })
2022-06-10 10:47:46 +00:00
2024-01-18 11:13:05 +00:00
const { SongListControls, config } = useSongListControls('Songs')
2022-11-18 18:56:21 +00:00
const { go, onScreenActivated } = useRouter()
const { isPlus } = useKoelPlus()
2024-03-15 15:09:50 +00:00
const { get: lsGet, set: lsSet } = useLocalStorage()
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
let sortField: SongListSortField = 'title' // @todo get from query string
let sortOrder: SortOrder = 'asc'
const page = ref<number | null>(1)
const moreSongsAvailable = computed(() => page.value !== null)
2022-07-30 15:08:20 +00:00
const showSkeletons = computed(() => loading.value && songs.value.length === 0)
2022-06-10 10:47:46 +00:00
2024-03-15 15:09:50 +00:00
const ownSongsOnly = ref(isPlus.value ? Boolean(lsGet('own-songs-only')) : false)
watch(ownSongsOnly, async value => {
2024-03-15 15:09:50 +00:00
lsSet('own-songs-only', value)
page.value = 1
songStore.state.songs = []
await fetchSongs()
})
2022-06-10 10:47:46 +00:00
const sort = async (field: SongListSortField, order: SortOrder) => {
page.value = 1
songStore.state.songs = []
sortField = field
sortOrder = order
await fetchSongs()
}
const showSongsFromOthers = async () => {
ownSongsOnly.value = false
await fetchSongs()
}
2022-06-10 10:47:46 +00:00
const fetchSongs = async () => {
2022-07-30 15:08:20 +00:00
if (!moreSongsAvailable.value || loading.value) return
2022-06-10 10:47:46 +00:00
2022-07-30 15:08:20 +00:00
loading.value = true
try {
page.value = await songStore.paginate({
sort: sortField,
order: sortOrder,
page: page.value!,
own_songs_only: ownSongsOnly.value
})
2024-04-04 22:20:42 +00:00
} catch (error: any) {
useErrorHandler().handleHttpError(error)
} finally {
loading.value = false
}
2022-06-10 10:47:46 +00:00
}
const playAll = async (shuffle: boolean) => {
if (shuffle) {
await queueStore.fetchRandom()
} else {
await queueStore.fetchInOrder(sortField, sortOrder)
}
2022-11-18 18:44:20 +00:00
go('queue')
await playbackService.playFirstInQueue()
2022-06-10 10:47:46 +00:00
}
2022-11-18 18:56:21 +00:00
onScreenActivated('Songs', async () => {
if (!initialized) {
2022-06-10 10:47:46 +00:00
initialized = true
await fetchSongs()
2022-06-10 10:47:46 +00:00
}
})
2022-04-15 14:24:30 +00:00
</script>
2024-04-04 20:13:35 +00:00
<style lang="postcss" scoped>
2024-04-04 22:20:42 +00:00
.collapsed .controls {
@apply w-auto;
}
</style>