koel/resources/assets/js/stores/artistStore.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-07-24 11:47:18 +00:00
import { reactive, UnwrapNestedRefs } from 'vue'
2022-07-22 22:03:25 +00:00
import { differenceBy, orderBy, take, unionBy } from 'lodash'
2022-07-25 13:25:27 +00:00
import { cache, httpService } from '@/services'
2022-07-24 11:47:18 +00:00
import { arrayify, logger } from '@/utils'
2022-04-15 14:24:30 +00:00
const UNKNOWN_ARTIST_ID = 1
const VARIOUS_ARTISTS_ID = 2
export const artistStore = {
2022-07-24 11:47:18 +00:00
vault: new Map<number, UnwrapNestedRefs<Artist>>(),
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
state: reactive({
2022-04-21 09:38:24 +00:00
artists: []
}),
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
byId (id: number) {
return this.vault.get(id)
2022-04-15 14:24:30 +00:00
},
2022-06-10 10:47:46 +00:00
removeByIds (ids: number[]) {
2022-07-22 21:56:13 +00:00
this.state.artists = differenceBy(this.state.artists, ids.map(id => this.byId(id)), 'id')
2022-06-10 10:47:46 +00:00
ids.forEach(id => this.vault.delete(id))
2022-04-15 14:24:30 +00:00
},
isVarious: (artist: Artist | number) => (typeof artist === 'number')
? artist === VARIOUS_ARTISTS_ID
: artist.id === VARIOUS_ARTISTS_ID,
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
isUnknown: (artist: Artist | number) => (typeof artist === 'number')
? artist === UNKNOWN_ARTIST_ID
: artist.id === UNKNOWN_ARTIST_ID,
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
isStandard (artist: Artist | number) {
return !this.isVarious(artist) && !this.isUnknown(artist)
2022-04-15 14:24:30 +00:00
},
async uploadImage (artist: Artist, image: string) {
artist.image = (await httpService.put<{ imageUrl: string }>(`artist/${artist.id}/image`, { image })).imageUrl
2022-07-22 21:56:13 +00:00
// sync to vault
this.byId(artist.id).image = artist.image
2022-06-10 10:47:46 +00:00
return artist.image
2022-04-15 14:24:30 +00:00
},
2022-06-10 10:47:46 +00:00
syncWithVault (artists: Artist | Artist[]) {
return arrayify(artists).map(artist => {
let local = this.vault.get(artist.id)
local = local ? Object.assign(local, artist) : reactive(artist)
this.vault.set(artist.id, local)
2022-04-28 09:00:20 +00:00
2022-06-10 10:47:46 +00:00
return local
})
2022-04-15 14:24:30 +00:00
},
2022-06-10 10:47:46 +00:00
async resolve (id: number) {
let artist = this.byId(id)
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
if (!artist) {
2022-07-24 11:47:18 +00:00
try {
artist = this.syncWithVault(
2022-07-25 13:25:27 +00:00
await cache.remember<Artist>(['artist', id], async () => await httpService.get<Artist>(`artists/${id}`))
2022-07-24 11:47:18 +00:00
)[0]
} catch (e) {
logger.error(e)
}
2022-04-15 14:24:30 +00:00
}
2022-06-10 10:47:46 +00:00
return artist
2022-04-15 14:24:30 +00:00
},
2022-07-22 22:03:25 +00:00
async paginate (page: number) {
2022-06-10 10:47:46 +00:00
const resource = await httpService.get<PaginatorResource>(`artists?page=${page}`)
2022-07-22 21:56:13 +00:00
this.state.artists = unionBy(this.state.artists, this.syncWithVault(resource.data), 'id')
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
return resource.links.next ? ++resource.meta.current_page : null
2022-04-15 14:24:30 +00:00
},
2022-06-10 10:47:46 +00:00
getMostPlayed (count: number) {
return take(
orderBy(Array.from(this.vault.values()).filter(artist => this.isStandard(artist)), 'play_count', 'desc'),
count
)
2022-04-15 14:24:30 +00:00
}
}