forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
897 lines (816 loc) · 31.2 KB
/
App.js
File metadata and controls
897 lines (816 loc) · 31.2 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
// src/App.js
import React from "react";
import { createRoot } from "react-dom/client";
import Map from "./Map";
import Dataframe from "./Dataframe";
import TimeSlider from "./TimeSlider";
import LogTable from "./LogTable";
import ToggleBar from "./ToggleBar";
import TripLogs from "./TripLogs";
import CloudLogging from "./CloudLogging";
import {
uploadFile,
getUploadedData,
deleteUploadedData,
uploadCloudLogs,
saveDatasetAsJson,
saveToIndexedDB,
} from "./localStorage";
import _ from "lodash";
import { getQueryStringValue, setQueryStringValue } from "./queryString";
import "./global.css";
import { log } from "./Utils";
import { toast, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { ALL_TOGGLES, getVisibleToggles } from "./MapToggles";
const MARKER_COLORS = [
"#EA4335", // Red
"#E91E63", // Pink
"#34A853", // Green
"#FBBC05", // Yellow
"#9C27B0", // Purple
"#FF6D00", // Orange
"#00BFA5", // Teal
"#26A69A", // Darker Teal
];
class App extends React.Component {
constructor(props) {
super(props);
this.centerOnLocation = null;
this.renderMarkerOnMap = null;
this.nextColorIndex = 0;
const nowDate = new Date();
let urlMinTime = getQueryStringValue("minTime");
let urlMaxTime = getQueryStringValue("maxTime");
this.initialMinTime = urlMinTime ? parseInt(urlMinTime) : 0;
this.initialMaxTime = urlMaxTime ? parseInt(urlMaxTime) : nowDate.setFullYear(nowDate.getFullYear() + 1);
this.focusOnRowFunction = null;
this.state = {
timeRange: { minTime: this.initialMinTime, maxTime: this.initialMaxTime },
isPlaying: false,
playSpeed: 1000,
featuredObject: { msg: "Click a table row to select object" },
extraColumns: [],
toggleOptions: Object.fromEntries(ALL_TOGGLES.map((t) => [t.id, false])),
uploadedDatasets: [null, null, null, null, null],
activeDatasetIndex: null,
activeMenuIndex: null,
initialMapBounds: null,
selectedRowIndexPerDataset: [-1, -1, -1, -1, -1],
currentLogData: this.props.logData,
dynamicMarkerLocations: {},
visibleToggles: getVisibleToggles(this.props.logData.solutionType),
};
this.onSliderChangeDebounced = _.debounce((timeRange) => this.onSliderChange(timeRange), 25);
this.setFeaturedObject = this.setFeaturedObject.bind(this);
this.setTimeRange = this.setTimeRange.bind(this);
}
updateMapAndAssociatedData = () => {
this.setTimeRange(this.state.timeRange.minTime, this.state.timeRange.maxTime);
};
componentDidMount() {
log(`Initial device pixel ratio: ${window.devicePixelRatio}`);
this.initializeData().then(() => {
this.updateMapAndAssociatedData();
});
document.addEventListener("keydown", this.handleKeyPress);
}
initializeData = async () => {
await this.checkUploadedDatasets();
if (!this.state.uploadedDatasets[0]) {
await this.checkForDemoFile();
}
};
updateToggleState(newValue, toggleName, jsonPaths) {
this.setState((prevState) => {
const newToggleOptions = {
...prevState.toggleOptions,
[toggleName]: newValue,
};
const newExtraColumns = newValue
? _.union(prevState.extraColumns, jsonPaths)
: _.difference(prevState.extraColumns, jsonPaths);
return {
toggleOptions: newToggleOptions,
extraColumns: newExtraColumns,
};
});
}
onSliderChange(timeRange) {
this.setTimeRange(timeRange.minTime, timeRange.maxTime);
}
onSelectionChange(selectedRow, rowIndex) {
if (this.state.activeDatasetIndex !== null && rowIndex !== undefined) {
this.setState((prevState) => {
const newSelectedIndexes = [...prevState.selectedRowIndexPerDataset];
newSelectedIndexes[prevState.activeDatasetIndex] = rowIndex;
return {
selectedRowIndexPerDataset: newSelectedIndexes,
featuredObject: selectedRow,
};
});
} else {
log("Unable to save index:", rowIndex, "for dataset:", this.state.activeDatasetIndex);
this.setFeaturedObject(selectedRow);
}
}
setFeaturedObject(featuredObject) {
this.setState({ featuredObject: featuredObject });
}
setFocusOnRowFunction = (func) => {
this.focusOnRowFunction = func;
};
focusOnSelectedRow = () => {
if (this.focusOnRowFunction && this.state.featuredObject) {
this.focusOnRowFunction(this.state.featuredObject);
}
};
setTimeRange(minTime, maxTime, callback) {
setQueryStringValue("minTime", minTime);
setQueryStringValue("maxTime", maxTime);
this.setState(
{
timeRange: { minTime: minTime, maxTime: maxTime },
},
callback
);
}
/*
* Callback to handle clicks on properties in the json viewer.
* Adds/removes row from the log viewer based on which property
* in the json object was clicked on
*/
onDataframePropClick(jsonPath) {
this.setState((prevState) => {
let newColumns;
if (_.find(prevState.extraColumns, (x) => x === jsonPath)) {
newColumns = _.without(prevState.extraColumns, jsonPath);
} else {
newColumns = [...prevState.extraColumns, jsonPath];
}
return { extraColumns: newColumns };
});
}
selectFirstRow = () => {
return new Promise((resolve) => {
this.setState((prevState) => {
const minDate = new Date(prevState.timeRange.minTime);
const maxDate = new Date(prevState.timeRange.maxTime);
const logs = this.state.currentLogData.tripLogs.getLogs_(minDate, maxDate).value();
if (logs.length > 0) {
const firstRow = logs[0];
setTimeout(() => this.focusOnSelectedRow(), 0);
resolve(firstRow);
return { featuredObject: firstRow };
} else {
console.log("selectFirstRow: No logs found in the current time range");
resolve(null);
return null;
}
});
});
};
selectLastRow = () => {
const minDate = new Date(this.state.timeRange.minTime);
const maxDate = new Date(this.state.timeRange.maxTime);
const logsWrapper = this.state.currentLogData.tripLogs.getLogs_(minDate, maxDate);
const logs = logsWrapper.value();
if (logs.length > 0) {
const lastRow = logs[logs.length - 1];
this.setFeaturedObject(lastRow);
this.focusOnRowFunction(lastRow);
} else {
console.log("selectLastRow: No logs found in the current time range");
}
};
handleRowChange = async (direction) => {
const { featuredObject } = this.state;
const minDate = new Date(this.state.timeRange.minTime);
const maxDate = new Date(this.state.timeRange.maxTime);
const logs = this.state.currentLogData.tripLogs.getLogs_(minDate, maxDate).value();
let newFeaturedObject = featuredObject;
const currentIndex = logs.findIndex((log) => log.timestamp === featuredObject.timestamp);
if (direction === "next" && currentIndex < logs.length - 1) {
newFeaturedObject = logs[currentIndex + 1];
} else if (direction === "previous" && currentIndex > 0) {
newFeaturedObject = logs[currentIndex - 1];
}
if (newFeaturedObject !== featuredObject) {
this.setState({ featuredObject: newFeaturedObject }, () => {
this.focusOnSelectedRow();
});
}
};
handleNextEvent = () => {
this.handleRowChange("next");
};
handlePreviousEvent = () => {
this.handleRowChange("previous");
};
handlePlayStop = () => {
this.setState((prevState) => {
if (!prevState.isPlaying) {
this.timerID = setInterval(() => {
this.handleNextEvent();
}, prevState.playSpeed);
} else {
clearInterval(this.timerID);
}
return { isPlaying: !prevState.isPlaying };
});
};
handleSpeedChange = (event) => {
const newSpeed = parseInt(event.target.value);
this.setState({ playSpeed: newSpeed });
};
handleKeyPress = (event) => {
if (["ArrowLeft", ",", "<"].includes(event.key)) {
this.handlePreviousEvent();
} else if (["ArrowRight", ".", ">"].includes(event.key)) {
this.handleNextEvent();
}
};
componentWillUnmount() {
clearInterval(this.timerID);
document.removeEventListener("keydown", this.handleKeyPress);
if (this._outsideClickHandler) {
document.removeEventListener("click", this._outsideClickHandler);
this._outsideClickHandler = null;
}
}
checkForDemoFile = async () => {
try {
const response = await fetch("./data.json");
const contentType = response.headers.get("content-type");
if (!response.ok || !contentType || !contentType.includes("application/json")) {
return;
}
console.log("data.json demo file found on the server root, saving it to Dataset 1");
const blob = await response.blob();
const file = new File([blob], "data.json", { type: "application/json" });
const event = { target: { files: [file] } };
await this.handleFileUpload(event, 0);
} catch (error) {
return;
}
};
handleFileUpload = async (event, index) => {
const file = event.target.files[0];
log(`Attempting to upload file for button ${index}:`, file ? file.name : "No file selected");
if (file) {
try {
log(`Uploading file ${file.name} for button ${index}`);
await uploadFile(file, index);
log(`File ${file.name} uploaded successfully for button ${index}`);
this.setState(
(prevState) => {
const newUploadedDatasets = [...prevState.uploadedDatasets];
newUploadedDatasets[index] = "Uploaded";
log(`Updated dataset button state for index ${index}:`, newUploadedDatasets);
return { uploadedDatasets: newUploadedDatasets };
},
() => {
console.log(`handleFileUpload: setState callback executed for index ${index}, now switching dataset.`);
this.switchDataset(index);
}
);
} catch (error) {
console.error("Error uploading file:", error);
}
}
};
checkUploadedDatasets = async () => {
const newUploadedDatasets = await Promise.all(
[0, 1, 2, 3, 4].map(async (index) => {
const data = await getUploadedData(index);
log(`Dataset ${index}:`, data);
if (data && data.rawLogs && Array.isArray(data.rawLogs) && data.rawLogs.length > 0) {
return { status: "Uploaded", index };
}
return { status: null, index };
})
);
this.setState(
{
uploadedDatasets: newUploadedDatasets.map((d) => d.status),
},
() => {
if (this.state.activeDatasetIndex === null) {
const firstAvailableIndex = newUploadedDatasets.find((dataset) => dataset.status === "Uploaded")?.index;
if (firstAvailableIndex !== undefined) {
this.switchDataset(firstAvailableIndex);
}
}
}
);
};
handleLongPress = async (index) => {
log(`Long press detected for button ${index}`);
try {
log(`Attempting to delete data for button ${index}`);
await deleteUploadedData(index);
log(`Data deleted successfully for button ${index}`);
this.setState((prevState) => {
const newUploadedDatasets = [...prevState.uploadedDatasets];
newUploadedDatasets[index] = null;
log(`Updated state after deletion for button ${index}:`, newUploadedDatasets);
return {
uploadedDatasets: newUploadedDatasets,
activeDatasetIndex: prevState.activeDatasetIndex === index ? null : prevState.activeDatasetIndex,
};
});
} catch (error) {
console.error("Error deleting uploaded data:", error);
alert("Error deleting uploaded data. Please try again.");
}
};
renderUploadButton = (index) => {
const isUploaded = this.state.uploadedDatasets[index] === "Uploaded";
const isActive = this.state.activeDatasetIndex === index;
const isMenuOpen = this.state.activeMenuIndex === index;
const toggleMenu = (e) => {
e.stopPropagation();
log(`Toggle menu for dataset ${index}`);
this.setState((prevState) => ({
activeMenuIndex: prevState.activeMenuIndex === index ? null : index,
}));
};
const handleDeleteClick = async (e) => {
e.stopPropagation();
log(`Delete initiated for dataset ${index}`);
this.setState({ activeMenuIndex: null }); // Close menu
try {
await deleteUploadedData(index);
log(`Data deleted successfully for dataset ${index}`);
this.setState((prevState) => {
const newUploadedDatasets = [...prevState.uploadedDatasets];
newUploadedDatasets[index] = null;
return {
uploadedDatasets: newUploadedDatasets,
activeDatasetIndex: prevState.activeDatasetIndex === index ? null : prevState.activeDatasetIndex,
};
});
} catch (error) {
console.error("Error deleting local storage data:", error);
alert("Error deleting local storage data. Please try again.");
}
};
const handleSaveClick = async (e) => {
e.stopPropagation();
log(`Export initiated for dataset ${index}`);
// Close menu
this.setState({ activeMenuIndex: null });
try {
await saveDatasetAsJson(index);
toast.success(`Dataset ${index + 1} exported successfully`);
log(`Dataset ${index} exported successfully`);
} catch (error) {
console.error("Error exporting dataset:", error);
toast.error(`Error exporting dataset: ${error.message}`);
}
};
const handlePruneClick = async (e) => {
e.stopPropagation();
log(`Prune initiated for dataset ${index}`);
// Close menu
this.setState({ activeMenuIndex: null });
try {
const { minTime, maxTime } = this.state.timeRange;
const data = await getUploadedData(index);
if (!data || !data.rawLogs || !Array.isArray(data.rawLogs)) {
throw new Error("No valid data to prune");
}
// Calculate how many logs will be removed
const originalLength = data.rawLogs.length;
const minDate = new Date(minTime);
const maxDate = new Date(maxTime);
const remainingLogs = data.rawLogs.filter((log) => {
const logTime = new Date(log.timestamp || log.insertTime);
return logTime >= minDate && logTime <= maxDate;
});
const removeCount = originalLength - remainingLogs.length;
if (
!confirm(
`This will remove ${removeCount} logs outside the selected time range.\nAre you sure you want to continue?`
)
) {
log("Prune operation cancelled by user");
return;
}
log(`Pruning dataset ${index}: removing ${removeCount} logs outside time range`);
data.rawLogs = remainingLogs;
// Save the pruned dataset back to storage
await saveToIndexedDB(data, index);
// Update the current dataset if this is the active one
if (this.state.activeDatasetIndex === index) {
log(`handlePruneClick: Pruning active dataset ${index}, updating state.`);
const tripLogs = new TripLogs(data.rawLogs, data.solutionType);
this.setState(
(prevState) => ({
currentLogData: {
...prevState.currentLogData,
tripLogs: tripLogs,
solutionType: data.solutionType,
},
}),
() => {
log("handlePruneClick: setState callback executed, selecting first row.");
// Select first row after pruning
this.selectFirstRow();
}
);
}
toast.success(`Dataset pruned: removed ${removeCount} logs outside the selected time range.`);
} catch (error) {
console.error("Error pruning dataset:", error);
toast.error(`Error pruning dataset: ${error.message}`);
}
};
const handleClick = async () => {
if (isUploaded) {
this.switchDataset(index);
} else {
log("Opening Cloud Logging dialog directly for index " + index);
const result = await this.showCloudLoggingDialog();
if (result) {
try {
if (result.file) {
log("Processing file upload from dialog");
const uploadEvent = { target: { files: [result.file] } };
await this.handleFileUpload(uploadEvent, index);
} else if (result.logs) {
log("Processing cloud logs");
await uploadCloudLogs(result.logs, index);
this.setState((prevState) => ({
uploadedDatasets: prevState.uploadedDatasets.map((dataset, i) => (i === index ? "Uploaded" : dataset)),
activeDatasetIndex: index,
}));
this.switchDataset(index);
}
} catch (error) {
console.error("Failed to process data:", error);
alert("Failed to process data: " + error.message);
}
}
}
};
// Close menu when clicking outside
const handleOutsideClick = () => {
if (isMenuOpen) {
this.setState({ activeMenuIndex: null });
}
};
// Add event listener for outside clicks when a menu is open
if (isMenuOpen && !this._outsideClickHandler) {
this._outsideClickHandler = () => {
handleOutsideClick();
};
document.addEventListener("click", this._outsideClickHandler);
} else if (!isMenuOpen && this._outsideClickHandler) {
document.removeEventListener("click", this._outsideClickHandler);
this._outsideClickHandler = null;
}
return (
<div key={index} style={{ display: "inline-block", marginRight: "10px", position: "relative" }}>
<input
type="file"
accept=".zip,.json"
onChange={(e) => this.handleFileUpload(e, index)}
style={{ display: "none" }}
id={`fileInput${index}`}
/>
<button
onClick={handleClick}
className={`dataset-button ${
isActive ? "dataset-button-active" : isUploaded ? "dataset-button-uploaded" : "dataset-button-empty"
}`}
>
{isUploaded ? `Dataset ${index + 1}` : `Select Dataset ${index + 1}`}
{isUploaded && isActive && (
<span className="dataset-button-actions" onClick={toggleMenu}>
▼
{isMenuOpen && (
<div className="dataset-button-menu">
<div className="dataset-button-menu-item export" onClick={handleSaveClick}>
Export
</div>
<div className="dataset-button-menu-item prune" onClick={handlePruneClick}>
Prune
</div>
<div className="dataset-button-menu-item delete" onClick={handleDeleteClick}>
Delete
</div>
</div>
)}
</span>
)}
</button>
</div>
);
};
async showDataSourceDialog(index) {
log("showDataSourceDialog");
const dialog = document.createElement("dialog");
dialog.innerHTML = `
<div>
<h3>Choose Data Source for Dataset ${index + 1}</h3>
<button id="fileUpload">Load JSON/ZIP File</button>
<button id="cloudLogging">Connect to Cloud Logging</button>
<button id="cancel">Cancel</button>
</div>
`;
document.body.appendChild(dialog);
dialog.showModal();
return new Promise((resolve) => {
dialog.querySelector("#fileUpload").onclick = () => {
dialog.remove();
resolve("file");
};
dialog.querySelector("#cloudLogging").onclick = () => {
dialog.remove();
resolve("cloud");
};
dialog.querySelector("#cancel").onclick = () => {
dialog.remove();
resolve(null);
};
});
}
async showCloudLoggingDialog() {
const dialog = document.createElement("dialog");
dialog.className = "cloud-logging-dialog";
document.body.appendChild(dialog);
const dialogRootElement = document.createElement("div");
dialog.appendChild(dialogRootElement);
const dialogRoot = createRoot(dialogRootElement);
dialog.showModal();
return new Promise((resolve) => {
const cleanupAndResolve = (result) => {
dialogRoot.unmount();
dialog.remove();
resolve(result);
};
const handleError = (error) => {
console.error("Cloud Logging Error:", error);
toast.error(error.message || "Failed to fetch logs. Please try again.");
cleanupAndResolve(null);
};
const handleFileUpload = (event) => {
console.log("File upload selected from Cloud Logging dialog");
const file = event?.target?.files?.[0];
if (file) {
cleanupAndResolve({ file });
}
};
const cloudLoggingComponent = React.createElement(CloudLogging, {
onLogsReceived: (logs) => {
log(`Received ${logs.length} logs from Cloud Logging`);
if (logs.length === 0) {
toast.warning("No logs found matching your criteria.");
cleanupAndResolve(null);
} else {
resolve({ logs });
cleanupAndResolve({ logs });
}
},
onFileUpload: handleFileUpload,
setError: handleError,
});
dialogRoot.render(cloudLoggingComponent);
});
}
setCenterOnLocation = (func) => {
this.centerOnLocation = func;
};
setRenderMarkerOnMap = (func) => {
this.renderMarkerOnMap = func;
};
toggleDynamicMarker = (location) => {
log("App.js: toggleDynamicMarker called for location:", location);
const locationKey = `${location.lat}_${location.lng}`;
const markerState = this.state.dynamicMarkerLocations[locationKey];
const shouldAdd = !markerState;
if (shouldAdd) {
const color = MARKER_COLORS[this.nextColorIndex];
this.nextColorIndex = (this.nextColorIndex + 1) % MARKER_COLORS.length;
if (this.renderMarkerOnMap) {
this.renderMarkerOnMap(location, color, true);
} else {
console.error("renderMarkerOnMap function not available on App component");
return;
}
this.setState((prevState) => {
const newLocations = {
...prevState.dynamicMarkerLocations,
[locationKey]: { location, color },
};
log(`App.js: Adding marker to state for key ${locationKey} with color ${color}`);
return { dynamicMarkerLocations: newLocations };
});
} else {
if (this.renderMarkerOnMap) {
this.renderMarkerOnMap(location, markerState.color, false);
}
this.setState((prevState) => {
const newLocations = { ...prevState.dynamicMarkerLocations };
delete newLocations[locationKey];
log(`App.js: Removing marker from state for key ${locationKey}`);
return { dynamicMarkerLocations: newLocations };
});
}
};
switchDataset = async (index) => {
log(`Attempting to switch to dataset ${index}`);
if (this.state.uploadedDatasets[index] !== "Uploaded") {
console.error(`Attempted to switch to dataset ${index}, but it's not uploaded or is empty`);
return;
}
try {
const data = await getUploadedData(index);
if (data && data.rawLogs && Array.isArray(data.rawLogs) && data.rawLogs.length > 0) {
const tripLogs = new TripLogs(data.rawLogs, data.solutionType);
const newVisibleToggles = getVisibleToggles(data.solutionType);
let newToggleOptions = { ...this.state.toggleOptions };
let newExtraColumns = [...this.state.extraColumns];
if (data.solutionType === "LMFS") {
const tasksToggleId = "showTasksAsCreated";
const tasksToggle = _.find(ALL_TOGGLES, { id: tasksToggleId });
if (tasksToggle && !newToggleOptions[tasksToggleId]) {
log("Auto-enabling 'showTasksAsCreated' for LMFS dataset.");
newToggleOptions[tasksToggleId] = true;
newExtraColumns = _.union(newExtraColumns, tasksToggle.columns);
}
}
this.setState(
(prevState) => ({
activeDatasetIndex: index,
timeRange: { minTime: tripLogs.minDate.getTime(), maxTime: tripLogs.maxDate.getTime() },
initialMapBounds: data.bounds,
currentLogData: {
...prevState.currentLogData,
tripLogs: tripLogs,
solutionType: data.solutionType,
},
visibleToggles: newVisibleToggles,
dynamicMarkerLocations: {}, // Clear markers when switching datasets
toggleOptions: newToggleOptions,
extraColumns: newExtraColumns,
}),
() => {
log(`Switched to dataset ${index}`);
log(`New time range: ${tripLogs.minDate} - ${tripLogs.maxDate}`);
const savedRowIndex = this.state.selectedRowIndexPerDataset[index];
log(`Attempting to restore row at index ${savedRowIndex} for dataset ${index}`);
setTimeout(() => {
if (savedRowIndex >= 0) {
const minDate = new Date(this.state.timeRange.minTime);
const maxDate = new Date(this.state.timeRange.maxTime);
const logs = tripLogs.getLogs_(minDate, maxDate).value();
if (savedRowIndex < logs.length) {
log(`Restoring row at index ${savedRowIndex}`);
const rowToSelect = logs[savedRowIndex];
this.setState({ featuredObject: rowToSelect }, () => {
this.focusOnSelectedRow();
const lat = _.get(rowToSelect, "lastlocation.rawlocation.latitude");
const lng = _.get(rowToSelect, "lastlocation.rawlocation.longitude");
if (lat && lng && this.centerOnLocation) {
log(`Centering map on restored row location: ${lat}, ${lng}`);
this.centerOnLocation(lat, lng);
} else {
log("Unable to center map: coordinates not found or centerOnLocation not available");
}
});
} else {
log(`Index ${savedRowIndex} out of bounds (max: ${logs.length - 1}), selecting first row`);
this.selectFirstRow();
}
} else {
log(`No previously saved row index for dataset ${index}, selecting first row`);
this.selectFirstRow();
}
}, 300);
this.updateMapAndAssociatedData();
}
);
} else {
console.error(`Invalid or empty data structure for dataset ${index}`);
}
} catch (error) {
console.error(`Error switching to dataset ${index}:`, error);
}
};
toggleClickHandler(id) {
const toggle = _.find(ALL_TOGGLES, { id });
const newValue = !this.state.toggleOptions[id];
this.updateToggleState(newValue, id, toggle.columns);
}
render() {
const {
featuredObject,
timeRange,
currentLogData,
toggleOptions,
extraColumns,
dynamicMarkerLocations,
visibleToggles,
} = this.state;
const selectedEventTime = featuredObject?.timestamp ? new Date(featuredObject.timestamp).getTime() : null;
return (
<div className="app-container">
<ToastContainer position="top-right" autoClose={5000} />
<div className="main-content">
<div className="map-and-control-section">
<div className="map-container">
<Map
key={`map-${this.state.activeDatasetIndex}`}
logData={currentLogData}
rangeStart={timeRange.minTime}
rangeEnd={timeRange.maxTime}
selectedRow={featuredObject}
toggles={visibleToggles}
toggleOptions={toggleOptions}
setFeaturedObject={this.setFeaturedObject}
setTimeRange={this.setTimeRange}
setCenterOnLocation={this.setCenterOnLocation}
setRenderMarkerOnMap={this.setRenderMarkerOnMap}
focusSelectedRow={this.focusOnSelectedRow}
initialMapBounds={this.state.initialMapBounds}
/>
</div>
<TimeSlider
logData={currentLogData}
curMin={timeRange.minTime}
curMax={timeRange.maxTime}
onSliderChange={this.onSliderChangeDebounced}
selectedEventTime={selectedEventTime}
onRowSelect={(row, rowIndex) => this.onSelectionChange(row, rowIndex)}
centerOnLocation={this.centerOnLocation}
focusSelectedRow={this.focusOnSelectedRow}
/>
<ToggleBar
toggles={visibleToggles}
toggleState={toggleOptions}
clickHandler={(id) => this.toggleClickHandler(id)}
/>
<div className="nav-controls">
<div className="button-row">
<div className="playback-controls">
<div>
<button onClick={this.selectFirstRow}>First</button>
<button onClick={this.selectLastRow}>Last</button>
</div>
<div>
<button onClick={this.handlePreviousEvent}>< Previous</button>
<button onClick={this.handleNextEvent}>Next ></button>
</div>
<div>
<button onClick={this.handlePlayStop}>{this.state.isPlaying ? "Stop" : "Play"}</button>
<select
value={this.state.playSpeed}
onChange={this.handleSpeedChange}
disabled={this.state.isPlaying}
>
<option value="250">0.25s</option>
<option value="500">0.5s</option>
<option value="1000">1s</option>
<option value="2500">2.5s</option>
<option value="5000">5s</option>
</select>
</div>
</div>
<div className="dataset-controls">{[0, 1, 2, 3, 4].map((index) => this.renderUploadButton(index))}</div>
<div className="help-text">
<div>All Data remains client side</div>
<div>
<strong><</strong> and <strong>></strong> to quickly navigate selected events
</div>
</div>
</div>
</div>
</div>
<div style={{ flex: 1, overflow: "auto" }}>
<LogTable
logData={currentLogData}
style={{ width: "100%" }}
timeRange={timeRange}
extraColumns={extraColumns}
onSelectionChange={(rowData, rowIndex) => this.onSelectionChange(rowData, rowIndex)}
setFocusOnRowFunction={this.setFocusOnRowFunction}
centerOnLocation={this.centerOnLocation}
/>
</div>
</div>
<div className="dataframe-section">
<Dataframe
featuredObject={featuredObject}
extraColumns={extraColumns}
onColumnToggle={(path) => this.onDataframePropClick(path)}
onToggleMarker={this.toggleDynamicMarker}
dynamicMarkerLocations={dynamicMarkerLocations}
/>
</div>
</div>
);
}
}
export { App as default };