Skip to content

Commit d4d6050

Browse files
authored
Fix automatically opening directories (codesandbox#4080)
1 parent 2a88cf9 commit d4d6050

File tree

3 files changed

+39
-16
lines changed
  • packages/app/src/app
    • overmind/namespaces/editor
    • pages/Sandbox/Editor/Workspace

3 files changed

+39
-16
lines changed

packages/app/src/app/overmind/namespaces/editor/state.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ type State = {
7373
currentTab: Derive<State, ModuleTab | DiffTab | undefined>;
7474
modulesByPath: SandboxFs;
7575
isAdvancedEditor: Derive<State, boolean>;
76-
shouldDirectoryBeOpen: Derive<State, (directoryShortid: string) => boolean>;
76+
shouldDirectoryBeOpen: Derive<
77+
State,
78+
(params: { directoryId: string; module?: Module }) => boolean
79+
>;
7780
currentDevToolsPosition: DevToolsTabPosition;
7881
sessionFrozen: boolean;
7982
hasLoadedInitialModule: boolean;
@@ -203,21 +206,23 @@ export const state: State = {
203206
? currentPackageJSON.code
204207
: generateFileFromSandbox(currentSandbox);
205208
},
206-
shouldDirectoryBeOpen: ({ currentSandbox, currentModule }) => (
207-
directoryShortid: string
208-
) => {
209+
shouldDirectoryBeOpen: ({ currentSandbox, currentModule }) => ({
210+
directoryId,
211+
module = currentModule,
212+
}) => {
209213
if (!currentSandbox) {
210214
return false;
211215
}
212216

213217
const { modules, directories } = currentSandbox;
214-
const currentModuleId = currentModule.id;
218+
const currentModuleId = module.id;
215219
const currentModuleParents = getModuleParents(
216220
modules,
217221
directories,
218222
currentModuleId
219223
);
220-
const isParentOfModule = currentModuleParents.includes(directoryShortid);
224+
225+
const isParentOfModule = currentModuleParents.includes(directoryId);
221226

222227
return isParentOfModule;
223228
},

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ const DirectoryEntry: React.FunctionComponent<Props> = ({
114114
} = useOvermind();
115115

116116
const [creating, setCreating] = React.useState<ItemTypes>(null);
117-
const [open, setOpen] = React.useState(root || shouldDirectoryBeOpen(id));
117+
const [open, setOpen] = React.useState(
118+
root || shouldDirectoryBeOpen({ directoryId: id })
119+
);
118120
const [modalConfirm, setModalConfirm] = React.useState<Modal | null>(null);
119121

120122
React.useEffect(() => {
@@ -131,12 +133,19 @@ const DirectoryEntry: React.FunctionComponent<Props> = ({
131133
React.useEffect(
132134
() =>
133135
reaction(
134-
({ editor }) => editor.currentModuleShortid,
135-
() => {
136-
setOpen(isOpen => isOpen || shouldDirectoryBeOpen(id));
136+
({ editor }) => editor.currentModule,
137+
currentModule => {
138+
setOpen(
139+
isOpen =>
140+
isOpen ||
141+
shouldDirectoryBeOpen({ directoryId: id, module: currentModule })
142+
);
137143
}
138144
),
139-
[id, reaction, shouldDirectoryBeOpen]
145+
146+
// shouldDirectoryOpen causes this to unmount for some reason, which bugs out how directories open and close
147+
// eslint-disable-next-line
148+
[id, reaction]
140149
);
141150

142151
React.useEffect(() => {

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Explorer/Files/DirectoryEntry/index.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ const DirectoryEntry: React.FunctionComponent<Props> = ({
114114
} = useOvermind();
115115

116116
const [creating, setCreating] = React.useState<ItemTypes>(null);
117-
const [open, setOpen] = React.useState(root || shouldDirectoryBeOpen(id));
117+
const [open, setOpen] = React.useState(
118+
root || shouldDirectoryBeOpen({ directoryId: id })
119+
);
118120
const [modalConfirm, setModalConfirm] = React.useState<Modal | null>(null);
119121

120122
React.useEffect(() => {
@@ -131,12 +133,19 @@ const DirectoryEntry: React.FunctionComponent<Props> = ({
131133
React.useEffect(
132134
() =>
133135
reaction(
134-
({ editor }) => editor.currentModuleShortid,
135-
() => {
136-
setOpen(isOpen => isOpen || shouldDirectoryBeOpen(id));
136+
({ editor }) => editor.currentModule,
137+
currentModule => {
138+
setOpen(
139+
isOpen =>
140+
isOpen ||
141+
shouldDirectoryBeOpen({ directoryId: id, module: currentModule })
142+
);
137143
}
138144
),
139-
[id, reaction, shouldDirectoryBeOpen]
145+
146+
// shouldDirectoryOpen causes this to unmount for some reason, which bugs out how directories open and close
147+
// eslint-disable-next-line
148+
[id, reaction]
140149
);
141150

142151
React.useEffect(() => {

0 commit comments

Comments
 (0)