|
1 | 1 | import React, { useEffect, useState } from "react"; |
2 | 2 | import { ReactComponent as FolderIcon } from "../../assets/icons/folder-icon.svg"; |
3 | 3 |
|
4 | | -import { FSObject, isFSDirectory } from "../../stores/localFS"; |
| 4 | +import useLocalFS, { |
| 5 | + FSDirectory, |
| 6 | + FSObject, |
| 7 | + isFSDirectory, |
| 8 | +} from "../../stores/localFS"; |
5 | 9 | import useLocalFSWithHistory from "../../hooks/useLocalFSWithHistory"; |
6 | 10 |
|
7 | 11 | import "./FileBrowser.scss"; |
8 | 12 | import AppSideBar from "../../components/AppSideBar"; |
| 13 | +import useCompression from "../../hooks/useCompression"; |
9 | 14 | const defaultPath = "/home/user"; |
10 | 15 |
|
11 | 16 | interface FileBrowserProps { |
@@ -53,35 +58,74 @@ function TopBar({ |
53 | 58 | } |
54 | 59 |
|
55 | 60 | interface MainContentProps { |
56 | | - dirContents: Record<string, FSObject>; |
| 61 | + currentDirectory: FSDirectory; |
57 | 62 | openFSObject: (fsObject: FSObject) => void; |
58 | 63 | } |
59 | 64 |
|
60 | | -function MainContent({ dirContents, openFSObject }: MainContentProps) { |
| 65 | +function MainContent({ currentDirectory, openFSObject }: MainContentProps) { |
61 | 66 | const [selected, setSelected] = useState<string>(""); |
| 67 | + const fs = useLocalFS(); |
| 68 | + const { compress } = useCompression(); |
| 69 | + |
| 70 | + function handleRightClick(e: React.MouseEvent) { |
| 71 | + e.stopPropagation(); |
| 72 | + e.preventDefault(); |
| 73 | + //fs.createFile("test-create-file.txt", currentDirectory, compress("Hello world!")); |
| 74 | + } |
| 75 | + |
62 | 76 | return ( |
63 | | - <div className="file-browser__main-content"> |
64 | | - {Object.values<FSObject>(dirContents).map((fsObject) => ( |
65 | | - <div |
66 | | - className={`file-browser__main-content__item ${ |
67 | | - fsObject.path === selected ? "active" : "" |
68 | | - }`} |
69 | | - onDoubleClick={() => openFSObject(fsObject)} |
70 | | - onClick={() => setSelected(fsObject.path)} |
71 | | - key={fsObject.path} |
72 | | - > |
73 | | - {isFSDirectory(fsObject) ? ( |
74 | | - <FolderIcon className="file-browser__main-content__item-icon" /> |
75 | | - ) : null} |
76 | | - <span className="file-browser__main-content__item-name"> |
77 | | - {fsObject.name} |
78 | | - </span> |
79 | | - </div> |
| 77 | + <div |
| 78 | + className="file-browser__main-content" |
| 79 | + onContextMenu={handleRightClick} |
| 80 | + > |
| 81 | + {Object.values<FSObject>(currentDirectory.contents).map((fsObject) => ( |
| 82 | + <DirectoryOrFile |
| 83 | + fsObject={fsObject} |
| 84 | + openFSObject={openFSObject} |
| 85 | + selected={selected === fsObject.path} |
| 86 | + setSelected={setSelected} |
| 87 | + /> |
80 | 88 | ))} |
81 | 89 | </div> |
82 | 90 | ); |
83 | 91 | } |
84 | 92 |
|
| 93 | +interface DirectoryOrFileProps { |
| 94 | + fsObject: FSObject; |
| 95 | + selected: boolean; |
| 96 | + openFSObject: (fsObject: FSObject) => void; |
| 97 | + setSelected: (path: string) => void; |
| 98 | +} |
| 99 | + |
| 100 | +function DirectoryOrFile({ |
| 101 | + fsObject, |
| 102 | + openFSObject, |
| 103 | + selected, |
| 104 | + setSelected, |
| 105 | +}: DirectoryOrFileProps) { |
| 106 | + function handleRightClick(event: React.MouseEvent) { |
| 107 | + event.stopPropagation(); |
| 108 | + event.preventDefault(); |
| 109 | + } |
| 110 | + |
| 111 | + return ( |
| 112 | + <div |
| 113 | + className={`file-browser__main-content__item ${selected ? "active" : ""}`} |
| 114 | + onDoubleClick={() => openFSObject(fsObject)} |
| 115 | + onClick={() => setSelected(fsObject.path)} |
| 116 | + key={fsObject.path} |
| 117 | + onContextMenu={handleRightClick} |
| 118 | + > |
| 119 | + {isFSDirectory(fsObject) ? ( |
| 120 | + <FolderIcon className="file-browser__main-content__item-icon" /> |
| 121 | + ) : null} |
| 122 | + <span className="file-browser__main-content__item-name"> |
| 123 | + {fsObject.name} |
| 124 | + </span> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
85 | 129 | function FileBrowser({ path = defaultPath }: FileBrowserProps) { |
86 | 130 | const fs = useLocalFSWithHistory(path); |
87 | 131 |
|
@@ -117,12 +161,12 @@ function FileBrowser({ path = defaultPath }: FileBrowserProps) { |
117 | 161 | items={fs.favorites.map((fav) => ({ |
118 | 162 | title: fav.name, |
119 | 163 | isActive: fav.path === fs.currentDirectory.path, |
120 | | - onClick: () => fs.navToObject(fav), |
| 164 | + onClick: () => fs.navToPath(fav.path), |
121 | 165 | }))} |
122 | 166 | /> |
123 | 167 |
|
124 | 168 | <MainContent |
125 | | - dirContents={fs.currentDirectory.contents} |
| 169 | + currentDirectory={fs.currentDirectory} |
126 | 170 | openFSObject={fs.navToObject} |
127 | 171 | /> |
128 | 172 | <div className="file-browser__bottom-bar"></div> |
|
0 commit comments