Skip to content

Commit d91ca3c

Browse files
Satyarueckstiess
authored andcommitted
COMPASS-394: Collapse sidebar by default (#740)
* sidebar collapsed by default + filter expands on match * remove reliance of sidebar store on collections store * functional tests for sidebar changes
1 parent 7603d23 commit d91ca3c

File tree

6 files changed

+121
-46
lines changed

6 files changed

+121
-46
lines changed

src/internal-packages/database/lib/stores/collections-store.js

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const Reflux = require('reflux');
22
const StateMixin = require('reflux-state-mixin');
33
const CollectionsActions = require('../actions/collections-actions');
4+
const { NamespaceStore } = require('hadron-reflux-store');
45
const toNS = require('mongodb-ns');
56
const app = require('ampersand-app');
67
const _ = require('lodash');
@@ -38,7 +39,8 @@ const CollectionsStore = Reflux.createStore({
3839
* Initialize everything that is not part of the store's state.
3940
*/
4041
init() {
41-
this.listenToExternalStore('Sidebar.Store', this.onSidebarChange.bind(this));
42+
this.listenToExternalStore('App.InstanceStore', this.onInstanceChange.bind(this));
43+
NamespaceStore.listen(this.onNamespaceChanged.bind(this));
4244
this.indexes = [];
4345
},
4446

@@ -60,16 +62,20 @@ const CollectionsStore = Reflux.createStore({
6062
column || this.state.sortColumn, order || this.state.sortOrder);
6163
},
6264

63-
onSidebarChange(state) {
64-
// continue only when a database is the activeNamespace
65-
if (!state.activeNamespace || state.activeNamespace.includes('.')) {
66-
return;
67-
}
65+
setDatabaseCollections(databases, namespace) {
66+
const database = _.first(_.filter(databases.models, '_id', namespace));
6867

69-
// retrieve the collections from sidebar object
70-
const database = _.first(_.filter(state.databases, '_id', state.activeNamespace));
68+
const collections = database.collections.models.map(c => {
69+
return {
70+
_id: c._id,
71+
database: c.database,
72+
capped: c.capped,
73+
power_of_two: c.power_of_two,
74+
readonly: c.readonly
75+
};
76+
});
7177

72-
app.dataService.database(state.activeNamespace, {}, (err, res) => {
78+
app.dataService.database(namespace, {}, (err, res) => {
7379
if (err) {
7480
this.setState({
7581
collections: [],
@@ -80,31 +86,49 @@ const CollectionsStore = Reflux.createStore({
8086
return;
8187
}
8288
const unsorted = _(res.collections)
83-
.filter((coll) => {
84-
// skip system collections
85-
const ns = toNS(coll.ns);
86-
return !ns.system;
87-
})
88-
.map((coll) => {
89-
// re-format for table view
90-
return _.zipObject(COLL_COLUMNS, [
91-
coll.name, // Collection Name
92-
coll.document_count, // Num. Documents
93-
coll.size / coll.document_count, // Avg. Document Size
94-
coll.size, // Total Document Size
95-
coll.index_count, // Num Indexes
96-
coll.index_size // Total Index Size
97-
]);
98-
})
99-
.value();
89+
.filter((coll) => {
90+
// skip system collections
91+
const ns = toNS(coll.ns);
92+
return !ns.system;
93+
})
94+
.map((coll) => {
95+
// re-format for table view
96+
return _.zipObject(COLL_COLUMNS, [
97+
coll.name, // Collection Name
98+
coll.document_count, // Num. Documents
99+
coll.size / coll.document_count, // Avg. Document Size
100+
coll.size, // Total Document Size
101+
coll.index_count, // Num Indexes
102+
coll.index_size // Total Index Size
103+
]);
104+
})
105+
.value();
100106
this.setState({
101-
collections: database.collections,
107+
collections: collections,
102108
renderedCollections: this._sort(unsorted),
103-
database: state.activeNamespace
109+
database: namespace
104110
});
105111
});
106112
},
107113

114+
onNamespaceChanged(namespace) {
115+
if (!namespace || namespace.includes('.') || namespace === this.state.database) {
116+
return;
117+
}
118+
119+
this.setDatabaseCollections(app.instance.databases, namespace);
120+
},
121+
122+
onInstanceChange(state) {
123+
// continue only when a database is the activeNamespace
124+
const namespace = NamespaceStore.ns;
125+
if (!namespace || namespace.includes('.')) {
126+
return;
127+
}
128+
129+
this.setDatabaseCollections(state.instance.databases, namespace);
130+
},
131+
108132
sortCollections(column, order) {
109133
this.setState({
110134
renderedCollections: this._sort(this.state.renderedCollections, column, order),

src/internal-packages/sidebar/lib/components/sidebar-database.jsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ const ipc = require('hadron-ipc');
33
const React = require('react');
44
const SidebarCollection = require('./sidebar-collection');
55
const { NamespaceStore } = require('hadron-reflux-store');
6+
const toNS = require('mongodb-ns');
67

78
class SidebarDatabase extends React.Component {
8-
constructor() {
9-
super();
10-
this.state = { expanded: true };
9+
constructor(props) {
10+
super(props);
11+
this.state = { expanded: props.expanded };
1112
this.CollectionStore = app.appRegistry.getStore('App.CollectionStore');
1213
}
1314

15+
componentWillReceiveProps(nextProps) {
16+
this.setState({
17+
expanded: nextProps.expanded || toNS(nextProps.activeNamespace).database === this.props._id}
18+
);
19+
}
20+
1421
getCollectionComponents() {
1522
if (this.state.expanded) {
1623
return this.props.collections.map(c => {
@@ -75,7 +82,8 @@ class SidebarDatabase extends React.Component {
7582
SidebarDatabase.propTypes = {
7683
_id: React.PropTypes.string,
7784
activeNamespace: React.PropTypes.string.isRequired,
78-
collections: React.PropTypes.array
85+
collections: React.PropTypes.array,
86+
expanded: React.PropTypes.bool
7987
};
8088

8189
module.exports = SidebarDatabase;

src/internal-packages/sidebar/lib/components/sidebar.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Sidebar extends React.Component {
1919
try {
2020
re = new RegExp(searchString, 'i');
2121
} catch (e) {
22-
re = /.*/;
22+
re = /(?:)/;
2323
}
2424

2525
SidebarActions.filterDatabases(re);
@@ -36,14 +36,15 @@ class Sidebar extends React.Component {
3636
</StoreConnector>
3737
<div className="compass-sidebar-filter">
3838
<i className="fa fa-search compass-sidebar-search-icon"></i>
39-
<input className="compass-sidebar-search-input" placeholder="filter" onChange={this.handleFilter}></input>
39+
<input data-test-id="sidebar-filter-input" className="compass-sidebar-search-input" placeholder="filter" onChange={this.handleFilter}></input>
4040
</div>
4141
<div className="compass-sidebar-content">
4242
{
4343
this.props.databases.map(db => {
4444
const props = {
4545
_id: db._id,
4646
collections: db.collections,
47+
expanded: this.props.expanded,
4748
activeNamespace: this.props.activeNamespace
4849
};
4950
return (
@@ -60,7 +61,8 @@ class Sidebar extends React.Component {
6061
Sidebar.propTypes = {
6162
instance: React.PropTypes.object,
6263
databases: React.PropTypes.array,
63-
activeNamespace: React.PropTypes.string
64+
activeNamespace: React.PropTypes.string,
65+
expanded: React.PropTypes.bool
6466
};
6567

6668
module.exports = Sidebar;

src/internal-packages/sidebar/lib/stores/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const { NamespaceStore } = require('hadron-reflux-store');
66

77
const debug = require('debug')('mongodb-compass:stores:sidebar');
88

9+
const BLANK = '(?:)';
10+
911
/**
1012
* Compass Sidebar store.
1113
*/
@@ -37,9 +39,10 @@ const SidebarStore = Reflux.createStore({
3739
*/
3840
getInitialState() {
3941
return {
42+
expanded: false,
4043
instance: {},
4144
databases: [],
42-
filterRegex: /.*/,
45+
filterRegex: /(?:)/,
4346
activeNamespace: ''
4447
};
4548
},
@@ -53,12 +56,14 @@ const SidebarStore = Reflux.createStore({
5356
onInstanceChange(state) {
5457
this.setState({
5558
instance: state.instance,
59+
expanded: this.state.filterRegex.source !== BLANK,
5660
databases: this._filterDatabases(this.state.filterRegex, state.instance.databases)
5761
});
5862
},
5963

6064
filterDatabases(re) {
6165
this.setState({
66+
expanded: re.source !== BLANK,
6267
databases: this._filterDatabases(re, this.state.instance.databases),
6368
filterRegex: re
6469
});

test/compass-functional.test.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ describe('Compass Functional Test Suite #spectron', function() {
9090
});
9191
});
9292

93-
context('when entering a filter in the sidebar', function() {
94-
context('when entering a plain string', function() {
95-
it('filters the list');
96-
});
97-
98-
context('when entering a regex', function() {
99-
it('filters the list');
100-
});
101-
});
10293

10394
context('when viewing the performance view', function() {
10495
it('renders the operations graph inserts', function() {
@@ -262,6 +253,44 @@ describe('Compass Functional Test Suite #spectron', function() {
262253
});
263254
});
264255

256+
context('when entering a filter in the sidebar', function() {
257+
let dbCount;
258+
259+
before(function(done) {
260+
client.getSidebarDatabaseNames().then(function(names) {
261+
dbCount = names.length;
262+
done();
263+
});
264+
});
265+
266+
context('when entering a plain string', function() {
267+
it('filters the list', function() {
268+
return client
269+
.inputSidebarFilter('mus')
270+
.getSidebarDatabaseNames()
271+
.should.eventually.include('music');
272+
});
273+
});
274+
275+
context('when entering a regex', function() {
276+
it('filters the list', function() {
277+
return client
278+
.inputSidebarFilter('ad|al')
279+
.getSidebarDatabaseNames()
280+
.should.eventually.include('local');
281+
});
282+
});
283+
284+
context('when entering default blank regex', function() {
285+
it('restores the sidebar', function() {
286+
return client
287+
.inputSidebarFilter('(?:)')
288+
.getSidebarDatabaseNames()
289+
.should.eventually.have.length(dbCount);
290+
});
291+
});
292+
});
293+
265294
context('when deleting a database', function() {
266295
let dbCount;
267296

test/support/spectron-support.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ function addWaitCommands(client) {
207207
return this.waitForVisibleInCompass(selector('create-index-modal'));
208208
});
209209

210+
client.addCommand('waitForSidebar', function(type) {
211+
return this.waitForVisibleInCompass(selector('sidebar-' + type));
212+
});
210213
/**
211214
* Waits for the create database modal to open.
212215
*/
@@ -985,9 +988,13 @@ function addGetCommands(client) {
985988
* Get a list of database names from the sidebar.
986989
*/
987990
client.addCommand('getSidebarDatabaseNames', function() {
988-
return this.getText(selector('sidebar-database'));
991+
return this.waitForSidebar('database').getText(selector('sidebar-database'));
989992
});
990993

994+
client.addCommand('inputSidebarFilter', function(filter) {
995+
const base = selector('sidebar-filter-input');
996+
return this.setValue(base, filter);
997+
});
991998
/**
992999
* Get a list of database names from the home view.
9931000
*/

0 commit comments

Comments
 (0)