-
Notifications
You must be signed in to change notification settings - Fork 3
/
DetailCard.tsx
179 lines (166 loc) · 4.54 KB
/
DetailCard.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
import { Box, Card, Stack } from "@mui/material";
import { DetailCardHeaderProps, DetailCardProps } from "./DetailCard.types";
import { FileDetails, Infographic } from "../../CardContent";
import React, { Fragment, useEffect, useRef, useState } from "react";
import LabelChipGroup from "../../LabelSelector/LabelChipGroup/LabelChipGroup";
import type { LabelChipGroupProps } from "../../LabelSelector/LabelChipGroup/LabelChipGroup.types";
import NoWrapTypography from "../../NoWrapTypography/NoWrapTypography";
import { ResizeObserver } from "@juggle/resize-observer";
// TODO: add tests in browser once we are done with the migration to cypress. The old tests live in a txt file in this folder until then.
function DetailCard({
buttonsStack = null,
content = null,
files = [],
downloadButtonText,
downloadButtonTextOnSearch,
fileTitle = "title",
labels = [],
media = "",
onClickDownload,
onClickFile,
subtitle = "subtitle",
title = "title",
width = 1150
}: DetailCardProps) {
// render the detail card
return (
<Stack
className="detail-card-container"
mt={1}
mb={3}
sx={{
display: "flex",
flexDirection: "column",
flexGrow: 1,
height: "100%",
width
}}
>
<DetailCardHeader
title={title}
subtitle={subtitle}
buttonsStack={buttonsStack}
labels={labels}
width={width}
/>
<Box
sx={{
display: "flex",
flexDirection: "row",
height: "100%",
my: 2,
overflow: "auto"
}}
>
<Box ml={0.5}>
<Card>
<Infographic media={media} />
<FileDetails
files={files}
downloadButtonText={downloadButtonText}
downloadButtonTextOnSearch={downloadButtonTextOnSearch}
fileTitle={fileTitle}
onClickDownload={onClickDownload}
onClickFile={onClickFile}
/>
</Card>
</Box>
<Stack
ml={2}
spacing={2}
sx={{ display: "flex", flexGrow: 1, mr: 0.5, width: "100%" }}
>
{content}
</Stack>
</Box>
</Stack>
);
}
function DetailCardHeader({
width,
title,
subtitle,
buttonsStack,
labels
}: DetailCardHeaderProps) {
// convert the labels to chips
const labelChips: LabelChipGroupProps["chips"] = labels
? labels.map(label => {
return {
clickable: false,
color: label.color,
label: label.name,
size: "small"
};
})
: [];
// buttonStack ref
const buttonStackRef = useRef<HTMLDivElement>(null);
// get the width of the button stack
const useButtonStackwidth = (
buttonStackRef: React.RefObject<HTMLDivElement>
) => {
const [buttonStackWidth, setButtonStackWidth] = useState(0);
useEffect(() => {
const sizeObserver = new ResizeObserver((entries, observer) => {
entries.forEach(({ target }) => {
setButtonStackWidth(target.clientWidth);
});
});
if (buttonStackRef.current) {
sizeObserver.observe(buttonStackRef.current);
}
return () => sizeObserver.disconnect();
}, [buttonStackRef]);
return [buttonStackWidth];
};
// get the width of the button stack
const [buttonsStackWidth] = useButtonStackwidth(buttonStackRef);
// header content width
const headerContentWidth = width - buttonsStackWidth - 10;
return (
<Fragment>
<Box
m={1}
sx={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between"
}}
>
<Box sx={{ width: headerContentWidth }}>
<NoWrapTypography
sx={{
color: theme =>
theme.palette.mode === "dark" ? "white" : "black",
fontSize: 20,
fontWeight: 700
}}
>
{title}
</NoWrapTypography>
<NoWrapTypography
sx={{
color: theme => theme.palette.text.secondary,
fontSize: 14,
fontWeight: 400
}}
>
{subtitle}
</NoWrapTypography>
</Box>
<Box
sx={{ display: "flex", flexDirection: "row", gap: 2 }}
ref={buttonStackRef}
>
{buttonsStack}
</Box>
</Box>
<Box mx={1}>
{labels && labels.length > 0 && <LabelChipGroup chips={labelChips} />}
</Box>
</Fragment>
);
}
// export the detail card
export default DetailCard;