Show search results asynchronously

This commit is contained in:
itepechi 2023-11-03 06:17:28 +09:00
parent 8b63ade65b
commit 42934f4299
1 changed files with 67 additions and 45 deletions

View File

@ -95,7 +95,7 @@ const Search = {
this.$router.push({ name: 'search', query: { query } })
this.$refs.searchInput.focus()
},
search (query, searchType = null) {
async search (query, searchType = null) {
if (!query) {
this.loading = false
return
@ -116,6 +116,20 @@ const Search = {
this.lastMediaFetchCount = 0
}
const searchTypes = searchType
? [searchType]
: ['statuses', 'media', 'accounts', 'hashtags']
let oldStasusesLength = this.statuses.length
let oldMediaLength = this.media.length
let skipMediaSearch = !this.canSearchMediaPosts
for (const searchType of searchTypes) {
if (searchType === 'media' && skipMediaSearch) {
continue
}
let searchOffset
if (searchType === 'statuses') {
searchOffset = this.statusesOffset
@ -123,7 +137,7 @@ const Search = {
searchOffset = this.mediaOffset
}
this.$store.dispatch('search', {
const data = await this.$store.dispatch('search', {
q: query,
resolve: true,
offset: searchOffset,
@ -131,11 +145,13 @@ const Search = {
following:
'followingOnly' in this.filter && this.filter.followingOnly
})
.then(data => {
this.loading = false
let oldStasusesLength = this.statuses.length
let oldMediaLength = this.media.length
for (const value of Object.values(data)) {
if (value.length) {
this.loading = true
break
}
}
// Always append to old results. If new results are empty, this doesn't change anything
this.userIds = this.userIds.concat(map(data.accounts, 'id'))
@ -147,21 +163,27 @@ const Search = {
if (isNewSearch) {
this.currenResultTab = this.getActiveTab()
if (searchType === 'statuses' && data.statuses.length === 0) {
// safe to assume that there are no media posts
skipMediaSearch = true
}
}
this.loadedInitially = true
if (!searchType || searchType === 'statuses') {
if (searchType === 'statuses') {
// Offset from whatever we already have
this.statusesOffset = this.statuses.length
// Because the amount of new statuses can actually be zero, compare to old lenght instead
// Because the amount of new statuses can actually be zero, compare to old length instead
this.lastStatusFetchCount = this.statuses.length - oldStasusesLength
}
if (!searchType || searchType === 'media') {
if (searchType === 'media') {
this.mediaOffset = this.media.length
this.lastMediaFetchCount = this.media.length - oldMediaLength
}
}
this.lastQuery = query
})
this.loading = false
},
resultCount (tabName) {
const length = this[tabName].length