[web-src] Refactor "recently added" - group in JS instead of doing 3 queries against the back end

This commit is contained in:
chme
2021-01-10 11:36:31 +01:00
parent cdc7d7a1da
commit 1a6c76d990
4 changed files with 46 additions and 134 deletions

View File

@@ -19,6 +19,8 @@ export default class Albums {
getAlbumIndex (album) {
if (this.options.sort === 'Recently added') {
return album.time_added.substring(0, 4)
} else if (this.options.sort === 'Recently added (browse)') {
return this.getRecentlyAddedBrowseIndex(album.time_added)
} else if (this.options.sort === 'Recently released') {
return album.date_released ? album.date_released.substring(0, 4) : '0000'
} else if (this.options.sort === 'Release date') {
@@ -27,6 +29,23 @@ export default class Albums {
return album.name_sort.charAt(0).toUpperCase()
}
getRecentlyAddedBrowseIndex (recentlyAdded) {
if (!recentlyAdded) {
return '0000'
}
const diff = new Date().getTime() - new Date(recentlyAdded).getTime()
if (diff < 86400000) { // 24h
return 'Today'
} else if (diff < 604800000) { // 7 days
return 'Last week'
} else if (diff < 2592000000) { // 30 days
return 'Last month'
}
return recentlyAdded.substring(0, 4)
}
isAlbumVisible (album) {
if (this.options.hideSingles && album.track_count <= 2) {
return false
@@ -47,7 +66,7 @@ export default class Albums {
if (this.options.hideSingles || this.options.hideSpotify || this.options.hideOther) {
albumsSorted = albumsSorted.filter(album => this.isAlbumVisible(album))
}
if (this.options.sort === 'Recently added') {
if (this.options.sort === 'Recently added' || this.options.sort === 'Recently added (browse)') {
albumsSorted = [...albumsSorted].sort((a, b) => b.time_added.localeCompare(a.time_added))
} else if (this.options.sort === 'Recently released') {
albumsSorted = [...albumsSorted].sort((a, b) => {