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

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-04-21 16:06:45 +00:00
import { reactive } from 'vue'
import { http } from '@/services'
import { albumStore, artistStore, songStore } from '@/stores'
2022-04-15 14:24:30 +00:00
interface 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
}
export interface ExcerptSearchResult {
2024-05-19 05:49:42 +00:00
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,
playables: [] 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
},
async playableSearch (q: string) {
this.state.playables = songStore.syncWithVault(await http.get<Playable[]>(`search/songs?q=${q}`))
2022-04-15 14:24:30 +00:00
},
resetPlayableResultState () {
this.state.playables = []
},
2022-04-15 14:24:30 +00:00
}