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

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-04-21 16:06:45 +00:00
import { reactive } from 'vue'
import { http } from '@/services'
2024-05-19 05:49:42 +00:00
import { albumStore, artistStore, podcastStore, songStore } from '@/stores'
2022-04-15 14:24:30 +00:00
2022-07-23 11:07:14 +00:00
type ExcerptState = {
2024-05-19 05:49:42 +00:00
playables: Playable[]
albums: Album[]
2022-07-23 11:07:14 +00:00
artists: Artist[]
2024-05-19 05:49:42 +00:00
podcasts: Podcast[]
2022-07-23 11:07:14 +00:00
}
2024-05-19 05:49:42 +00:00
export type ExcerptSearchResult = {
songs: Playable[] // backward compatibility
albums: Album[]
artists: Artist[]
podcasts: Podcast[]
}
2022-07-23 11:07:14 +00:00
2022-04-15 14:24:30 +00:00
export const searchStore = {
2022-04-21 16:06:45 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
excerpt: {
2024-05-19 05:49:42 +00:00
playables: [],
2022-07-23 11:07:14 +00:00
albums: [],
2024-05-19 05:49:42 +00:00
artists: [],
podcasts: []
2022-07-23 11:07:14 +00:00
} as ExcerptState,
2024-05-19 05:49:42 +00:00
songs: [] as Playable[]
2022-04-21 16:06:45 +00:00
}),
2022-04-15 14:24:30 +00:00
2022-04-21 16:06:45 +00:00
async excerptSearch (q: string) {
const result = await http.get<ExcerptSearchResult>(`search?q=${q}`)
2022-06-10 10:47:46 +00:00
2024-05-19 05:49:42 +00:00
this.state.excerpt.playables = songStore.syncWithVault(result.songs)
2022-06-10 10:47:46 +00:00
this.state.excerpt.albums = albumStore.syncWithVault(result.albums)
this.state.excerpt.artists = artistStore.syncWithVault(result.artists)
2024-05-19 05:49:42 +00:00
this.state.excerpt.podcasts = result.podcasts
2022-04-15 14:24:30 +00:00
},
2022-04-21 16:06:45 +00:00
async songSearch (q: string) {
this.state.songs = songStore.syncWithVault(await http.get<Song[]>(`search/songs?q=${q}`))
2022-04-15 14:24:30 +00:00
},
resetSongResultState () {
this.state.songs = []
}
}