Skip to content

Commit c4b45e3

Browse files
authored
Fixed select file buttons (#746)
* Fixed select file buttons * Address copilot review comment
1 parent c75a32a commit c4b45e3

4 files changed

Lines changed: 50 additions & 38 deletions

File tree

gcs/electron/main.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ ipcMain.on("window:zoom-out", () => {
180180
ipcMain.on("window:open-file-in-explorer", (_event, filePath) => {
181181
shell.showItemInFolder(filePath)
182182
})
183+
ipcMain.handle("window:select-file-in-explorer", async (_event, filters) => {
184+
const { canceled, filePaths } = await dialog.showOpenDialog({
185+
properties: ["openFile"],
186+
filters: [...filters, { name: "All Files", extensions: ["*"] }],
187+
})
188+
if (!canceled && filePaths.length > 0) {
189+
const filePath = filePaths[0]
190+
try {
191+
const stats = fs.statSync(filePath)
192+
return {
193+
path: filePath,
194+
name: path.basename(filePath),
195+
size: stats.size,
196+
}
197+
} catch (err) {
198+
// File is inaccessible or deleted
199+
return null
200+
}
201+
}
202+
return null
203+
})
183204

184205
function createWindow() {
185206
win = new BrowserWindow({

gcs/electron/preload.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const ALLOWED_INVOKE_CHANNELS = [
1919
"app:open-link-stats-window",
2020
"app:close-link-stats-window",
2121
"app:update-link-stats",
22+
"window:select-file-in-explorer",
2223
]
2324

2425
const ALLOWED_SEND_CHANNELS = [

gcs/src/components/fla/SelectFlightLog.jsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
Button,
33
Divider,
4-
FileButton,
54
LoadingOverlay,
65
Progress,
76
ScrollArea,
@@ -34,6 +33,16 @@ export default function SelectFlightLog({ processLoadedFile }) {
3433
getFgcsLogs()
3534
}
3635

36+
const selectFile = async () => {
37+
const result = await window.ipcRenderer.invoke(
38+
"window:select-file-in-explorer",
39+
[{ name: "Flight Logs", extensions: ["log", "ftlog"] }],
40+
)
41+
if (result) {
42+
handleFile(result)
43+
}
44+
}
45+
3746
const handleFile = useCallback(
3847
async function (file) {
3948
if (!file) return
@@ -111,14 +120,9 @@ export default function SelectFlightLog({ processLoadedFile }) {
111120
<div className="flex flex-col items-center justify-center h-full mx-auto">
112121
<div className="flex flex-row items-center justify-center gap-8">
113122
<div className="flex flex-col gap-4">
114-
{/* File selection */}
115-
<FileButton onChange={handleFile} accept=".log,.ftlog">
116-
{(props) => (
117-
<Button {...props} loading={loadingFile}>
118-
Analyse a log
119-
</Button>
120-
)}
121-
</FileButton>
123+
<Button onClick={selectFile} loading={loadingFile}>
124+
Analyse a log
125+
</Button>
122126
<Button color="red" variant="filled" onClick={clearFgcsLogs}>
123127
Clear Logs
124128
</Button>

gcs/src/missions.jsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
ActionIcon,
1616
Button,
1717
Divider,
18-
FileButton,
1918
Modal,
2019
NumberInput,
2120
Progress,
@@ -117,10 +116,6 @@ export default function Missions() {
117116
const tabsListRef = useRef(null)
118117
const [tableSectionHeight, setTableSectionHeight] = useState(300)
119118

120-
// File import handling
121-
const [importFile, setImportFile] = useState(null)
122-
const importFileResetRef = useRef(null)
123-
124119
// Modal for mission progress
125120
const [missionProgressModalTitle, setMissionProgressModalTitle] = useState(
126121
"Mission progress update",
@@ -152,12 +147,6 @@ export default function Missions() {
152147
dispatch(emitGetTargetInfo())
153148
}, [currentPage])
154149

155-
useEffect(() => {
156-
if (importFile) {
157-
importMissionFromFile(importFile.path)
158-
}
159-
}, [importFile])
160-
161150
useEffect(() => {
162151
activeTabRef.current = activeTab
163152
}, [activeTab])
@@ -245,17 +234,19 @@ export default function Missions() {
245234
dispatch(setMissionProgressModal(true))
246235
}
247236

248-
function importMissionFromFile(filePath) {
249-
dispatch(
250-
emitImportMissionFromFile({
251-
type: activeTabRef.current,
252-
file_path: filePath,
253-
}),
237+
async function importMissionFromFile() {
238+
const result = await window.ipcRenderer.invoke(
239+
"window:select-file-in-explorer",
240+
[{ name: "Waypoint files", extensions: ["waypoints", "txt"] }],
254241
)
255-
256-
// Reset the import file after sending
257-
setImportFile(null)
258-
importFileResetRef.current?.()
242+
if (result) {
243+
dispatch(
244+
emitImportMissionFromFile({
245+
type: activeTabRef.current,
246+
file_path: result.path,
247+
}),
248+
)
249+
}
259250
}
260251

261252
async function saveMissionToFile() {
@@ -414,14 +405,9 @@ export default function Missions() {
414405
<Divider className="my-1" />
415406

416407
<div className="flex flex-col gap-4">
417-
<FileButton
418-
resetRef={importFileResetRef}
419-
onChange={setImportFile}
420-
accept=".waypoints,.txt"
421-
className="grow"
422-
>
423-
{(props) => <Button {...props}>Import from file</Button>}
424-
</FileButton>
408+
<Button className="grow" onClick={importMissionFromFile}>
409+
Import from file
410+
</Button>
425411
<Button
426412
onClick={() => {
427413
saveMissionToFile()

0 commit comments

Comments
 (0)