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
224 lines (202 loc) · 6.4 KB
/
Dataframe.js
File metadata and controls
224 lines (202 loc) · 6.4 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
// src/Dataframe.js
import { useCallback, useState } 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 }) {
// We manage the expansion state ourselves to work around state-loss.
// The key is a string `depth-name`, and the value is `true` if expanded.
const [expandedPaths, setExpandedPaths] = useState({
"2-request": true,
"3-vehicle": true,
"3-trip": true,
"3-deliveryvehicle": true,
"3-task": true,
});
const handleCollapse = useCallback(
(params) => {
const { indexOrName, depth, isCollapsing } = params;
const pathKey = `${depth}-${indexOrName}`;
// The library's `isCollapsing` is true when a node is expanding.
const shouldBeExpanded = isCollapsing;
const isCurrentlyExpanded = !!expandedPaths[pathKey];
if (shouldBeExpanded === isCurrentlyExpanded) {
return;
}
setExpandedPaths((prev) => {
const newPaths = { ...prev };
if (shouldBeExpanded) {
newPaths[pathKey] = true;
} else {
delete newPaths[pathKey];
}
return newPaths;
});
},
[expandedPaths]
);
const shouldCollapse = useCallback(
({ indexOrName, depth }) => {
if (depth === 1 && typeof indexOrName === "undefined") {
return false;
}
const pathKey = `${depth}-${indexOrName}`;
// A node is collapsed if its path key is NOT in our expandedPaths state.
return !expandedPaths[pathKey];
},
[expandedPaths]
);
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)) {
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 };
}, []);
return (
<div style={{ display: "flex", flexDirection: "column", height: "100%", position: "relative" }}>
<div style={{ padding: "4px 8px", flexShrink: 0 }}>
<button onClick={handleCopyRoot} className="copy-object-button">
Copy Object
</button>
</div>
<div className="dataframe-content">
<JsonView
src={featuredObject}
collapsed={shouldCollapse}
onCollapse={handleCollapse}
enableClipboard={true}
customizeNode={customizeNode}
customizeCopy={customizeCopy}
CopyComponent={AddColumnButton}
CustomOperation={CustomLocationOperation}
/>
</div>
</div>
);
}
export default Dataframe;