-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatasetPageCard.tsx
More file actions
187 lines (176 loc) · 5.47 KB
/
DatasetPageCard.tsx
File metadata and controls
187 lines (176 loc) · 5.47 KB
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
import {
Box,
Button,
Card,
CardContent,
Chip,
Link,
Stack,
Typography,
Grid,
} from "@mui/material";
import { Colors } from "design/theme";
import React from "react";
import { useNavigate } from "react-router-dom";
import { Row } from "redux/neurojson/types/neurojson.interface";
import RoutesEnum from "types/routes.enum";
const formatSize = (sizeInBytes: number): string => {
if (sizeInBytes < 1024) {
return `${sizeInBytes} Bytes`;
} else if (sizeInBytes < 1024 * 1024) {
return `${(sizeInBytes / 1024).toFixed(1)} KB`;
} else if (sizeInBytes < 1024 * 1024 * 1024) {
return `${(sizeInBytes / (1024 * 1024)).toFixed(2)} MB`;
} else if (sizeInBytes < 1024 * 1024 * 1024 * 1024) {
return `${(sizeInBytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
} else {
return `${(sizeInBytes / (1024 * 1024 * 1024 * 1024)).toFixed(2)} TB`;
}
};
// for showing the size
const jsonBytes = (obj: unknown) =>
obj ? new TextEncoder().encode(JSON.stringify(obj)).length : 0;
interface DatasetPageCardProps {
doc: Row;
index: number;
dbName: string;
pageSize: number;
page: number;
}
const DatasetPageCard: React.FC<DatasetPageCardProps> = ({
doc,
index,
dbName,
page,
pageSize,
}) => {
const navigate = useNavigate();
const datasetIndex = (page - 1) * pageSize + index + 1;
const sizeInBytes = React.useMemo(() => {
const len = (doc as any)?.value?.length; // bytes from length key
if (typeof len === "number" && Number.isFinite(len)) return len;
return jsonBytes(doc.value); // fallback: summary object size
}, [doc.value]);
return (
<Grid item xs={12} sm={6} key={doc.id}>
<Card
sx={{
position: "relative",
backgroundColor: Colors.white,
boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
height: "100%",
display: "flex",
flexDirection: "column",
}}
>
<Box
sx={{
position: "absolute",
bottom: 8,
right: 12,
fontSize: "1rem",
fontWeight: "bold",
color: Colors.darkPurple,
}}
>
#{datasetIndex}
</Box>
<CardContent sx={{ flex: 1 }}>
<Button
onClick={() =>
navigate(
`${RoutesEnum.DATABASES}/${encodeURIComponent(
dbName ?? ""
)}/${encodeURIComponent(doc.id ?? "")}`
)
}
sx={{
fontSize: "1.25rem",
margin: 0,
color: Colors.darkPurple,
textTransform: "none",
justifyContent: "flex-start",
width: "100%",
textAlign: "left",
"&:hover": {
textDecoration: "underline",
color: Colors.purple,
backgroundColor: "transparent",
transform: "scale(1.01)",
},
}}
>
{doc.value.name || "Untitled"}
</Button>
<Typography
color={Colors.textSecondary}
variant="body2"
sx={{ mb: 2, marginLeft: 1 }}
>
ID: {doc.id}
</Typography>
<Stack spacing={2} margin={1}>
<Stack direction="row" spacing={1} flexWrap="wrap" gap={1}>
{doc.value.subj && (
<Chip
label={`${doc.value.subj.length} subjects`}
size="small"
sx={{
backgroundColor: Colors.darkOrange,
color: Colors.white,
}}
/>
)}
{doc.value.modality &&
doc.value.modality.map((mod: string) => (
<Chip
key={mod}
label={mod}
size="small"
sx={{
backgroundColor: Colors.purple,
color: Colors.white,
}}
/>
))}
</Stack>
<Typography variant="body2" color={Colors.textSecondary}>
<strong>Summary:</strong>{" "}
{doc.value.readme || "No description available"}
</Typography>
<Typography variant="body2" color={Colors.textPrimary}>
<strong>Authors:</strong>{" "}
{Array.isArray(doc.value.info?.Authors)
? doc.value.info.Authors.join(", ")
: doc.value.info?.Authors || "Unknown"}
</Typography>
<Stack direction="row" spacing={2} alignItems="center">
<Typography variant="body2" color={Colors.textPrimary}>
<strong>Size:</strong>{" "}
{/* {doc.value.length
? `${(doc.value.length / 1024 / 1024).toFixed(2)} MB`
: "Unknown"} */}
{formatSize(sizeInBytes)}
</Typography>
{doc.value.info?.DatasetDOI && (
<Link
href={doc.value.info.DatasetDOI}
target="_blank"
rel="noopener"
>
<Chip
label="DOI"
size="small"
clickable
sx={{ backgroundColor: Colors.accent, color: Colors.white }}
/>
</Link>
)}
</Stack>
</Stack>
</CardContent>
</Card>
</Grid>
);
};
export default DatasetPageCard;