From e18c41f8a7919f84fa0bc1e3084d9b8c4b529c91 Mon Sep 17 00:00:00 2001 From: Raymond Lo Date: Sun, 11 May 2025 03:03:58 -0700 Subject: [PATCH 1/2] feat(sidebar): filter both databases and collections with dot notation --- .../use-filtered-connections.spec.ts | 37 +++++++++++++++++++ .../components/use-filtered-connections.ts | 10 +++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts b/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts index 42b93b40aa5..392da923ab1 100644 --- a/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts +++ b/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts @@ -53,6 +53,14 @@ const sidebarConnections: SidebarConnection[] = [ pipeline: [], isNonExistent: false, }, + { + _id: 'coll_ready_1_1_2', + name: 'coll_ready_shared_name', + type: 'collection', + sourceName: '', + pipeline: [], + isNonExistent: false, + }, ], collectionsLength: 1, collectionsStatus: 'ready', @@ -70,6 +78,14 @@ const sidebarConnections: SidebarConnection[] = [ pipeline: [], isNonExistent: false, }, + { + _id: 'coll_ready_1_2_2', + name: 'coll_ready_shared_name', + type: 'collection', + sourceName: '', + pipeline: [], + isNonExistent: false, + }, ], collectionsLength: 1, collectionsStatus: 'ready', @@ -626,6 +642,27 @@ describe('useFilteredConnections', function () { }); }); + it('should filter collection items and database items using dot notation', async function () { + const { result } = renderHookWithContext(useFilteredConnections, { + initialProps: { + connections: mockSidebarConnections, + filter: { + regex: new RegExp('ready_1_1.coll_ready_shared_name', 'i'), // this matches only coll_ready_shared_name collection in ready_1_1 database + excludeInactive: false, + }, + fetchAllCollections: fetchAllCollectionsStub, + onDatabaseExpand: onDatabaseExpandStub, + }, + }); + + await waitFor(() => { + expect( + (result.current.filtered?.[0] as SidebarConnectedConnection) + .databases[0].collections + ).to.have.length(1); // the result has 1 collection + }); + }); + it('as expanded, it should return an object containing an expanded object for the matching items', async function () { const { result } = renderHookWithContext(useFilteredConnections, { initialProps: { diff --git a/packages/compass-sidebar/src/components/use-filtered-connections.ts b/packages/compass-sidebar/src/components/use-filtered-connections.ts index e2ffb8b1384..107b19b1b46 100644 --- a/packages/compass-sidebar/src/components/use-filtered-connections.ts +++ b/packages/compass-sidebar/src/components/use-filtered-connections.ts @@ -85,7 +85,7 @@ const filterDatabases = ( const results: FilteredDatabase[] = []; for (const db of databases) { const isMatch = !regex || regex.test(db.name); - const childMatches = filterCollections(db.collections, regex); + const childMatches = filterCollections(db.collections, regex, db.name); if (isMatch || childMatches.length) { // If the db doesn't match, we want to use just the matching collections. @@ -111,10 +111,14 @@ const filterDatabases = ( const filterCollections = ( collections: SidebarCollection[], - regex: RegExp | null + regex: RegExp | null, + dbName: string ): FilteredCollection[] => { return collections - .filter(({ name }) => !regex || regex.test(name)) + .filter( + ({ name }) => + !regex || regex.test(name) || regex.test(`${dbName}.${name}`) + ) .map((collection) => ({ ...collection, isMatch: true })); }; From 06ebee8e385688c79bca998a9972fb2805cf8ef8 Mon Sep 17 00:00:00 2001 From: Raymond Lo Date: Mon, 19 May 2025 12:55:35 -0700 Subject: [PATCH 2/2] chore(sidebar): fix regex in test for dot notation filter --- .../src/components/use-filtered-connections.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts b/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts index 392da923ab1..0c171690dd5 100644 --- a/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts +++ b/packages/compass-sidebar/src/components/use-filtered-connections.spec.ts @@ -647,7 +647,7 @@ describe('useFilteredConnections', function () { initialProps: { connections: mockSidebarConnections, filter: { - regex: new RegExp('ready_1_1.coll_ready_shared_name', 'i'), // this matches only coll_ready_shared_name collection in ready_1_1 database + regex: new RegExp('ready_1_1\\.coll_ready_shared_name', 'i'), // this matches only coll_ready_shared_name collection in ready_1_1 database excludeInactive: false, }, fetchAllCollections: fetchAllCollectionsStub,