From 29f0adf2156e6242d607bc2e7af6c6fb5cc7fa46 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello <8296030+Piccirello@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:07:15 -0700 Subject: [PATCH] WebUI: Restore search tabs on load This PR restores searches previously performed in the same browser (via local storage). PR #20637. --- src/webui/www/private/views/search.html | 31 +++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/webui/www/private/views/search.html b/src/webui/www/private/views/search.html index 558f96b72..084762bc6 100644 --- a/src/webui/www/private/views/search.html +++ b/src/webui/www/private/views/search.html @@ -322,6 +322,12 @@ } } }).activate(); + + // restore search tabs + const searchJobs = JSON.parse(LocalPreferences.get('search_jobs', '[]')); + for (const { id, pattern } of searchJobs) { + createSearchTab(id, pattern); + } }; const numSearchTabs = function() { @@ -406,6 +412,21 @@ tab.destroy(); + new Request({ + url: new URI('api/v2/search/delete'), + method: 'post', + data: { + id: searchId + }, + }).send(); + + const searchJobs = JSON.parse(LocalPreferences.get('search_jobs', '[]')); + const jobIndex = searchJobs.findIndex((job) => job.id === searchId); + if (jobIndex >= 0) { + searchJobs.splice(jobIndex, 1); + LocalPreferences.set('search_jobs', JSON.stringify(searchJobs)); + } + if (numSearchTabs() === 0) { resetSearchState(); resetFilters(); @@ -559,6 +580,10 @@ $('startSearchButton').set('text', 'QBT_TR(Stop)QBT_TR[CONTEXT=SearchEngineWidget]'); const searchId = response.id; createSearchTab(searchId, pattern); + + const searchJobs = JSON.parse(LocalPreferences.get('search_jobs', '[]')); + searchJobs.push({ id: searchId, pattern: pattern }); + LocalPreferences.set('search_jobs', JSON.stringify(searchJobs)); } }).send(); }; @@ -799,7 +824,9 @@ const searchPluginsEmpty = (searchPlugins.length === 0); if (!searchPluginsEmpty) { $('searchResultsNoPlugins').style.display = "none"; - $('searchResultsNoSearches').style.display = "block"; + if (numSearchTabs() === 0) { + $('searchResultsNoSearches').style.display = "block"; + } // sort plugins alphabetically const allPlugins = searchPlugins.sort((left, right) => { @@ -921,7 +948,7 @@ offset: state.rowId }, onFailure: function(response) { - if (response.status === 400) { + if ((response.status === 400) || (response.status === 404)) { // bad params. search id is invalid resetSearchState(searchId); updateStatusIconElement(searchId, 'QBT_TR(An error occurred during search...)QBT_TR[CONTEXT=SearchJobWidget]', 'images/error.svg');