-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSelectFlightLog.jsx
More file actions
150 lines (136 loc) · 4.39 KB
/
SelectFlightLog.jsx
File metadata and controls
150 lines (136 loc) · 4.39 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
144
145
146
147
148
149
150
import {
Button,
Divider,
LoadingOverlay,
Progress,
ScrollArea,
} from "@mantine/core"
import moment from "moment"
import { useCallback, useEffect, useMemo, useState } from "react"
import { useDispatch } from "react-redux"
import {
showErrorNotification,
showSuccessNotification,
} from "../../helpers/notification.js"
import { setFile } from "../../redux/slices/logAnalyserSlice.js"
import { readableBytes } from "./utils"
/**
* Initial FLA screen for selecting or uploading a flight log file.
*/
export default function SelectFlightLog({ processLoadedFile }) {
const dispatch = useDispatch()
const [recentFgcsLogs, setRecentFgcsLogs] = useState(null)
const [loadingFile, setLoadingFile] = useState(false)
const [loadingFileProgress, setLoadingFileProgress] = useState(0)
async function getFgcsLogs() {
setRecentFgcsLogs(await window.ipcRenderer.invoke("fla:get-recent-logs"))
}
async function clearFgcsLogs() {
await window.ipcRenderer.invoke("fla:clear-recent-logs")
getFgcsLogs()
}
const selectFile = async () => {
const result = await window.ipcRenderer.invoke(
"window:select-file-in-explorer",
[{ name: "Flight Logs", extensions: ["log", "ftlog"] }],
)
if (result) {
handleFile(result)
}
}
const handleFile = useCallback(
async function (file) {
if (!file) return
console.time(`Loading file: ${file.name}`)
try {
dispatch(setFile(file))
setLoadingFile(true)
setLoadingFileProgress(0)
console.log(
`Starting to load file: ${file.name} (${(file.size / 1024 / 1024).toFixed(2)} MB)`,
)
const result = await window.ipcRenderer.invoke(
"fla:open-file",
file.path,
)
if (!result.success) {
showErrorNotification(
`Error loading file: ${result.error || "File not found. Please reload."}`,
)
return
}
await processLoadedFile(result)
showSuccessNotification(`${file.name} loaded successfully`)
console.timeEnd(`Loading file: ${file.name}`)
} catch (error) {
console.error("Error loading file:", error)
showErrorNotification("Error loading file: " + error.message)
} finally {
setLoadingFile(false)
setLoadingFileProgress(0)
}
},
[dispatch, processLoadedFile],
)
useEffect(() => {
const onProgress = (_event, message) =>
setLoadingFileProgress(message.percent)
window.ipcRenderer.on("fla:log-parse-progress", onProgress)
getFgcsLogs()
return () => {
window.ipcRenderer.removeAllListeners("fla:log-parse-progress")
}
}, [])
const recentLogItems = useMemo(() => {
if (!recentFgcsLogs) return null
return recentFgcsLogs.map((log, idx) => (
<div
key={idx}
className="flex flex-col px-4 py-2 hover:cursor-pointer hover:bg-falcongrey-700 hover:rounded-sm w-80"
onClick={() => handleFile(log)}
>
<p>{log.name} </p>
<div className="flex flex-row gap-2">
<p className="text-sm text-gray-400">
{moment(
log.timestamp.toISOString(),
"YYYY-MM-DD_HH-mm-ss",
).fromNow()}
</p>
<p className="text-sm text-gray-400">{readableBytes(log.size)}</p>
</div>
</div>
))
}, [recentFgcsLogs, handleFile])
return (
<div className="flex flex-col items-center justify-center h-full mx-auto">
<div className="flex flex-row items-center justify-center gap-8">
<div className="flex flex-col gap-4">
<Button onClick={selectFile} loading={loadingFile}>
Analyse a log
</Button>
<Button color="red" variant="filled" onClick={clearFgcsLogs}>
Clear Logs
</Button>
</div>
<Divider size="sm" orientation="vertical" />
<div className="relative">
<LoadingOverlay visible={recentFgcsLogs === null || loadingFile} />
<div className="flex flex-col items-center gap-2">
<p className="font-bold">Recent FGCS telemetry logs</p>
<ScrollArea h={250} offsetScrollbars>
{recentLogItems}
</ScrollArea>
</div>
</div>
</div>
{loadingFile && (
<Progress
value={loadingFileProgress}
className="w-full my-4"
color="green"
/>
)}
</div>
)
}