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,52 +116,74 @@ const Search = {
this.lastMediaFetchCount = 0
}
let searchOffset
if (searchType === 'statuses') {
searchOffset = this.statusesOffset
} else if (searchType === 'media') {
searchOffset = this.mediaOffset
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
} else if (searchType === 'media') {
searchOffset = this.mediaOffset
}
const data = await this.$store.dispatch('search', {
q: query,
resolve: true,
offset: searchOffset,
'type': searchType,
following:
'followingOnly' in this.filter && this.filter.followingOnly
})
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'))
this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id')
if ('media' in data) {
this.media = uniqBy(this.media.concat(data.media), 'id')
}
this.hashtags = this.hashtags.concat(data.hashtags)
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 === '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 length instead
this.lastStatusFetchCount = this.statuses.length - oldStasusesLength
}
if (searchType === 'media') {
this.mediaOffset = this.media.length
this.lastMediaFetchCount = this.media.length - oldMediaLength
}
}
this.$store.dispatch('search', {
q: query,
resolve: true,
offset: searchOffset,
'type': searchType,
following:
'followingOnly' in this.filter && this.filter.followingOnly
})
.then(data => {
this.loading = false
let oldStasusesLength = this.statuses.length
let oldMediaLength = this.media.length
// Always append to old results. If new results are empty, this doesn't change anything
this.userIds = this.userIds.concat(map(data.accounts, 'id'))
this.statuses = uniqBy(this.statuses.concat(data.statuses), 'id')
if ('media' in data) {
this.media = uniqBy(this.media.concat(data.media), 'id')
}
this.hashtags = this.hashtags.concat(data.hashtags)
if (isNewSearch) {
this.currenResultTab = this.getActiveTab()
}
this.loadedInitially = true
if (!searchType || 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
this.lastStatusFetchCount = this.statuses.length - oldStasusesLength
}
if (!searchType || searchType === 'media') {
this.mediaOffset = this.media.length
this.lastMediaFetchCount = this.media.length - oldMediaLength
}
this.lastQuery = query
})
this.lastQuery = query
this.loading = false
},
resultCount (tabName) {
const length = this[tabName].length