forked from conaticus/FileExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
93 lines (75 loc) · 3.08 KB
/
Copy pathApp.tsx
File metadata and controls
93 lines (75 loc) · 3.08 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {useEffect, useState} from "react";
import {invoke} from "@tauri-apps/api/tauri";
import {DirectoryContent, Disk} from "./types";
import {openDirectory} from "./ipc/fileExplorer";
import DiskList from "./components/MainBody/Disks/DiskList";
import FolderNavigation from "./components/TopBar/FolderNavigation";
import {DirectoryContents} from "./components/MainBody/DirectoryContents";
import useNavigation from "./hooks/useNavigation";
import SearchBar from "./components/TopBar/SearchBar";
import SearchFilter from "./components/TopBar/SearchFilter";
function App() {
const [disks, setDisks] = useState<Disk[]>([]);
const [directoryContents, setDirectoryContents] = useState<DirectoryContent[]>([]);
const [searchResults, setSearchResults] = useState<DirectoryContent[]>([])
const {
pathHistory,
setPathHistory,
historyPlace,
setHistoryPlace,
onBackArrowClick,
onForwardArrowClick,
canGoBackward,
canGoForward,
} = useNavigation();
async function updateDirectoryContents() {
const contents = await openDirectory(pathHistory[historyPlace]);
setDirectoryContents(contents);
}
async function onDiskClick(letter: string) {
const path = letter + ":/";
if (pathHistory[pathHistory.length - 1] != path) {
pathHistory.push(path);
}
setHistoryPlace(pathHistory.length - 1);
const directoryContents = await openDirectory(pathHistory[historyPlace]);
setDirectoryContents(directoryContents);
}
async function onDirectoryClick(name: string) {
const currentPath = pathHistory[pathHistory.length - 1];
const newPath = currentPath + name + "/";
pathHistory.push(newPath);
setHistoryPlace(pathHistory.length - 1);
updateDirectoryContents();
}
async function getDisks() {
const disks = await invoke<Disk[]>("get_disks");
setDisks(disks);
}
async function updateCurrentDirectory() {
if (pathHistory[historyPlace] == "") {
return getDisks();
}
await updateDirectoryContents();
}
useEffect(() => {
if (pathHistory[historyPlace] == "") {
getDisks().catch(console.error);
return;
}
updateCurrentDirectory();
}, [historyPlace])
return (
<div className="p-4">
<div className="flex justify-between pb-5">
<FolderNavigation onBackArrowClick={onBackArrowClick} canGoBackward={canGoBackward()} onForwardArrowClick={onForwardArrowClick}
canGoForward={canGoForward()}/>
<SearchBar currentDirectoryPath={pathHistory[historyPlace]} setSearchResults={setSearchResults} />
</div>
{pathHistory[historyPlace] === "" && searchResults.length === 0
? <DiskList disks={disks} onClick={onDiskClick}/>
: <DirectoryContents content={searchResults.length === 0 ? directoryContents : searchResults} onDirectoryClick={onDirectoryClick}/>}
</div>
);
}
export default App;