|
1 | | -import { Card, CardProps, Chip } from '@pluralsh/design-system' |
| 1 | +import { CardProps, Chip, ChipList } from '@pluralsh/design-system' |
2 | 2 | import { Div, Flex } from 'honorable' |
3 | 3 | import { ReactNode } from 'react' |
4 | | -import type { LabelPair, Metadata as MetadataT } from 'generated/graphql' |
| 4 | +import type { LabelPair, Maybe, Metadata as MetadataT } from 'generated/graphql' |
| 5 | +import { CARD_CONTENT_MAX_WIDTH, MetadataCard } from 'components/utils/Metadata' |
| 6 | +import { useTheme } from 'styled-components' |
5 | 7 |
|
6 | 8 | export const mapify = tags => tags.reduce((acc, { name, value }) => ({ ...acc, [name]: value }), {}) |
7 | 9 |
|
@@ -34,45 +36,66 @@ export function LabelsAnnotationsRow({ |
34 | 36 |
|
35 | 37 | export function LabelsAnnotationsTag({ name, value }: LabelPair) { |
36 | 38 | return ( |
37 | | - <Chip> |
| 39 | + <Chip size="small"> |
38 | 40 | {name} |
39 | 41 | {value && `: ${value}`} |
40 | 42 | </Chip> |
41 | 43 | ) |
42 | 44 | } |
43 | 45 |
|
| 46 | +function renderLabel(label: Maybe<LabelPair>) { |
| 47 | + return ( |
| 48 | + <> |
| 49 | + {label?.name} |
| 50 | + {label?.value && `: ${label.value}`} |
| 51 | + </> |
| 52 | + ) |
| 53 | +} |
| 54 | + |
44 | 55 | export function LabelsAnnotations({ |
45 | 56 | metadata: { labels, annotations }, |
46 | 57 | ...props |
47 | 58 | }: { |
48 | 59 | metadata: MetadataT |
49 | 60 | } & CardProps) { |
| 61 | + const theme = useTheme() |
| 62 | + |
| 63 | + const hasLabels = labels && labels?.length > 0 |
| 64 | + const hasAnnotations = annotations && annotations.length > 0 |
| 65 | + const hasData = hasLabels || hasAnnotations |
| 66 | + |
| 67 | + if (!hasData) { |
| 68 | + return null |
| 69 | + } |
| 70 | + |
50 | 71 | return ( |
51 | | - <Card |
52 | | - padding="xlarge" |
53 | | - {...props} |
54 | | - > |
| 72 | + <MetadataCard {...props}> |
55 | 73 | <Flex |
56 | 74 | direction="column" |
57 | 75 | gap="xlarge" |
| 76 | + maxWidth={(CARD_CONTENT_MAX_WIDTH - theme.spacing.xlarge * 3) / 2} |
58 | 77 | > |
59 | | - <LabelsAnnotationsRow name="Labels"> |
60 | | - {labels?.map(label => ( |
61 | | - <LabelsAnnotationsTag |
62 | | - key={label?.name} |
63 | | - {...label} |
| 78 | + {hasLabels && ( |
| 79 | + <LabelsAnnotationsRow name="Labels"> |
| 80 | + <ChipList |
| 81 | + size="small" |
| 82 | + limit={8} |
| 83 | + values={labels} |
| 84 | + transformValue={renderLabel} |
64 | 85 | /> |
65 | | - ))} |
66 | | - </LabelsAnnotationsRow> |
67 | | - <LabelsAnnotationsRow name="Annotations"> |
68 | | - {annotations?.map(annotation => ( |
69 | | - <LabelsAnnotationsTag |
70 | | - key={annotation?.name} |
71 | | - {...annotation} |
| 86 | + </LabelsAnnotationsRow> |
| 87 | + )} |
| 88 | + {hasAnnotations && ( |
| 89 | + <LabelsAnnotationsRow name="Annotations"> |
| 90 | + <ChipList |
| 91 | + size="small" |
| 92 | + limit={8} |
| 93 | + values={annotations} |
| 94 | + transformValue={renderLabel} |
72 | 95 | /> |
73 | | - ))} |
74 | | - </LabelsAnnotationsRow> |
| 96 | + </LabelsAnnotationsRow> |
| 97 | + )} |
75 | 98 | </Flex> |
76 | | - </Card> |
| 99 | + </MetadataCard> |
77 | 100 | ) |
78 | 101 | } |
0 commit comments