-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathMetadataSection.tsx
200 lines (188 loc) · 5.1 KB
/
MetadataSection.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
import {
Box,
createStyles,
IconButton,
Link,
makeStyles,
Paper,
Tooltip,
Typography,
} from "@material-ui/core";
import copy from "copy-to-clipboard";
import React, { useState } from "react";
import { RiFileCopyLine } from "react-icons/ri";
import { Link as RouterLink } from "react-router-dom";
import { HelpInfo } from "../Tooltip";
export type StringOnlyMetadataContent = {
readonly value: string;
};
type LinkableMetadataContent = StringOnlyMetadataContent & {
readonly link: string;
};
type CopyableMetadataContent = StringOnlyMetadataContent & {
/**
* The "copyable value" may be different from "value"
* in case we want to render a more readable text.
*/
readonly copyableValue: string;
};
export type Metadata = {
readonly label: string;
readonly labelTooltip?: string | JSX.Element;
// If content is undefined, we display "-" as the placeholder.
readonly content?:
| StringOnlyMetadataContent
| LinkableMetadataContent
| CopyableMetadataContent
| JSX.Element;
/**
* This flag will determine this metadata field will show in the UI.
* Defaults to true.
*/
readonly isAvailable?: boolean;
};
const useStyles = makeStyles((theme) =>
createStyles({
root: {
display: "grid",
gridTemplateColumns: "repeat(3, minmax(0, 1fr))",
rowGap: theme.spacing(1),
columnGap: theme.spacing(4),
padding: theme.spacing(2),
},
label: {
color: theme.palette.text.secondary,
},
labelTooltip: {
marginLeft: theme.spacing(0.5),
},
contentContainer: {
display: "flex",
alignItems: "center",
},
content: {
display: "block",
textOverflow: "ellipsis",
overflow: "hidden",
whiteSpace: "nowrap",
},
button: {
color: "black",
marginLeft: theme.spacing(0.5),
},
}),
);
/**
* We style the metadata content based on the type supplied.
*
* A default style will be applied if content is MetadataContent type.
* If content is undefined, we display "-" as the placeholder.
*/
export const MetadataContentField: React.FC<{
content: Metadata["content"];
}> = ({ content }) => {
const classes = useStyles();
const [copyIconClicked, setCopyIconClicked] = useState<boolean>(false);
if (content === undefined || "value" in content) {
return content === undefined || !("link" in content) ? (
<div className={classes.contentContainer}>
<Typography
className={classes.content}
variant="body2"
title={content?.value}
>
{content?.value ?? "-"}
</Typography>
{content && "copyableValue" in content && (
<Tooltip
placement="top"
title={copyIconClicked ? "Copied" : "Click to copy"}
>
<IconButton
aria-label="copy"
onClick={() => {
setCopyIconClicked(true);
copy(content.copyableValue);
}}
// Set up mouse events to avoid text changing while tooltip is visible
onMouseEnter={() => setCopyIconClicked(false)}
onMouseLeave={() =>
setTimeout(() => setCopyIconClicked(false), 333)
}
size="small"
className={classes.button}
>
<RiFileCopyLine />
</IconButton>
</Tooltip>
)}
</div>
) : content.link.startsWith("http") ? (
<Link className={classes.content} href={content.link}>
{content.value}
</Link>
) : (
<Link
className={classes.content}
component={RouterLink}
to={content.link}
>
{content.value}
</Link>
);
}
return content;
};
/**
* Renders the metadata list in a column format.
*/
const MetadataList: React.FC<{
metadataList: Metadata[];
}> = ({ metadataList }) => {
const classes = useStyles();
const filteredMetadataList = metadataList.filter(
({ isAvailable }) => isAvailable ?? true,
);
return (
<Box className={classes.root}>
{filteredMetadataList.map(({ label, labelTooltip, content }, idx) => (
<Box key={idx} flex={1} paddingTop={0.5} paddingBottom={0.5}>
<Box display="flex" alignItems="center" marginBottom={0.5}>
<Typography className={classes.label} variant="body2">
{label}
</Typography>
{labelTooltip && (
<HelpInfo className={classes.labelTooltip}>
{labelTooltip}
</HelpInfo>
)}
</Box>
<MetadataContentField content={content} />
</Box>
))}
</Box>
);
};
/**
* Renders the Metadata UI with the header and metadata in a 3-column format.
*/
export const MetadataSection = ({
header,
metadataList,
}: {
header?: string;
metadataList: Metadata[];
}) => {
return (
<Box marginTop={1} marginBottom={4}>
{header && (
<Box paddingBottom={2}>
<Typography variant="h2">{header}</Typography>
</Box>
)}
<Paper variant="outlined">
<MetadataList metadataList={metadataList} />
</Paper>
</Box>
);
};