forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataframe.js
More file actions
188 lines (170 loc) · 5.38 KB
/
Dataframe.js
File metadata and controls
188 lines (170 loc) · 5.38 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
// src/Dataframe.js
import { useCallback } from "react";
import JsonView from "react18-json-view";
import "react18-json-view/src/style.css";
import { log } from "./Utils";
import { toast } from "react-toastify";
import _ from "lodash";
// We'll use this list to prevent users from adding default columns again.
const DEFAULT_COLUMN_PATHS = [
"formattedDate",
"@type",
"lastlocation.rawlocationsensor",
"lastlocation.locationsensor",
"response.vehiclestate",
"response.state",
"response.tripstatus",
"request.deliveryvehicle.remainingdistancemeters",
"navStatus",
];
// Helper to check if a value is a location object
const isLocationObject = (value) => {
return (
value &&
typeof value === "object" &&
!Array.isArray(value) &&
Object.prototype.hasOwnProperty.call(value, "latitude") &&
Object.prototype.hasOwnProperty.call(value, "longitude") &&
typeof value.latitude === "number" &&
typeof value.longitude === "number"
);
};
const MarkerIcon = ({ color }) => (
<svg width="17" height="17" viewBox="0 0 24 24" fill={color} xmlns="http://www.w3.org/2000/svg">
<path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7z" />
</svg>
);
// Stateless component to render a button for location objects
const LocationButton = ({ color, onToggle }) => {
const isAdded = color !== "lightgrey";
const handleToggle = (e) => {
e.stopPropagation();
log(`LocationButton: Toggle marker clicked. Marker will be ${isAdded ? "removed" : "added"}.`);
onToggle();
};
return (
<button
onClick={handleToggle}
title={isAdded ? "Remove from map" : "Show on map"}
style={{
marginLeft: "4px",
cursor: "pointer",
background: "none",
border: "none",
padding: "0",
display: "inline-flex",
alignItems: "center",
verticalAlign: "middle",
}}
>
<MarkerIcon color={color} />
</button>
);
};
const AddColumnButton = (props) => {
return (
<button
{...props}
title="Add/Remove as column in LogTable"
style={{
cursor: "pointer",
background: "none",
border: "none",
color: "var(--json-property)",
fontWeight: "bold",
padding: "0 4px",
}}
>
+
</button>
);
};
function Dataframe({ featuredObject, extraColumns, onColumnToggle, onToggleMarker, dynamicMarkerLocations }) {
const handleCopyRoot = useCallback(() => {
if (!featuredObject) return;
const objectToCopy = _.omit(featuredObject, ["lastlocation", "lastlocationResponse"]);
const jsonString = JSON.stringify(objectToCopy, null, 2);
navigator.clipboard
.writeText(jsonString)
.then(() => {
toast.success("Object copied to clipboard");
})
.catch((err) => {
console.error("Failed to copy object: ", err);
toast.error("Failed to copy object.");
});
}, [featuredObject]);
const CustomLocationOperation = useCallback(
({ node }) => {
if (isLocationObject(node)) {
log("Dataframe: Rendering location button for node.");
const locationKey = `${node.latitude}_${node.longitude}`;
const markerState = dynamicMarkerLocations[locationKey];
const color = markerState ? markerState.color : "lightgrey";
return (
<LocationButton color={color} onToggle={() => onToggleMarker({ lat: node.latitude, lng: node.longitude })} />
);
}
return null;
},
[dynamicMarkerLocations, onToggleMarker]
);
const customizeCopy = useCallback(
(node, nodeMeta) => {
if (nodeMeta && nodeMeta.currentPath) {
const path = nodeMeta.currentPath.join(".");
if (DEFAULT_COLUMN_PATHS.includes(path)) {
log(`Prevented adding default column: "${path}"`);
toast.warn(`Column "${path}" is already displayed by default.`);
return " ";
}
const isCurrentlyAdded = extraColumns.some((c) => c === path);
onColumnToggle(path);
if (isCurrentlyAdded) {
toast.info(`Column "${path}" removed`);
} else {
toast.success(`Column "${path}" added`);
}
}
return " ";
},
[extraColumns, onColumnToggle]
);
const customizeNode = useCallback(({ node }) => {
if (typeof node === "object" && node !== null) {
return { enableClipboard: false };
}
return { enableClipboard: true };
}, []);
const shouldCollapse = useCallback(({ indexOrName, depth }) => {
if (depth === 1 && typeof indexOrName === "undefined") {
return false;
}
if (depth === 2 && indexOrName === "request") {
return false;
}
if (depth === 3 && ["vehicle", "trip", "deliveryvehicle", "task"].includes(indexOrName)) {
return false;
}
return true;
}, []);
return (
<div style={{ display: "flex", flexDirection: "column", height: "100%" }}>
<div style={{ padding: "4px 8px", flexShrink: 0 }}>
<button onClick={handleCopyRoot}>Copy Object</button>
</div>
<div style={{ overflow: "auto", flexGrow: 1 }}>
<JsonView
src={featuredObject}
collapsed={shouldCollapse}
enableClipboard={true}
customizeNode={customizeNode}
customizeCopy={customizeCopy}
CopyComponent={AddColumnButton}
CustomOperation={CustomLocationOperation}
/>
</div>
</div>
);
}
export default Dataframe;