diff --git a/src/components/search/search.js b/src/components/search/search.js index 0c00b164..710e04a5 100644 --- a/src/components/search/search.js +++ b/src/components/search/search.js @@ -27,16 +27,20 @@ const Search = { ], data () { return { - loaded: false, + loadedInitially: false, loading: false, searchTerm: this.query || '', + mediaSearchSupported: false, userIds: [], statuses: [], + media: [], hashtags: [], currenResultTab: 'statuses', statusesOffset: 0, lastStatusFetchCount: 0, + mediaOffset: 0, + lastMediaFetchCount: 0, lastQuery: '' } }, @@ -50,6 +54,13 @@ const Search = { return this.statuses.filter(status => allStatusesObject[status.id] && !allStatusesObject[status.id].deleted ) + }, + visibleMedia () { + const allStatusesObject = this.$store.state.statuses.allStatusesObject + + return this.media.filter(status => + allStatusesObject[status.id] && !allStatusesObject[status.id].deleted + ) } }, mounted () { @@ -72,61 +83,92 @@ const Search = { return } + const isNewSearch = this.lastQuery !== query this.loading = true this.$refs.searchInput.blur() - if (this.lastQuery !== query) { + if (isNewSearch) { this.userIds = [] this.hashtags = [] this.statuses = [] + this.media = [] this.statusesOffset = 0 this.lastStatusFetchCount = 0 + this.mediaOffset = 0 + this.lastMediaFetchCount = 0 } - this.$store.dispatch('search', { q: query, resolve: true, offset: this.statusesOffset, 'type': searchType }) + let searchOffset + if (searchType === 'statuses') { + searchOffset = this.statusesOffset + } else if (searchType === 'media') { + searchOffset = this.mediaOffset + } + + this.$store.dispatch('search', { + q: query, + resolve: true, + offset: searchOffset, + 'type': searchType, + }) .then(data => { this.loading = false - let oldLength = this.statuses.length + 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.mediaSearchSupported = true + this.media = uniqBy(this.media.concat(data.media), 'id') + } this.hashtags = this.hashtags.concat(data.hashtags) - this.currenResultTab = this.getActiveTab() - this.loaded = true + if (isNewSearch) { + this.currenResultTab = this.getActiveTab() + } + this.loadedInitially = true - // 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 - oldLength + 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 }) }, resultCount (tabName) { const length = this[tabName].length - return ( - length === 0 - ? '' - : ` (${ - length + ( - tabName === "visibleStatuses" && - this.lastStatusFetchCount !== 0 && - !this.loading && - this.loaded - ? '+' - : '' - ) - })` - ) + + if (length === 0 || !this.loadedInitially) { + return '' + } + + if ( + (tabName === 'visibleStatuses' && this.lastStatusFetchCount !== 0) || + (tabName === 'visibleMedia' && this.lastMediaFetchCount !== 0) + ) { + return ` (${length}+)` + } + + return ` (${length})` }, onResultTabSwitch (key) { this.currenResultTab = key + this.loading = false }, getActiveTab () { if (this.visibleStatuses.length > 0) { return 'statuses' + } else if (this.visibleMedia.length > 0) { + return 'media' } else if (this.users.length > 0) { return 'people' } else if (this.hashtags.length > 0) { diff --git a/src/components/search/search.vue b/src/components/search/search.vue index 11db6154..ea601fff 100644 --- a/src/components/search/search.vue +++ b/src/components/search/search.vue @@ -24,7 +24,13 @@
-
+
+

@@ -95,9 +106,48 @@

+
+ + +
+ +
+
+

+ {{ visibleMedia.length === 0 ? $t('search.no_results') : $t('search.no_more_results') }} +

+
+

{{ $t('search.no_results') }}

@@ -111,7 +161,7 @@

{{ $t('search.no_results') }}

diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index de21ef3b..c4fb1e4f 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -1424,6 +1424,9 @@ const search2 = ({ credentials, q, resolve, limit, offset, following, type }) => .then((data) => { data.accounts = data.accounts.slice(0, limit).map(u => parseUser(u)) data.statuses = data.statuses.slice(0, limit).map(s => parseStatus(s)) + if ('media' in data) { + data.media = data.media.slice(0, limit).map(s => parseStatus(s)) + } return data }) }