Skip to content

Commit 607916e

Browse files
committed
Add "exclude inactive" to connections filtering
1 parent 3ef6576 commit 607916e

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

packages/compass-sidebar/src/components/multiple-connections/connections-navigation.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ const ConnectionsNavigation: React.FC<ConnectionsNavigationProps> = ({
273273
filterRegex,
274274
fetchAllCollections,
275275
onDatabaseExpand,
276+
excludeInactive,
276277
});
277278

278279
const connectionListTitleActions =

packages/compass-sidebar/src/components/use-filtered-connections.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ type FilteredConnection = (
4242

4343
const filterConnections = (
4444
connections: SidebarConnection[],
45-
regex: RegExp
45+
regex: RegExp | null,
46+
excludeInactive: boolean
4647
): FilteredConnection[] => {
4748
const results: FilteredConnection[] = [];
4849
for (const connection of connections) {
49-
const isMatch = regex.test(connection.name);
50+
// Conditionally skip connections that aren't considered active
51+
const inactive =
52+
connection.connectionStatus !== 'connected' &&
53+
connection.connectionStatus !== 'connecting';
54+
if (excludeInactive && inactive) {
55+
continue;
56+
}
57+
const isMatch = !regex || regex.test(connection.name);
5058
let childMatches: FilteredDatabase[] = [];
5159
if (connection.connectionStatus === 'connected') {
5260
childMatches = filterDatabases(connection.databases, regex);
@@ -72,11 +80,11 @@ const filterConnections = (
7280

7381
const filterDatabases = (
7482
databases: SidebarDatabase[],
75-
regex: RegExp
83+
regex: RegExp | null
7684
): FilteredDatabase[] => {
7785
const results: FilteredDatabase[] = [];
7886
for (const db of databases) {
79-
const isMatch = regex.test(db.name);
87+
const isMatch = !regex || regex.test(db.name);
8088
const childMatches = filterCollections(db.collections, regex);
8189

8290
if (isMatch || childMatches.length) {
@@ -89,7 +97,7 @@ const filterDatabases = (
8997
? childMatches
9098
: db.collections.map((collection) => ({
9199
...collection,
92-
isMatch: regex.test(collection.name),
100+
isMatch: !regex || regex.test(collection.name),
93101
}));
94102
results.push({
95103
...db,
@@ -103,10 +111,10 @@ const filterDatabases = (
103111

104112
const filterCollections = (
105113
collections: SidebarCollection[],
106-
regex: RegExp
114+
regex: RegExp | null
107115
): FilteredCollection[] => {
108116
return collections
109-
.filter(({ name }) => regex.test(name))
117+
.filter(({ name }) => !regex || regex.test(name))
110118
.map((collection) => ({ ...collection, isMatch: true }));
111119
};
112120

@@ -205,7 +213,8 @@ const FILTER_CONNECTIONS =
205213
interface FilterConnectionsAction {
206214
type: typeof FILTER_CONNECTIONS;
207215
connections: SidebarConnection[];
208-
filterRegex: RegExp;
216+
filterRegex: RegExp | null;
217+
excludeInactive: boolean;
209218
}
210219

211220
const CLEAR_FILTER = 'sidebar/active-connections/CLEAR_FILTER' as const;
@@ -265,7 +274,8 @@ const connectionsReducer = (
265274
case FILTER_CONNECTIONS: {
266275
const filtered = filterConnections(
267276
action.connections,
268-
action.filterRegex
277+
action.filterRegex,
278+
action.excludeInactive
269279
);
270280
const persistingExpanded = revertTemporaryExpanded(state.expanded);
271281
return {
@@ -381,11 +391,13 @@ function filteredConnectionsToSidebarConnection(
381391
export const useFilteredConnections = ({
382392
connections,
383393
filterRegex,
394+
excludeInactive,
384395
fetchAllCollections,
385396
onDatabaseExpand,
386397
}: {
387398
connections: SidebarConnection[];
388399
filterRegex: RegExp | null;
400+
excludeInactive: boolean;
389401
fetchAllCollections: () => void;
390402
onDatabaseExpand: (connectionId: string, databaseId: string) => void;
391403
}): UseFilteredConnectionsHookResult => {
@@ -410,11 +422,12 @@ export const useFilteredConnections = ({
410422
// filter updates
411423
// connections change often, but the effect only uses connections if the filter is active
412424
// so we use this conditional dependency to avoid too many calls
413-
const connectionsButOnlyIfFilterIsActive = filterRegex && connections;
425+
const connectionsWhenFiltering =
426+
(filterRegex || excludeInactive) && connections;
414427
useEffect(() => {
415-
if (!filterRegex) {
428+
if (!filterRegex && !excludeInactive) {
416429
dispatch({ type: CLEAR_FILTER });
417-
} else if (connectionsButOnlyIfFilterIsActive) {
430+
} else if (connectionsWhenFiltering) {
418431
// the above check is extra just to please TS
419432

420433
// When filtering, emit an event so that we can fetch all collections. This
@@ -424,11 +437,17 @@ export const useFilteredConnections = ({
424437

425438
dispatch({
426439
type: FILTER_CONNECTIONS,
427-
connections: connectionsButOnlyIfFilterIsActive,
440+
connections: connectionsWhenFiltering,
428441
filterRegex,
442+
excludeInactive,
429443
});
430444
}
431-
}, [filterRegex, connectionsButOnlyIfFilterIsActive, fetchAllCollections]);
445+
}, [
446+
filterRegex,
447+
excludeInactive,
448+
connectionsWhenFiltering,
449+
fetchAllCollections,
450+
]);
432451

433452
const onConnectionToggle = useCallback(
434453
(connectionId: string, expand: boolean) =>

0 commit comments

Comments
 (0)