fix: duplicate collection in search results

Ensure the entire collection tree is rendered if the search query matches a collection name.
This commit is contained in:
jamesgeorge007
2024-02-27 13:50:06 +05:30
parent 240b131e06
commit 7e2deaac5b

View File

@@ -964,40 +964,39 @@ export class PersonalWorkspaceProviderService
const isMatch = (inputText: string, textToMatch: string) => const isMatch = (inputText: string, textToMatch: string) =>
inputText.toLowerCase().includes(textToMatch.toLowerCase()) inputText.toLowerCase().includes(textToMatch.toLowerCase())
// Recursive function to filter requests and folders const filterRequests = (requests: HoppRESTRequest[]) => {
const filterItems = (items: HoppCollection[], searchQuery: string) => { return requests.filter((request) =>
const filteredItems = [] isMatch(request.name, searchQuery.value)
)
}
for (const item of items) { const filterChildCollections = (
if (isMatch(item.name, searchQuery)) { childCollections: HoppCollection[]
filteredItems.push(item) ): HoppCollection[] => {
} return childCollections
.map((childCollection) => {
if (item.requests) { // Render the entire collection tree if the search query matches a collection name
const filteredRequests = item.requests.filter((request) => if (isMatch(childCollection.name, searchQuery.value)) {
isMatch(request.name, searchQuery) return childCollection
)
if (filteredRequests.length > 0) {
const filteredItem = { ...item, requests: filteredRequests }
filteredItems.push(filteredItem)
} }
}
if (item.folders) { const requests = filterRequests(
const filteredFolders: HoppCollection[] = filterItems( childCollection.requests as HoppRESTRequest[]
item.folders,
searchQuery
) )
const folders = filterChildCollections(childCollection.folders)
if (filteredFolders.length > 0) { return {
const filteredItem = { ...item, folders: filteredFolders } ...childCollection,
filteredItems.push(filteredItem) requests,
folders,
} }
} })
} .filter(
(childCollection) =>
return filteredItems childCollection.requests.length > 0 ||
childCollection.folders.length > 0 ||
isMatch(childCollection.name, searchQuery.value)
)
} }
const scopeHandle = effectScope() const scopeHandle = effectScope()
@@ -1013,24 +1012,27 @@ export class PersonalWorkspaceProviderService
const filteredCollections = this.restCollectionState.value.state const filteredCollections = this.restCollectionState.value.state
.map((collection) => { .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( const requests = filterRequests(
(request) => isMatch(request.name, newSearchQuery) collection.requests as HoppRESTRequest[]
) )
const folders = filterChildCollections(collection.folders)
filteredCollection.folders = filterItems( return {
collection.folders, ...collection,
newSearchQuery requests,
) folders,
}
return filteredCollection
}) })
.filter( .filter(
(collection) => (collection) =>
collection.requests.length > 0 || collection.requests.length > 0 ||
collection.folders.length > 0 || collection.folders.length > 0 ||
isMatch(collection.name, newSearchQuery) isMatch(collection.name, searchQuery.value)
) )
results.value = filteredCollections results.value = filteredCollections