From 7e2deaac5bdbbb6519d4c28a0ded65e33a60802f Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Tue, 27 Feb 2024 13:50:06 +0530 Subject: [PATCH] fix: duplicate collection in search results Ensure the entire collection tree is rendered if the search query matches a collection name. --- .../providers/personal.workspace.ts | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts index fdba5823a..ddfa549a2 100644 --- a/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts +++ b/packages/hoppscotch-common/src/services/new-workspace/providers/personal.workspace.ts @@ -964,40 +964,39 @@ export class PersonalWorkspaceProviderService const isMatch = (inputText: string, textToMatch: string) => inputText.toLowerCase().includes(textToMatch.toLowerCase()) - // Recursive function to filter requests and folders - const filterItems = (items: HoppCollection[], searchQuery: string) => { - const filteredItems = [] + const filterRequests = (requests: HoppRESTRequest[]) => { + return requests.filter((request) => + isMatch(request.name, searchQuery.value) + ) + } - for (const item of items) { - if (isMatch(item.name, searchQuery)) { - filteredItems.push(item) - } - - if (item.requests) { - const filteredRequests = item.requests.filter((request) => - isMatch(request.name, searchQuery) - ) - - if (filteredRequests.length > 0) { - const filteredItem = { ...item, requests: filteredRequests } - filteredItems.push(filteredItem) + const filterChildCollections = ( + childCollections: HoppCollection[] + ): HoppCollection[] => { + return childCollections + .map((childCollection) => { + // Render the entire collection tree if the search query matches a collection name + if (isMatch(childCollection.name, searchQuery.value)) { + return childCollection } - } - if (item.folders) { - const filteredFolders: HoppCollection[] = filterItems( - item.folders, - searchQuery + const requests = filterRequests( + childCollection.requests as HoppRESTRequest[] ) + const folders = filterChildCollections(childCollection.folders) - if (filteredFolders.length > 0) { - const filteredItem = { ...item, folders: filteredFolders } - filteredItems.push(filteredItem) + return { + ...childCollection, + requests, + folders, } - } - } - - return filteredItems + }) + .filter( + (childCollection) => + childCollection.requests.length > 0 || + childCollection.folders.length > 0 || + isMatch(childCollection.name, searchQuery.value) + ) } const scopeHandle = effectScope() @@ -1013,24 +1012,27 @@ export class PersonalWorkspaceProviderService const filteredCollections = this.restCollectionState.value.state .map((collection) => { - const filteredCollection = { ...collection } + // Render the entire collection tree if the search query matches a collection name + if (isMatch(collection.name, searchQuery.value)) { + return collection + } - filteredCollection.requests = collection.requests.filter( - (request) => isMatch(request.name, newSearchQuery) + const requests = filterRequests( + collection.requests as HoppRESTRequest[] ) + const folders = filterChildCollections(collection.folders) - filteredCollection.folders = filterItems( - collection.folders, - newSearchQuery - ) - - return filteredCollection + return { + ...collection, + requests, + folders, + } }) .filter( (collection) => collection.requests.length > 0 || collection.folders.length > 0 || - isMatch(collection.name, newSearchQuery) + isMatch(collection.name, searchQuery.value) ) results.value = filteredCollections