From 8f5c50609768edb12846fb1a239ca9846515acf0 Mon Sep 17 00:00:00 2001 From: LiBe Date: Sat, 6 Apr 2019 11:11:55 +0200 Subject: [PATCH] Bug fixed : file explorer not displaying anything When the project has not got any file in the subdirectory but only folders, the file explorer is not displaying anything. --- .../FileExplorer/ModuleList/ModuleList.tsx | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx b/packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx index 5b55903ab6e..de6e04496a1 100644 --- a/packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx +++ b/packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx @@ -21,27 +21,18 @@ export default class ModuleList extends React.PureComponent { prefixedPath, files, } = this.props; - - const filesToShow: { path: string }[] = []; - const directoriesToShow: Set = new Set(); - const pathParts = prefixedPath.split('/'); - - Object.keys(files).forEach(path => { - if (path.startsWith(prefixedPath)) { - const filePathParts = path.split('/'); - - if (filePathParts.length === pathParts.length) { - if (path.endsWith('/')) { - directoriesToShow.add(path); - } else { - filesToShow.push({ path }); - } - } else if (filePathParts.length === pathParts.length + 1) { - filePathParts.pop(); - directoriesToShow.add(filePathParts.join('/') + '/'); - } - } - }); + + const fileListWithoutPrefix = Object.keys(files) + .filter(file => file.startsWith(prefixedPath)) + .map(file => file.substring(prefixedPath.length)); + + const directoriesToShow = new Set(fileListWithoutPrefix + .filter(file => file.includes('/')) + .map(file => `${prefixedPath}${file.split('/')[0]}/`)); + + const filesToShow = fileListWithoutPrefix + .filter(file => !file.includes('/')) + .map(file => ({ path: `${prefixedPath}${file}` })); return (