-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathApp.tsx
More file actions
143 lines (121 loc) · 4.01 KB
/
App.tsx
File metadata and controls
143 lines (121 loc) · 4.01 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { useEffect, useState } from "react";
import { invoke } from "@tauri-apps/api/tauri";
import { DirectoryContent, Volume } from "./types";
import { openDirectory } from "./ipc";
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";
import { useAppDispatch, useAppSelector } from "./state/hooks";
import useContextMenu from "./hooks/useContextMenu";
import ContextMenus from "./components/ContextMenus/ContextMenus";
import {
selectDirectoryContents,
unselectDirectoryContents,
updateDirectoryContents,
} from "./state/slices/currentDirectorySlice";
import { DIRECTORY_ENTITY_ID } from "./components/MainBody/DirectoryEntity";
function App() {
const [volumes, setVolumes] = useState<Volume[]>([]);
const directoryContents = useAppSelector(selectDirectoryContents);
const dispatch = useAppDispatch();
const [searchResults, setSearchResults] = useState<DirectoryContent[]>([]);
const {
pathHistory,
historyPlace,
setHistoryPlace,
onBackArrowClick,
onForwardArrowClick,
canGoBackward,
canGoForward,
currentVolume,
setCurrentVolume,
} = useNavigation(searchResults, setSearchResults);
async function getNewDirectoryContents() {
const contents = await openDirectory(pathHistory[historyPlace]);
dispatch(updateDirectoryContents(contents));
}
async function onVolumeClick(mountpoint: string) {
if (pathHistory[pathHistory.length - 1] != mountpoint) {
pathHistory.push(mountpoint);
}
setHistoryPlace(pathHistory.length - 1);
setCurrentVolume(mountpoint);
await getNewDirectoryContents();
}
async function onDirectoryClick(filePath: string) {
if (searchResults.length > 0) {
setSearchResults([]);
}
pathHistory.push(filePath);
setHistoryPlace(pathHistory.length - 1);
await getNewDirectoryContents();
}
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;
}
getNewDirectoryContents().catch(console.error);
}, [historyPlace]);
const [handleMainContextMenu, handleCloseContextMenu] = useContextMenu(
dispatch,
pathHistory[historyPlace]
);
return (
<div
className="h-full"
onClick={(e) => {
handleCloseContextMenu(e);
if (e.target instanceof HTMLElement) {
if (e.target.id === DIRECTORY_ENTITY_ID) return;
}
dispatch(unselectDirectoryContents());
}}
onContextMenu={handleMainContextMenu}
>
<ContextMenus />
<div className="p-4">
<FolderNavigation
onBackArrowClick={onBackArrowClick}
canGoBackward={canGoBackward()}
onForwardArrowClick={onForwardArrowClick}
canGoForward={canGoForward()}
currentVolume={currentVolume}
currentDirectoryPath={pathHistory[historyPlace]}
setSearchResults={setSearchResults}
/>
<div className="pb-5">
<div>
{pathHistory[historyPlace] === "" && searchResults.length === 0 ? (
<VolumeList volumes={volumes} onClick={onVolumeClick} />
) : (
<DirectoryContents
content={
searchResults.length === 0 ? directoryContents : searchResults
}
onDirectoryClick={onDirectoryClick}
/>
)}
</div>
</div>
</div>
</div>
);
}
export default App;