forked from conaticus/FileExplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
117 lines (97 loc) · 3.18 KB
/
App.tsx
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { useEffect, useState } from "react";
import { invoke } from "@tauri-apps/api/tauri";
import { DirectoryContent, Volume } from "./types";
import { openDirectory } from "./ipc/fileExplorer";
import VolumeList from "./components/MainBody/Volumes/VolumeList";
import FolderNavigation from "./components/TopBar/FolderNavigation";
import { DirectoryContents } from "./components/MainBody/DirectoryContents";
import useNavigation from "./hooks/useNavigation";
import SearchBar from "./components/TopBar/SearchBar";
function App() {
const [volumes, setVolumes] = useState<Volume[]>([]);
const [directoryContents, setDirectoryContents] = useState<
DirectoryContent[]
>([]);
const [searchResults, setSearchResults] = useState<DirectoryContent[]>([]);
const {
pathHistory,
historyPlace,
setHistoryPlace,
onBackArrowClick,
onForwardArrowClick,
canGoBackward,
canGoForward,
currentVolume,
setCurrentVolume,
} = useNavigation(searchResults, setSearchResults);
async function updateDirectoryContents() {
const contents = await openDirectory(pathHistory[historyPlace]);
setDirectoryContents(contents);
}
async function onVolumeClick(mountpoint: string) {
if (pathHistory[pathHistory.length - 1] != mountpoint) {
pathHistory.push(mountpoint);
}
setHistoryPlace(pathHistory.length - 1);
setCurrentVolume(mountpoint);
const directoryContents = await openDirectory(pathHistory[historyPlace]);
setDirectoryContents(directoryContents);
}
async function onDirectoryClick(filePath: string) {
if (searchResults.length > 0) {
setSearchResults([]);
}
pathHistory.push(filePath);
setHistoryPlace(pathHistory.length - 1);
await updateDirectoryContents();
}
async function getVolumes() {
if (volumes.length > 0) {
return;
}
const newVolumes = await invoke<Volume[]>("get_volumes");
setVolumes(newVolumes);
}
let render = 0;
useEffect(() => {
if (render === 0) {
getVolumes().catch(console.error);
}
render += 1; // I don't know why but the use effect runs twice causing the "get_volumes" to be called twice.
}, [])
useEffect(() => {
if (pathHistory[historyPlace] == "") {
setCurrentVolume("");
return;
}
updateDirectoryContents().catch(console.error);
}, [historyPlace]);
return (
<div className="p-4">
<div className="flex justify-between pb-5">
<FolderNavigation
onBackArrowClick={onBackArrowClick}
canGoBackward={canGoBackward()}
onForwardArrowClick={onForwardArrowClick}
canGoForward={canGoForward()}
/>
<SearchBar
currentVolume={currentVolume}
currentDirectoryPath={pathHistory[historyPlace]}
setSearchResults={setSearchResults}
/>
</div>
{pathHistory[historyPlace] === "" && searchResults.length === 0 ? (
<VolumeList volumes={volumes} onClick={onVolumeClick} />
) : (
<DirectoryContents
content={
searchResults.length === 0 ? directoryContents : searchResults
}
onDirectoryClick={onDirectoryClick}
/>
)}
</div>
);
}
export default App;