From 42934f42994c37838e0a1eacee30c3fc9eda7574 Mon Sep 17 00:00:00 2001 From: itepechi <72330683+itepechi@users.noreply.github.com> Date: Fri, 3 Nov 2023 06:17:28 +0900 Subject: [PATCH] Show search results asynchronously --- src/components/search/search.js | 112 +++++++++++++++++++------------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/src/components/search/search.js b/src/components/search/search.js index f24ad349..fcb5466d 100644 --- a/src/components/search/search.js +++ b/src/components/search/search.js @@ -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