forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleList.tsx
More file actions
63 lines (55 loc) · 1.57 KB
/
ModuleList.tsx
File metadata and controls
63 lines (55 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as React from 'react';
import { IFiles, IFile } from '../../../types';
import File from '../File';
import Directory from '../Directory';
export interface Props {
prefixedPath: string;
files: IFiles;
selectFile: (path: string) => void;
openedPath: string;
depth?: number;
}
export default class ModuleList extends React.PureComponent<Props> {
render(): JSX.Element {
const {
depth = 0,
openedPath,
selectFile,
prefixedPath,
files,
} = this.props;
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 (
<div style={{ marginLeft: `${0.5 * depth}rem` }}>
{Array.from(directoriesToShow).map(dir => (
<Directory
key={dir}
prefixedPath={dir}
files={files}
selectFile={selectFile}
openedPath={openedPath}
depth={depth + 1}
/>
))}
{filesToShow.map(file => (
<File
key={file.path}
selectFile={this.props.selectFile}
path={file.path}
active={openedPath === file.path}
/>
))}
</div>
);
}
}