Show search results asynchronously

This commit is contained in:
itepechi 2023-11-03 06:17:28 +09:00
parent 8b63ade65b
commit 42934f4299

View file

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