From 2f8fa91b32d13e01e808556313329de76a4a1d68 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Tue, 20 Aug 2024 10:46:44 +0800 Subject: [PATCH] * Make channel page remember last query string only when going back --- .../ChannelDetails/ChannelDetails.vue | 7 ++- src/renderer/views/Channel/Channel.js | 62 ++++++++++++++++--- src/renderer/views/Channel/Channel.vue | 3 +- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/renderer/components/ChannelDetails/ChannelDetails.vue b/src/renderer/components/ChannelDetails/ChannelDetails.vue index c6574c40d81c1..5b7897a5d7dde 100644 --- a/src/renderer/components/ChannelDetails/ChannelDetails.vue +++ b/src/renderer/components/ChannelDetails/ChannelDetails.vue @@ -206,6 +206,7 @@ v-if="showSearchBar" ref="searchBar" :placeholder="$t('Channel.Search Channel')" + :value="props.query" :show-clear-text-button="true" class="channelSearch" :maxlength="255" @@ -274,7 +275,11 @@ const props = defineProps({ currentTab: { type: String, default: 'videos' - } + }, + query: { + type: String, + default: '' + }, }) const emit = defineEmits(['change-tab', 'search']) diff --git a/src/renderer/views/Channel/Channel.js b/src/renderer/views/Channel/Channel.js index 8116dbb705fe6..952d879976cb7 100644 --- a/src/renderer/views/Channel/Channel.js +++ b/src/renderer/views/Channel/Channel.js @@ -41,6 +41,7 @@ import { parseLocalSubscriberCount, getLocalArtistTopicChannelReleasesContinuation } from '../../helpers/api/local' +import { isNavigationFailure, NavigationFailureType } from 'vue-router' export default defineComponent({ name: 'Channel', @@ -57,6 +58,7 @@ export default defineComponent({ }, data: function () { return { + skipRouteChangeWatcherOnce: false, isLoading: true, isElementListLoading: false, currentTab: 'videos', @@ -281,6 +283,10 @@ export default defineComponent({ watch: { $route() { // react to route changes... + if (this.skipRouteChangeWatcherOnce) { + this.skipRouteChangeWatcherOnce = false + return + } this.isLoading = true if (this.$route.query.url) { @@ -337,8 +343,9 @@ export default defineComponent({ // Re-enable auto refresh on sort value change AFTER update done if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious') { - this.getChannelInfoInvidious() - this.autoRefreshOnSortByChangeEnabled = true + this.getChannelInfoInvidious().finally(() => { + this.autoRefreshOnSortByChangeEnabled = true + }) } else { this.getChannelLocal().finally(() => { this.autoRefreshOnSortByChangeEnabled = true @@ -415,9 +422,9 @@ export default defineComponent({ } } }, - mounted: function () { + mounted: async function () { if (this.$route.query.url) { - this.resolveChannelUrl(this.$route.query.url, this.$route.params.currentTab) + await this.resolveChannelUrl(this.$route.query.url, this.$route.params.currentTab) return } @@ -433,13 +440,19 @@ export default defineComponent({ // Enable auto refresh on sort value change AFTER initial update done if (!process.env.SUPPORTS_LOCAL_API || this.backendPreference === 'invidious') { - this.getChannelInfoInvidious() - this.autoRefreshOnSortByChangeEnabled = true + await this.getChannelInfoInvidious().finally(() => { + this.autoRefreshOnSortByChangeEnabled = true + }) } else { - this.getChannelLocal().finally(() => { + await this.getChannelLocal().finally(() => { this.autoRefreshOnSortByChangeEnabled = true }) } + + const oldQuery = this.$route.query.searchQueryText ?? '' + if (oldQuery !== null && oldQuery !== '') { + this.newSearch(oldQuery) + } }, methods: { resolveChannelUrl: async function (url, tab = undefined) { @@ -963,7 +976,7 @@ export default defineComponent({ this.channelInstance = null const expectedId = this.id - invidiousGetChannelInfo(this.id).then((response) => { + return invidiousGetChannelInfo(this.id).then((response) => { if (expectedId !== this.id) { return } @@ -1850,6 +1863,10 @@ export default defineComponent({ break } }, + newSearchWithStatePersist(query) { + this.saveStateInRouter(query) + this.newSearch(query) + }, searchChannelLocal: async function () { const isNewSearch = this.searchContinuationData === null @@ -1941,6 +1958,35 @@ export default defineComponent({ }) }, + async saveStateInRouter(query) { + this.skipRouteChangeWatcherOnce = true + if (query === '') { + await this.$router.replace({ path: `/channel/${this.id}` }).catch(failure => { + if (isNavigationFailure(failure, NavigationFailureType.duplicated)) { + return + } + + throw failure + }) + return + } + + await this.$router.replace({ + path: `/channel/${this.id}`, + query: { + currentTab: 'search', + searchQueryText: query, + }, + }).catch(failure => { + if (isNavigationFailure(failure, NavigationFailureType.duplicated)) { + return + } + + throw failure + }) + this.skipRouteChangeWatcherOnce = false + }, + getIconForSortPreference: (s) => getIconForSortPreference(s), ...mapActions([ diff --git a/src/renderer/views/Channel/Channel.vue b/src/renderer/views/Channel/Channel.vue index c173205a6d50d..44ff15986da11 100644 --- a/src/renderer/views/Channel/Channel.vue +++ b/src/renderer/views/Channel/Channel.vue @@ -17,9 +17,10 @@ :is-subscribed="isSubscribed" :visible-tabs="tabInfoValues" :current-tab="currentTab" + :query="lastSearchQuery" class="card channelDetails" @change-tab="changeTab" - @search="newSearch" + @search="(v) => newSearchWithStatePersist(v)" />