Skip to content

Commit 1109059

Browse files
committed
Fix automatically opening dirs
1 parent 5020500 commit 1109059

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/index.js

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { inject, observer } from 'mobx-react';
33
import { DropTarget } from 'react-dnd';
4+
import { reaction } from 'mobx';
45
import Modal from 'app/components/Modal';
56
import Alert from 'app/components/Alert';
67

@@ -15,19 +16,10 @@ class DirectoryEntry extends React.Component {
1516
super(props);
1617

1718
const { id, store } = this.props;
18-
const { modules, directories } = store.editor.currentSandbox;
19-
const currentModuleId = store.editor.currentModule.id;
20-
const currentModuleParents = getModuleParents(
21-
modules,
22-
directories,
23-
currentModuleId
24-
);
25-
26-
const isParentOfModule = currentModuleParents.includes(id);
2719

2820
this.state = {
2921
creating: '',
30-
open: props.root || isParentOfModule,
22+
open: props.root || store.editor.shouldDirectoryBeOpen(id),
3123
showDeleteDirectoryModal: false,
3224
showDeleteModuleModal: false,
3325
moduleToDeleteTitle: null,
@@ -39,27 +31,22 @@ class DirectoryEntry extends React.Component {
3931
if (this.props.innerRef) {
4032
this.props.innerRef(this);
4133
}
42-
}
4334

44-
componentWillReceiveProps(nextProps, nextState) {
45-
if (
46-
!nextState.open &&
47-
this.props.store.editor.currentModule !==
48-
nextProps.store.editor.currentModule
49-
) {
50-
const { id, store } = nextProps;
51-
const { modules, directories } = store.editor.currentSandbox;
52-
const currentModuleId = store.editor.currentModule.id;
53-
const currentModuleParents = getModuleParents(
54-
modules,
55-
directories,
56-
currentModuleId
57-
);
58-
59-
const isParentOfModule = currentModuleParents.includes(id);
60-
if (isParentOfModule) {
61-
this.setState({ open: isParentOfModule });
35+
this.openListener = reaction(
36+
() => this.props.store.editor.currentModuleShortid,
37+
() => {
38+
if (!this.state.open) {
39+
const { id, store } = this.props;
40+
41+
this.setState({ open: store.editor.shouldDirectoryBeOpen(id) });
42+
}
6243
}
44+
);
45+
}
46+
47+
componentWillUnmount() {
48+
if (this.openListener) {
49+
this.openListener();
6350
}
6451
}
6552

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
export function isModuleSynced(moduleShortid) {
22
return this.changedModuleShortids.indexOf(moduleShortid) === -1;
33
}
4+
5+
function getModuleParents(modules, directories, id) {
6+
const module = modules.find(moduleEntry => moduleEntry.id === id);
7+
8+
if (!module) return [];
9+
10+
let directory = directories.find(
11+
directoryEntry => directoryEntry.shortid === module.directoryShortid
12+
);
13+
let directoryIds = [];
14+
while (directory != null) {
15+
directoryIds = [...directoryIds, directory.id];
16+
directory = directories.find(
17+
directoryEntry => directoryEntry.shortid === directory.directoryShortid // eslint-disable-line
18+
);
19+
}
20+
21+
return directoryIds;
22+
}
23+
24+
export function shouldDirectoryBeOpen(directoryShortid) {
25+
const { modules, directories } = this.currentSandbox;
26+
const currentModuleId = this.currentModule.id;
27+
const currentModuleParents = getModuleParents(
28+
modules,
29+
directories,
30+
currentModuleId
31+
);
32+
33+
const isParentOfModule = currentModuleParents.includes(directoryShortid);
34+
return isParentOfModule;
35+
}

packages/app/src/app/store/modules/editor/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
currentPackageJSONCode,
1111
parsedConfigurations,
1212
} from './getters';
13-
import { isModuleSynced } from './computed';
13+
import { isModuleSynced, shouldDirectoryBeOpen } from './computed';
1414
import { loadSandbox } from '../../sequences';
1515

1616
export default Module({
@@ -57,6 +57,7 @@ export default Module({
5757
},
5858
computed: {
5959
isModuleSynced,
60+
shouldDirectoryBeOpen,
6061
},
6162
signals: {
6263
addNpmDependency: sequences.addNpmDependency,

0 commit comments

Comments
 (0)