-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ResourceCard.tsx
397 lines (359 loc) · 12.8 KB
/
ResourceCard.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
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
/**
* Copyright 2023 Gravitational, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { useState, useEffect, useLayoutEffect, useRef } from 'react';
import styled, { css } from 'styled-components';
import { Box, ButtonLink, Flex, Label, Text } from 'design';
import { StyledCheckbox } from 'design/Checkbox';
import { ResourceIcon } from 'design/ResourceIcon';
import { makeLabelTag } from 'teleport/components/formatters';
import { HoverTooltip } from 'shared/components/ToolTip';
import { ResourceItemProps } from '../types';
import { PinButton } from '../shared/PinButton';
import { CopyButton } from '../shared/CopyButton';
// Since we do a lot of manual resizing and some absolute positioning, we have
// to put some layout constants in place here.
const labelHeight = 20; // px
const labelVerticalMargin = 1; // px
const labelRowHeight = labelHeight + labelVerticalMargin * 2;
/**
* This box serves twofold purpose: first, it prevents the underlying icon from
* being squeezed if the parent flexbox starts shrinking items. Second, it
* prevents the icon from magically occupying too much space, since the SVG
* element somehow forces the parent to occupy at least full line height.
*/
const ResTypeIconBox = styled(Box)`
line-height: 0;
`;
export function ResourceCard({
name,
primaryIconName,
SecondaryIcon,
onLabelClick,
cardViewProps,
ActionButton,
labels,
pinningSupport,
pinned,
pinResource,
selectResource,
selected,
}: Omit<ResourceItemProps, 'listViewProps' | 'expandAllLabels'>) {
const { primaryDesc, secondaryDesc } = cardViewProps;
const [showMoreLabelsButton, setShowMoreLabelsButton] = useState(false);
const [showAllLabels, setShowAllLabels] = useState(false);
const [numMoreLabels, setNumMoreLabels] = useState(0);
const [hovered, setHovered] = useState(false);
const innerContainer = useRef<Element | null>(null);
const labelsInnerContainer = useRef(null);
const collapseTimeout = useRef<ReturnType<typeof setTimeout>>(null);
// This effect installs a resize observer whose purpose is to detect the size
// of the component that contains all the labels. If this component is taller
// than the height of a single label row, we show a "+x more" button.
useLayoutEffect(() => {
if (!labelsInnerContainer.current) return;
const observer = new ResizeObserver(entries => {
const container = entries[0];
// We're taking labelRowHeight * 1.5 just in case some glitch adds or
// removes a pixel here and there.
const moreThanOneRow =
container.contentBoxSize[0].blockSize > labelRowHeight * 1.5;
setShowMoreLabelsButton(moreThanOneRow);
// Count number of labels in the first row. This will let us calculate and
// show the number of labels left out from the view.
const labelElements = [
...entries[0].target.querySelectorAll('[data-is-label]'),
];
const firstLabelPos = labelElements[0]?.getBoundingClientRect().top;
// Find the first one below.
const firstLabelInSecondRow = labelElements.findIndex(
e => e.getBoundingClientRect().top > firstLabelPos
);
setNumMoreLabels(
firstLabelInSecondRow > 0
? labelElements.length - firstLabelInSecondRow
: 0
);
});
observer.observe(labelsInnerContainer.current);
return () => {
observer.disconnect();
};
}, []);
// Clear the timeout on unmount to prevent changing a state of an unmounted
// component.
useEffect(() => () => clearTimeout(collapseTimeout.current), []);
const onMoreLabelsClick = () => {
setShowAllLabels(true);
};
const onMouseLeave = () => {
// If the user expanded the labels and then scrolled down enough to hide the
// top of the card, we scroll back up and collapse the labels with a small
// delay to keep the user from losing focus on the card that they were
// looking at. The delay is picked by hand, since there's no (easy) way to
// know when the animation ends.
if (
showAllLabels &&
(innerContainer.current?.getBoundingClientRect().top ?? 0) < 0
) {
innerContainer.current?.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
clearTimeout(collapseTimeout.current);
collapseTimeout.current = setTimeout(() => setShowAllLabels(false), 700);
} else {
// Otherwise, we just collapse the labels immediately.
setShowAllLabels(false);
}
};
return (
<CardContainer
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
<CardOuterContainer showAllLabels={showAllLabels}>
<CardInnerContainer
ref={innerContainer}
p={3}
// we set padding left a bit larger so we can have space to absolutely
// position the pin/checkbox buttons
pl={6}
alignItems="start"
onMouseLeave={onMouseLeave}
pinned={pinned}
selected={selected}
>
<HoverTooltip tipContent={selected ? 'Deselect' : 'Select'}>
<StyledCheckbox
css={`
position: absolute;
top: 16px;
left: 16px;
`}
checked={selected}
onChange={selectResource}
/>
</HoverTooltip>
<Box
css={`
position: absolute;
// we position far from the top so the layout of the pin doesn't change if we expand the card
top: ${props => props.theme.space[9]}px;
transition: none;
left: 16px;
`}
>
<PinButton
setPinned={pinResource}
pinned={pinned}
pinningSupport={pinningSupport}
hovered={hovered}
/>
</Box>
<ResourceIcon
name={primaryIconName}
width="45px"
height="45px"
ml={2}
/>
{/* MinWidth is important to prevent descriptions from overflowing. */}
<Flex flexDirection="column" flex="1" minWidth="0" ml={3} gap={1}>
<Flex flexDirection="row" alignItems="center">
<SingleLineBox flex="1">
<HoverTooltip tipContent={name} showOnlyOnOverflow>
<Text typography="h5" fontWeight={300}>
{name}
</Text>
</HoverTooltip>
</SingleLineBox>
{hovered && <CopyButton name={name} mr={2} />}
{ActionButton}
</Flex>
<Flex flexDirection="row" alignItems="center">
<ResTypeIconBox>
<SecondaryIcon size={18} />
</ResTypeIconBox>
{primaryDesc && (
<SingleLineBox ml={1} title={primaryDesc}>
<Text typography="body2" color="text.slightlyMuted">
{primaryDesc}
</Text>
</SingleLineBox>
)}
{secondaryDesc && (
<SingleLineBox ml={2} title={secondaryDesc}>
<Text typography="body2" color="text.muted">
{secondaryDesc}
</Text>
</SingleLineBox>
)}
</Flex>
<LabelsContainer showAll={showAllLabels}>
<LabelsInnerContainer ref={labelsInnerContainer}>
<MoreLabelsButton
style={{
visibility:
showMoreLabelsButton && !showAllLabels
? 'visible'
: 'hidden',
}}
onClick={onMoreLabelsClick}
>
+ {numMoreLabels} more
</MoreLabelsButton>
{labels.map((label, i) => {
const labelText = makeLabelTag(label);
return (
<StyledLabel
key={i}
title={labelText}
onClick={() => onLabelClick?.(label)}
kind="secondary"
data-is-label=""
>
{labelText}
</StyledLabel>
);
})}
</LabelsInnerContainer>
</LabelsContainer>
</Flex>
</CardInnerContainer>
</CardOuterContainer>
</CardContainer>
);
}
/**
* The outer container's purpose is to reserve horizontal space on the resource
* grid. It holds the inner container that normally holds a regular layout of
* the card, and is fully contained inside the outer container. Once the user
* clicks the "more" button, the inner container "pops out" by changing its
* position to absolute.
*
* TODO(bl-nero): Known issue: this doesn't really work well with one-column
* layout; we may need to globally set the card height to fixed size on the
* outer container.
*/
const CardContainer = styled(Box)`
position: relative;
`;
const CardOuterContainer = styled(Box)`
border-radius: ${props => props.theme.radii[3]}px;
${props =>
props.showAllLabels &&
css`
position: absolute;
left: 0;
right: 0;
z-index: 1;
`}
transition: all 150ms;
${CardContainer}:hover & {
background-color: ${props => props.theme.colors.levels.surface};
// We use a pseudo element for the shadow with position: absolute in order to prevent
// the shadow from increasing the size of the layout and causing scrollbar flicker.
:after {
box-shadow: ${props => props.theme.boxShadow[3]};
border-radius: ${props => props.theme.radii[3]}px;
content: '';
position: absolute;
top: 0;
left: 0;
z-index: -1;
width: 100%;
height: 100%;
}
}
`;
/**
* The inner container that normally holds a regular layout of the card, and is
* fully contained inside the outer container. Once the user clicks the "more"
* button, the inner container "pops out" by changing its position to absolute.
*
* TODO(bl-nero): Known issue: this doesn't really work well with one-column
* layout; we may need to globally set the card height to fixed size on the
* outer container.
*/
const CardInnerContainer = styled(Flex)`
border: ${props => props.theme.borders[2]}
${props => props.theme.colors.spotBackground[0]};
border-radius: ${props => props.theme.radii[3]}px;
background-color: ${props => getBackgroundColor(props)};
:hover {
// Make the border invisible instead of removing it, this is to prevent things from shifting due to the size change.
border: ${props => props.theme.borders[2]} rgba(0, 0, 0, 0);
}
`;
const getBackgroundColor = props => {
if (props.selected) {
return props.theme.colors.interactive.tonal.primary[2];
}
if (props.pinned) {
return props.theme.colors.interactive.tonal.primary[0];
}
return 'transparent';
};
const SingleLineBox = styled(Box)`
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
`;
/**
* The outer labels container is resized depending on whether we want to show a
* single row, or all labels. It hides the internal container's overflow if more
* than one row of labels exist, but is not yet visible.
*/
const LabelsContainer = styled(Box)`
${props => (props.showAll ? '' : `height: ${labelRowHeight}px;`)}
overflow: hidden;
`;
const StyledLabel = styled(Label)`
height: ${labelHeight}px;
margin: 1px 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
line-height: ${labelHeight - labelVerticalMargin}px;
`;
/**
* The inner labels container always adapts to the size of labels. Its height
* is measured by the resize observer.
*/
const LabelsInnerContainer = styled(Flex)`
position: relative;
flex-wrap: wrap;
align-items: start;
gap: ${props => props.theme.space[1]}px;
padding-right: 60px;
`;
/**
* It's important for this button to use absolute positioning; otherwise, its
* presence in the layout may itself influence the resize logic, potentially
* causing a feedback loop.
*/
const MoreLabelsButton = styled(ButtonLink)`
position: absolute;
right: 0;
height: ${labelHeight}px;
margin: ${labelVerticalMargin}px 0;
min-height: 0;
background-color: ${props => getBackgroundColor(props)};
color: ${props => props.theme.colors.text.slightlyMuted};
font-style: italic;
transition: visibility 0s;
transition: background 150ms;
`;