From 93ea1f0659fe549227ee5a31392cb937a5f7e095 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 23 Jan 2023 15:28:53 -0500 Subject: [PATCH] Implement loading more statuses when searching --- src/components/search/search.js | 35 +++++++++++++++++++++----- src/components/search/search.vue | 42 ++++++++++++++++++++++++++------ src/i18n/en.json | 4 ++- src/modules/statuses.js | 4 +-- src/services/api/api.service.js | 6 ++++- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/components/search/search.js b/src/components/search/search.js index b62bc2c5..360b8447 100644 --- a/src/components/search/search.js +++ b/src/components/search/search.js @@ -7,6 +7,7 @@ import { faCircleNotch, faSearch } from '@fortawesome/free-solid-svg-icons' +import { uniqBy } from 'lodash' library.add( faCircleNotch, @@ -30,7 +31,10 @@ const Search = { userIds: [], statuses: [], hashtags: [], - currenResultTab: 'statuses' + currenResultTab: 'statuses', + statusesOffset: 0, + lastStatusFetchCount: 0, + lastQuery: '' } }, computed: { @@ -59,7 +63,7 @@ const Search = { this.$router.push({ name: 'search', query: { query } }) this.$refs.searchInput.focus() }, - search (query) { + search (query, searchType = null) { if (!query) { this.loading = false return @@ -70,15 +74,34 @@ const Search = { this.statuses = [] this.hashtags = [] this.$refs.searchInput.blur() + if (this.lastQuery !== query) { + this.userIds = [] + this.hashtags = [] + this.statuses = [] - this.$store.dispatch('search', { q: query, resolve: true }) + this.statusesOffset = 0 + this.lastStatusFetchCount = 0 + } + + this.$store.dispatch('search', { q: query, resolve: true, offset: this.statusesOffset, 'type': searchType }) .then(data => { this.loading = false - this.userIds = map(data.accounts, 'id') - this.statuses = data.statuses - this.hashtags = data.hashtags + + let oldLength = this.statuses.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') + this.hashtags = this.hashtags.concat(data.hashtags) + this.currenResultTab = this.getActiveTab() this.loaded = 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 + this.lastQuery = query }) }, resultCount (tabName) { diff --git a/src/components/search/search.vue b/src/components/search/search.vue index b7bfc1f3..81c180ba 100644 --- a/src/components/search/search.vue +++ b/src/components/search/search.vue @@ -22,7 +22,7 @@
-
-
-

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

-
+
+ +
+ +
+
+

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

+
diff --git a/src/i18n/en.json b/src/i18n/en.json index 1572d78e..ca232320 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -832,7 +832,9 @@ "hashtags": "Hashtags", "person_talking": "{count} person talking", "people_talking": "{count} people talking", - "no_results": "No results" + "no_results": "No results", + "no_more_results": "No more results", + "load_more": "Load more results" }, "password_reset": { "forgot_password": "Forgot password?", diff --git a/src/modules/statuses.js b/src/modules/statuses.js index ac5d25c4..69f8af3a 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -747,8 +747,8 @@ const statuses = { rootState.api.backendInteractor.fetchRebloggedByUsers({ id }) .then(rebloggedByUsers => commit('addRepeats', { id, rebloggedByUsers, currentUser: rootState.users.currentUser })) }, - search (store, { q, resolve, limit, offset, following }) { - return store.rootState.api.backendInteractor.search2({ q, resolve, limit, offset, following }) + search (store, { q, resolve, limit, offset, following, type }) { + return store.rootState.api.backendInteractor.search2({ q, resolve, limit, offset, following, type }) .then((data) => { store.commit('addNewUsers', data.accounts) store.commit('addNewStatuses', { statuses: data.statuses }) diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 7e53d45c..e3e66f91 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -1002,7 +1002,7 @@ const searchUsers = ({ credentials, query }) => { .then((data) => data.map(parseUser)) } -const search2 = ({ credentials, q, resolve, limit, offset, following }) => { +const search2 = ({ credentials, q, resolve, limit, offset, following, type }) => { let url = MASTODON_SEARCH_2 let params = [] @@ -1026,6 +1026,10 @@ const search2 = ({ credentials, q, resolve, limit, offset, following }) => { params.push(['following', true]) } + if (type) { + params.push(['following', type]) + } + params.push(['with_relationships', true]) let queryString = map(params, (param) => `${param[0]}=${param[1]}`).join('&')