Skip to content

Commit 4907907

Browse files
libetlCompuIves
authored andcommitted
Bug fixed : file explorer not displaying anything (codesandbox#1735)
When the project has not got any file in the subdirectory but only folders, the file explorer is not displaying anything.
1 parent 33f19af commit 4907907

File tree

1 file changed

+12
-21
lines changed
  • packages/react-sandpack/src/components/FileExplorer/ModuleList

1 file changed

+12
-21
lines changed

packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,18 @@ export default class ModuleList extends React.PureComponent<Props> {
2121
prefixedPath,
2222
files,
2323
} = this.props;
24-
25-
const filesToShow: { path: string }[] = [];
26-
const directoriesToShow: Set<string> = new Set();
27-
const pathParts = prefixedPath.split('/');
28-
29-
Object.keys(files).forEach(path => {
30-
if (path.startsWith(prefixedPath)) {
31-
const filePathParts = path.split('/');
32-
33-
if (filePathParts.length === pathParts.length) {
34-
if (path.endsWith('/')) {
35-
directoriesToShow.add(path);
36-
} else {
37-
filesToShow.push({ path });
38-
}
39-
} else if (filePathParts.length === pathParts.length + 1) {
40-
filePathParts.pop();
41-
directoriesToShow.add(filePathParts.join('/') + '/');
42-
}
43-
}
44-
});
24+
25+
const fileListWithoutPrefix = Object.keys(files)
26+
.filter(file => file.startsWith(prefixedPath))
27+
.map(file => file.substring(prefixedPath.length));
28+
29+
const directoriesToShow = new Set(fileListWithoutPrefix
30+
.filter(file => file.includes('/'))
31+
.map(file => `${prefixedPath}${file.split('/')[0]}/`));
32+
33+
const filesToShow = fileListWithoutPrefix
34+
.filter(file => !file.includes('/'))
35+
.map(file => ({ path: `${prefixedPath}${file}` }));
4536

4637
return (
4738
<div style={{ marginLeft: `${0.5 * depth}rem` }}>

0 commit comments

Comments
 (0)