This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
GlobalVariablesTable.tsx
368 lines (339 loc) · 12 KB
/
GlobalVariablesTable.tsx
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2020-2021 Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found at http://www.apache.org/licenses/LICENSE-2.0
// You may not use this file except in compliance with the License.
import CloseIcon from "@mdi/svg/svg/close.svg";
import DotsVerticalIcon from "@mdi/svg/svg/dots-vertical.svg";
import { partition, pick, union, without } from "lodash";
import React, { useEffect, useMemo, useCallback, useRef, useState, ReactElement } from "react";
import styled, { css, keyframes } from "styled-components";
import { usePreviousValue } from "../util/hooks";
import ChildToggle from "@foxglove-studio/app/components/ChildToggle";
import Flex from "@foxglove-studio/app/components/Flex";
import Icon from "@foxglove-studio/app/components/Icon";
import Menu, { Item } from "@foxglove-studio/app/components/Menu";
import Tooltip from "@foxglove-studio/app/components/Tooltip";
import { JSONInput } from "@foxglove-studio/app/components/input/JSONInput";
import { ValidatedResizingInput } from "@foxglove-studio/app/components/input/ValidatedResizingInput";
import useGlobalVariables, { GlobalVariables } from "@foxglove-studio/app/hooks/useGlobalVariables";
import { memoizedGetLinkedGlobalVariablesKeyByName } from "@foxglove-studio/app/panels/ThreeDimensionalViz/Interactions/interactionUtils";
import useLinkedGlobalVariables from "@foxglove-studio/app/panels/ThreeDimensionalViz/Interactions/useLinkedGlobalVariables";
import { colors as sharedColors } from "@foxglove-studio/app/util/sharedStyleConstants";
// The minimum amount of time to wait between showing the global variable update animation again
export const ANIMATION_RESET_DELAY_MS = 3000;
// Returns an keyframe object that animates between two styles– "highlight twice then return to normal"
export const makeFlashAnimation = (initialCssProps: any, highlightCssProps: any) => {
return css`
${keyframes`
0%, 20%, 100% {
${initialCssProps}
}
10%, 30%, 80% {
${highlightCssProps}
}
`}
`;
};
const SGlobalVariablesTable = styled.div`
display: flex;
flex-direction: column;
white-space: nowrap;
color: ${sharedColors.LIGHT};
table {
width: calc(100% + 1px);
}
thead {
user-select: none;
border-bottom: 1px solid ${sharedColors.BORDER_LIGHT};
}
th,
td {
padding: 0px 16px;
line-height: 100%;
border: none;
}
tr:first-child th {
padding: 8px 16px;
border: none;
text-align: left;
color: rgba(255, 255, 255, 0.6);
min-width: 120px;
}
td {
input {
background: none !important;
color: inherit;
width: 100%;
padding-left: 0;
padding-right: 0;
min-width: 40px;
}
&:last-child {
color: rgba(255, 255, 255, 0.6);
}
}
`;
const SIconWrapper = styled.span<{ isOpen?: boolean }>`
display: inline-block;
cursor: pointer;
padding: 0;
color: ${sharedColors.LIGHT};
svg {
opacity: ${({ isOpen }) => (isOpen ? 1 : undefined)};
}
`;
const SLinkedTopicsSpan = styled.span`
overflow: hidden;
text-overflow: ellipsis;
direction: rtl;
max-width: 240px;
margin-left: -5px;
`;
const FlashRowAnimation = makeFlashAnimation(
css`
background: transparent;
`,
css`
background: ${sharedColors.HIGHLIGHT_MUTED};
`,
);
const AnimationDuration = 3;
const SAnimatedRow = styled.tr<{ animate: boolean; skipAnimation: any }>`
background: transparent;
animation: ${({ animate, skipAnimation }) =>
animate && !skipAnimation ? FlashRowAnimation : "none"}
${AnimationDuration}s ease-in-out;
animation-iteration-count: 1;
animation-fill-mode: forwards;
border-bottom: 1px solid ${sharedColors.BORDER_LIGHT};
`;
export function isActiveElementEditable() {
const activeEl = document.activeElement;
return (
activeEl &&
((activeEl as any).isContentEditable ||
activeEl.tagName === "INPUT" ||
activeEl.tagName === "TEXTAREA")
);
}
const changeGlobalKey = (
newKey: any,
oldKey: any,
globalVariables: any,
idx: any,
overwriteGlobalVariables: any,
) => {
const keys = Object.keys(globalVariables);
overwriteGlobalVariables({
...pick(globalVariables, keys.slice(0, idx)),
[newKey]: globalVariables[oldKey],
...pick(globalVariables, keys.slice(idx + 1)),
});
};
function LinkedGlobalVariableRow({ name }: { name: string }): ReactElement {
const [isOpen, setIsOpen] = useState<boolean>(false);
const { globalVariables, setGlobalVariables } = useGlobalVariables();
const { linkedGlobalVariables, setLinkedGlobalVariables } = useLinkedGlobalVariables();
const linkedTopicPaths = useMemo(
() =>
linkedGlobalVariables
.filter((variable: any) => variable.name === name)
.map(({ topic, markerKeyPath }: any) => [topic, ...markerKeyPath].join(".")),
[linkedGlobalVariables, name],
);
const unlink = useCallback(
(path) => {
setLinkedGlobalVariables(
linkedGlobalVariables.filter(
({ name: varName, topic, markerKeyPath }: any) =>
!(varName === name && [topic, ...markerKeyPath].join(".") === path),
),
);
},
[linkedGlobalVariables, name, setLinkedGlobalVariables],
);
const unlinkAndDelete = useCallback(() => {
const newLinkedGlobalVariables = linkedGlobalVariables.filter(
({ name: varName }: any) => varName !== name,
);
setLinkedGlobalVariables(newLinkedGlobalVariables);
setGlobalVariables({ [name]: undefined });
}, [linkedGlobalVariables, name, setGlobalVariables, setLinkedGlobalVariables]);
return (
<>
<td>${name}</td>
<td width="100%">
<JSONInput
value={JSON.stringify(globalVariables[name] ?? "")}
onChange={(newVal) => setGlobalVariables({ [name]: newVal })}
/>
</td>
<td>
<Flex center style={{ justifyContent: "space-between" }}>
<Flex style={{ marginRight: 16 }}>
{linkedTopicPaths.length > 1 && <span>({linkedTopicPaths.length})</span>}
<Tooltip
contents={
linkedTopicPaths.length ? (
<>
<div style={{ fontWeight: "bold", opacity: 0.4 }}>
{linkedTopicPaths.length} LINKED TOPIC{linkedTopicPaths.length > 1 ? "S" : ""}
</div>
{linkedTopicPaths.map((path: any) => (
<div key={path} style={{ paddingTop: "5px" }}>
{path}
</div>
))}
</>
) : null
}
>
<SLinkedTopicsSpan>
{linkedTopicPaths.length ? <bdi>{linkedTopicPaths.join(", ")}</bdi> : "--"}
</SLinkedTopicsSpan>
</Tooltip>
</Flex>
<ChildToggle isOpen={isOpen} onToggle={() => setIsOpen(!isOpen)} position="below">
<SIconWrapper isOpen={isOpen}>
<Icon small dataTest={`unlink-${name}`}>
<DotsVerticalIcon />
</Icon>
</SIconWrapper>
<Menu style={{ padding: "4px 0px" }}>
{linkedTopicPaths.map((path: any) => (
<Item dataTest="unlink-path" key={path} onClick={() => unlink(path)}>
Remove <span style={{ color: sharedColors.LIGHT, opacity: 1 }}>{path}</span>
</Item>
))}
<Item onClick={unlinkAndDelete}>Delete global variable</Item>
</Menu>
</ChildToggle>
</Flex>
</td>
</>
);
}
function GlobalVariablesTable(): ReactElement {
const { globalVariables, setGlobalVariables, overwriteGlobalVariables } = useGlobalVariables();
const { linkedGlobalVariables } = useLinkedGlobalVariables();
const globalVariableNames = Object.keys(globalVariables);
const linkedGlobalVariablesKeyByName = memoizedGetLinkedGlobalVariablesKeyByName(
linkedGlobalVariables,
);
const [linked, unlinked] = partition(
globalVariableNames,
(name) => !!linkedGlobalVariablesKeyByName[name],
);
// Don't run the animation when the Table first renders
const skipAnimation = useRef<boolean>(true);
useEffect(() => {
const timeoutId = setTimeout(() => (skipAnimation.current = false), ANIMATION_RESET_DELAY_MS);
return () => clearTimeout(timeoutId);
}, []);
const previousGlobalVariables = usePreviousValue(globalVariables);
const previousGlobalVariablesRef = useRef<GlobalVariables | null | undefined>(
previousGlobalVariables,
);
previousGlobalVariablesRef.current = previousGlobalVariables;
const [changedVariables, setChangedVariables] = useState<string[]>([]);
useEffect(() => {
if (skipAnimation.current || isActiveElementEditable()) {
return;
}
const newChangedVariables = union(
Object.keys(globalVariables),
Object.keys(previousGlobalVariablesRef.current || {}),
).filter((name) => {
const previousValue = previousGlobalVariablesRef.current?.[name];
return previousValue !== globalVariables[name];
});
setChangedVariables(newChangedVariables);
const timerId = setTimeout(() => setChangedVariables([]), ANIMATION_RESET_DELAY_MS);
return () => clearTimeout(timerId);
}, [globalVariables, skipAnimation]);
return (
<SGlobalVariablesTable>
<table>
<thead>
<tr>
<th>Global variable</th>
<th>Value</th>
<th>Topic(s)</th>
</tr>
</thead>
<tbody>
{linked.map((name, idx) => (
<SAnimatedRow
key={`linked-${idx}`}
skipAnimation={skipAnimation.current}
animate={changedVariables.includes(name)}
>
<LinkedGlobalVariableRow name={name} />
</SAnimatedRow>
))}
{unlinked.map((name, idx) => (
<SAnimatedRow
key={`unlinked-${idx}`}
skipAnimation={skipAnimation}
animate={changedVariables.includes(name)}
>
<td data-test="global-variable-key">
<ValidatedResizingInput
value={name}
dataTest={`global-variable-key-input-${name}`}
onChange={(newKey) =>
changeGlobalKey(
newKey,
name,
globalVariables,
linked.length + idx,
overwriteGlobalVariables,
)
}
invalidInputs={without(globalVariableNames, name).concat("")}
/>
</td>
<td width="100%">
<JSONInput
dataTest={`global-variable-value-input-${JSON.stringify(
globalVariables[name] ?? "",
)}`}
value={JSON.stringify(globalVariables[name] ?? "")}
onChange={(newVal) => setGlobalVariables({ [name]: newVal })}
/>
</td>
<td width="100%">
<Flex center style={{ justifyContent: "space-between" }}>
--
<SIconWrapper onClick={() => setGlobalVariables({ [name]: undefined })}>
<Icon small>
<CloseIcon />
</Icon>
</SIconWrapper>
</Flex>
</td>
</SAnimatedRow>
))}
</tbody>
</table>
<Flex style={{ margin: "20px 16px 16px", justifyContent: "flex-end" }}>
<button
disabled={globalVariables[""] != null}
onClick={() => setGlobalVariables({ "": "" })}
data-test="add-variable-btn"
>
Add variable
</button>
</Flex>
</SGlobalVariablesTable>
);
}
export default GlobalVariablesTable;