Skip to content

Commit 1947205

Browse files
committed
fixed recursive component issue
1 parent baa09e7 commit 1947205

File tree

8 files changed

+189
-47
lines changed

8 files changed

+189
-47
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Button } from "@mui/material";
2+
import { useDispatch } from "react-redux";
3+
import React, { useEffect } from "react";
4+
import { setDirectories } from "../../lib/state/reducer";
5+
import { openFolder } from "../../lib/utils/fileSystem";
6+
import { connect } from "react-redux";
7+
import TreeView from "@mui/lab/TreeView";
8+
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
9+
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
10+
import TreeItem, { useTreeItem } from "@mui/lab/TreeItem";
11+
12+
interface PanelProps {
13+
state: any;
14+
}
15+
16+
const RecursiveComponent = ({ name, items }) => {
17+
console.log(items);
18+
const hasChildren = items && items;
19+
console.log(hasChildren)
20+
21+
return (
22+
<>
23+
<TreeItem nodeId={name} label={name} className="mt-1" key={name}>
24+
{hasChildren &&
25+
items.map((item) => <RecursiveComponent key={item.name} {...item} />)}
26+
</TreeItem>
27+
</>
28+
);
29+
};
30+
31+
export const Explorer: React.FC<PanelProps> = ({ state }) => {
32+
const dispatch = useDispatch();
33+
async function onFolderSelect() {
34+
const folders = await openFolder();
35+
dispatch(setDirectories(folders));
36+
}
37+
return (
38+
<div className="mt-5 px-4 text-sm">
39+
{state && Object.keys(state).length ? (
40+
<TreeView
41+
aria-label="file system navigator"
42+
defaultCollapseIcon={<ExpandMoreIcon />}
43+
defaultExpandIcon={<ChevronRightIcon />}
44+
sx={{ flexGrow: 1, maxWidth: 400, overflowY: "auto" }}
45+
>
46+
<RecursiveComponent {...state} />
47+
</TreeView>
48+
) : (
49+
<div>
50+
<p>You have not yet added a folder to the workspace.</p>
51+
<Button className="mt-4" onClick={onFolderSelect}>
52+
Open Folder
53+
</Button>
54+
</div>
55+
)}
56+
</div>
57+
);
58+
};
59+
60+
interface State {
61+
state: Object;
62+
app: any;
63+
}
64+
function mapStateToProps(state: State) {
65+
return {
66+
state: state.app.directories,
67+
};
68+
}
69+
export default connect(mapStateToProps, null)(Explorer);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import Explorer from "./explorer";
3+
import Search from "./search";
4+
5+
interface PanelProps {
6+
showPanel: String | null;
7+
}
8+
export const Panel: React.FC<PanelProps> = ({showPanel}) => {
9+
return (
10+
<div className="p-1.5">
11+
<div className="px-4 text-sm">{showPanel}</div>
12+
{showPanel && showPanel === "Explorer" && <Explorer state={undefined} />}
13+
{showPanel && showPanel === "Search" && <Search />}
14+
</div>
15+
);
16+
}
17+
18+
19+
export default Panel;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from "react";
2+
3+
function Search() {
4+
return <div>hello</div>;
5+
}
6+
7+
export default Search;

src/frontend/components/SideBar/index.tsx

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,59 @@ import MenuBar from "../MenuBar/notebookOptionsBar";
1313

1414
export const sideNavData = [
1515
{
16-
name: "explorer",
16+
name: "Explorer",
1717
icon: <FolderRoundedIcon />,
1818
},
1919
{
20-
name: "search",
20+
name: "Search",
2121
icon: <SearchRoundedIcon />,
2222
},
2323
];
2424

25-
export default function MiniDrawer() {
25+
interface MiniDrawerProps {
26+
setShowPanel: Function;
27+
showPanel: String | null;
28+
}
29+
export const MiniDrawer: React.FC<MiniDrawerProps> = ({setShowPanel, showPanel}) => {
2630
const [openFileTree, setOpenFileTree] = useState(false);
2731

2832
const handleFileTreeOptionClick = (e: any) => {
2933
setOpenFileTree(!openFileTree);
3034
};
3135

3236
return (
37+
<div className="flex">
3338
<div className="flex flex-col justify-center items-center mt-6">
34-
<MenuBar />
35-
{sideNavData.map((data) => {
36-
return (
37-
<div
38-
style={{
39-
marginTop: "25px",
40-
cursor: "pointer",
41-
}}
42-
key={data.name}
43-
>
44-
<Button
39+
<MenuBar />
40+
{sideNavData.map((data) => {
41+
return (
42+
<div
4543
style={{
46-
padding: "5px",
44+
marginTop: "25px",
45+
cursor: "pointer",
4746
}}
47+
key={data.name}
4848
>
49-
{data.icon}
50-
</Button>
51-
</div>
52-
);
53-
})}
49+
<Button
50+
style={{
51+
padding: "5px",
52+
}}
53+
onClick={() => {
54+
if (showPanel && showPanel === data.name) {
55+
setShowPanel(null);
56+
} else {
57+
setShowPanel(data.name);
58+
}
59+
}}
60+
>
61+
{data.icon}
62+
</Button>
63+
</div>
64+
);
65+
})}
66+
</div>
5467
</div>
55-
// <Box sx={{ display: 'flex', marginLeft: '-10px', width: "50px" }}>
56-
// <div className=''>
57-
// <List>
58-
// <ListItem>
59-
// <FolderIcon
60-
// id={'file-tree'}
61-
// onClick={handleFileTreeOptionClick}
62-
// />
63-
// </ListItem>
64-
// </List>
65-
// </div>
66-
67-
// <div className='mt-4'>
68-
// {openFileTree && <CustomizedTreeView />}
69-
// </div>
70-
// </Box>
7168
);
7269
}
70+
71+
export default MiniDrawer;

src/frontend/lib/utils/fileSystem.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@ export async function openFile() {
33
console.log(fileHandle.kind);
44
}
55

6+
async function listAllFilesAndDirs(dirHandle) {
7+
const files = [];
8+
for await (let [name, handle] of dirHandle) {
9+
const {kind} = handle;
10+
if (handle.kind === 'directory') {
11+
files.push({ name, handle, kind, items: await listAllFilesAndDirs(handle) });
12+
} else {
13+
files.push({name, handle, kind});
14+
}
15+
}
16+
return files;
17+
}
18+
619
export async function openFolder() {
7-
const directoryHandle = await window.showDirectoryPicker();
8-
let dirs = [];
9-
for await (const [key, value] of directoryHandle.entries()) {
10-
dirs.push({ key, value });
11-
}
12-
return dirs;
20+
try {
21+
const directoryHandle = await window.showDirectoryPicker();
22+
const files = await listAllFilesAndDirs(directoryHandle);
23+
return {
24+
name: directoryHandle.name,
25+
items: files,
26+
};
27+
} catch (e) {
28+
console.log(e);
29+
}
1330
}
31+

src/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"react": "17.0.2",
2828
"react-ace": "^9.5.0",
2929
"react-dom": "17.0.2",
30+
"react-folder-tree": "^5.0.3",
3031
"react-redux": "^7.2.6",
3132
"react-spring": "^9.4.2",
3233
"tailwindcss": "^2.2.19",

src/frontend/pages/index.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import type { NextPage } from 'next';
2+
import { useState } from 'react';
23
import Head from 'next/head';
34
import MenuBar from '../components/MenuBar/notebookOptionsBar';
45
import NavBar from '../components/NavBar';
56
import SideBar from '../components/SideBar';
7+
import Panel from "../components/Panel";
68
import NoteBookCell from '../components/Cell';
79
import { useSelector } from "react-redux";
810
import { AppState, NbCell } from '../lib/typings/types';
911

1012
const Home: NextPage = () => {
13+
const [showPanel, setShowPanel] = useState<string | null>(null)
1114
const { cellIds, cells } = useSelector((state: { app: AppState }) => state.app)
1215
return (
1316
<div>
@@ -25,18 +28,25 @@ const Home: NextPage = () => {
2528
<MenuBar />
2629
</section> */}
2730
<section
28-
className="flex overflow-hidden"
31+
className="overflow-hidden h-full"
2932
style={{
3033
marginTop: "69px",
34+
display: "flex",
35+
height: "90vh",
3136
}}
3237
>
33-
<div className="fixed flex-none h-full border-r-2">
34-
<SideBar />
38+
<div className="sticky flex-none h-full border-r-2">
39+
<SideBar setShowPanel={setShowPanel} showPanel={showPanel} />
3540
</div>
41+
{showPanel && (
42+
<div className="sticky flex-none h-full border-r-2 w-72">
43+
<Panel showPanel={showPanel} />
44+
</div>
45+
)}
3646
<div
37-
className="flex-1 col-span-12 min-h-1/2 px-3.5"
47+
className="col-span-12 min-h-1/2 px-3.5 overflow-y-scroll"
3848
style={{
39-
marginTop: "15px",
49+
width: "100%",
4050
}}
4151
>
4252
{cellIds.map((cellId: string, i: number) => {

src/frontend/yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,6 +3380,20 @@ react-dom@17.0.2:
33803380
object-assign "^4.1.1"
33813381
scheduler "^0.20.2"
33823382

3383+
react-folder-tree@^5.0.3:
3384+
version "5.0.3"
3385+
resolved "https://registry.yarnpkg.com/react-folder-tree/-/react-folder-tree-5.0.3.tgz#586c28f26b1a5340e5a36c22d71f4067f5026f48"
3386+
integrity sha512-rCrojPpvJJZPzU9SrAEpcc/I2XddZ1eQ7wOnFPWHesZ9KQWehptm5QEbCw+eWRapDK+t8Nb/TVRPfE+oX0jVSA==
3387+
dependencies:
3388+
prop-types "^15.7.2"
3389+
react-icons "^4.1.0"
3390+
use-tree-state "^1.0.0"
3391+
3392+
react-icons@^4.1.0:
3393+
version "4.3.1"
3394+
resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-4.3.1.tgz#2fa92aebbbc71f43d2db2ed1aed07361124e91ca"
3395+
integrity sha512-cB10MXLTs3gVuXimblAdI71jrJx8njrJZmNMEMC+sQu5B/BIOmlsAjskdqpn81y8UBVEGuHODd7/ci5DvoSzTQ==
3396+
33833397
react-is@17.0.2, react-is@^17.0.2:
33843398
version "17.0.2"
33853399
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
@@ -4105,6 +4119,11 @@ use-subscription@1.5.1:
41054119
dependencies:
41064120
object-assign "^4.1.1"
41074121

4122+
use-tree-state@^1.0.0:
4123+
version "1.1.1"
4124+
resolved "https://registry.yarnpkg.com/use-tree-state/-/use-tree-state-1.1.1.tgz#229c0eb1019c382c446506b474f676ecfedd7b11"
4125+
integrity sha512-oNLnmUaAef97UGvw83L0tfTYj7ZBmPdf3tsaehr8FumSdq9Th/cc97jVTnSoOCoq9wYmhDuSffJxHv+wbgUlrA==
4126+
41084127
util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1:
41094128
version "1.0.2"
41104129
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"

0 commit comments

Comments
 (0)