-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrialTable.tsx
486 lines (462 loc) · 15.4 KB
/
TrialTable.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import React from "react";
import {
Grid,
Card,
Typography,
CardContent,
Button,
ButtonProps,
Box,
Table,
TableBody,
TableRow,
TableCell,
TableHead,
Divider,
withStyles,
Link
} from "@material-ui/core";
import { apiFetch, IApiPage } from "../../../api/api";
import { IFileBundle, Trial } from "../../../model/trial";
import { withIdToken } from "../../identity/AuthProvider";
import { flatMap, isEmpty, omitBy, pickBy, range } from "lodash";
import { CloudDownload } from "@material-ui/icons";
import BatchDownloadDialog from "../shared/BatchDownloadDialog";
import { Skeleton } from "@material-ui/lab";
import { useFilterFacets } from "../shared/FilterProvider";
import { useSWRInfinite } from "swr";
import { formatQueryString, naivePluralize } from "../../../util/utils";
import { useUserContext } from "../../identity/UserProvider";
const BatchDownloadButton: React.FC<{
ids: number[];
token: string;
} & ButtonProps> = ({ ids, token, children, disabled }) => {
const [open, setOpen] = React.useState<boolean>(false);
return (
<>
<Button
fullWidth
size="small"
variant="outlined"
startIcon={<CloudDownload />}
disabled={!ids.length || disabled}
onClick={() => {
setOpen(true);
}}
children={children}
/>
<BatchDownloadDialog
token={token}
ids={ids}
open={open}
onClose={() => setOpen(false)}
/>
</>
);
};
const TrialPlaceholder: React.FC = () => {
return (
<Card data-testid="placeholder-card">
<CardContent>
<Grid container direction="column" spacing={2}>
<Grid item>
<Grid
container
direction="row"
justify="space-between"
alignItems="stretch"
>
<Grid item xs={4}>
<Skeleton />
<Skeleton />
</Grid>
<Grid item xs={2}>
<Skeleton height={40} />
</Grid>
</Grid>
</Grid>
<Grid item>
<Skeleton />
<Skeleton />
<Skeleton />
</Grid>
</Grid>
<Skeleton />
</CardContent>
</Card>
);
};
interface ITrialCardProps {
trial: Trial;
token: string;
}
const AssayButton: React.FC<{
purpose: keyof IFileBundle[keyof IFileBundle];
bundle: IFileBundle[keyof IFileBundle];
token: string;
}> = ({ purpose, bundle, token }) => {
const user = useUserContext();
const ids = bundle[purpose] || [];
return (
<Box m={1}>
<BatchDownloadButton
token={token}
ids={ids}
disabled={!user.canDownload}
>
{ids.length} {naivePluralize("file", ids.length)}
</BatchDownloadButton>
</Box>
);
};
const AssayTableCell = withStyles({
root: {
borderBottom: "none"
}
})(TableCell);
const AssayButtonTable: React.FC<{
token: string;
assayBundle: IFileBundle;
}> = ({ token, assayBundle }) => {
return (
<Table size="small" padding="none">
<TableHead>
<TableRow>
<AssayTableCell align="left">
<Typography variant="overline" color="textSecondary">
Assay
</Typography>
</AssayTableCell>
<AssayTableCell align="center">
<Typography variant="overline" color="textSecondary">
Source Files
</Typography>
</AssayTableCell>
<AssayTableCell align="center">
<Typography variant="overline" color="textSecondary">
Analysis Files
</Typography>
</AssayTableCell>
</TableRow>
</TableHead>
<TableBody>
{Object.entries(assayBundle).map(([assay, bundle]) => {
if (assay === "other") {
return null;
}
return (
<TableRow key={assay}>
<AssayTableCell>{assay}</AssayTableCell>
<AssayTableCell>
<AssayButton
token={token}
purpose={"source"}
bundle={bundle}
/>
</AssayTableCell>
<AssayTableCell>
<AssayButton
token={token}
purpose={"analysis"}
bundle={bundle}
/>
</AssayTableCell>
</TableRow>
);
})}
</TableBody>
</Table>
);
};
const LabelAndValue: React.FC<{
label: string;
value: string | React.ReactElement;
}> = ({ label, value }) => {
return (
<Grid container alignItems="center">
<Grid item>
<Box paddingRight={1}>
<Typography noWrap variant="overline" color="textSecondary">
{label}
</Typography>
</Box>
</Grid>
<Grid item>
<Typography variant="subtitle2">{value}</Typography>
</Grid>
</Grid>
);
};
const extractFileIds = (fileBundle: IFileBundle, keys: string[]) =>
flatMap(
pickBy(fileBundle, (_, key) => keys.includes(key)),
bundle => flatMap(bundle, v => v || [])
);
const partSampFields = ["Participants Info", "Samples Info"];
const clinicalFields = ["Clinical Data"];
export const TrialCard: React.FC<ITrialCardProps> = ({ trial, token }) => {
const user = useUserContext();
const {
trial_name,
nct_id,
trial_status,
biobank,
lead_cimac_pis
} = trial.metadata_json;
const fileBundle = trial.file_bundle || {};
const partSampIds = extractFileIds(fileBundle, partSampFields);
const clinicalIds = extractFileIds(fileBundle, clinicalFields);
const assayBundle = omitBy(
fileBundle,
(v, k) =>
partSampFields.includes(k) ||
clinicalFields.includes(k) ||
k === "other"
);
const rightPanel =
isEmpty(assayBundle) &&
partSampIds.length === 0 &&
clinicalIds.length === 0 ? (
<Grid
container
justify="center"
alignItems="center"
style={{ height: "100%" }}
>
<Grid item>
<Typography color="textSecondary" variant="subtitle2">
No files have been uploaded yet.
</Typography>
</Grid>
</Grid>
) : (
<>
<Typography variant="overline" color="textSecondary">
Sample-Tracking Data
</Typography>
<BatchDownloadButton
token={token}
ids={partSampIds}
disabled={!user.canDownload}
>
{partSampIds.length} participant/sample{" "}
{naivePluralize("file", partSampIds.length)}
</BatchDownloadButton>
<Typography variant="overline" color="textSecondary">
Clinical Data
</Typography>
<BatchDownloadButton
token={token}
ids={clinicalIds}
disabled={!user.canDownload}
>
{clinicalIds.length} clinical data{" "}
{naivePluralize("file", clinicalIds.length)}
</BatchDownloadButton>
{!isEmpty(assayBundle) && (
<Box paddingTop={1}>
<AssayButtonTable
token={token}
assayBundle={assayBundle}
/>
</Box>
)}
</>
);
const leftPanel = (
<>
<Typography variant="h6">
<strong>{trial.trial_id}</strong>{" "}
</Typography>
<Grid container spacing={1}>
{[
[
"nct number",
<Link
href={`https://clinicaltrials.gov/ct2/show/${nct_id}`}
target="_blank"
rel="noopener noreferrer"
>
{nct_id}
</Link>
],
[
"cimac investigator(s)",
lead_cimac_pis ? lead_cimac_pis.join(", ") : undefined,
12
],
["biobank", biobank, 12],
["status", trial_status],
[
"participants",
trial.num_participants === undefined
? "-"
: trial.num_participants
],
[
"samples",
trial.num_samples === undefined
? "-"
: trial.num_samples
],
[
"assays",
assayBundle ? Object.keys(assayBundle).length : 0
],
["overview", trial_name, 12]
]
.filter(config => config[1] !== undefined)
.map(([label, value, width]) => (
<Grid item key={label} xs={width}>
<LabelAndValue label={label} value={value} />
</Grid>
))}
</Grid>
</>
);
return (
<Card>
<CardContent>
<Grid
container
direction="row"
justify="space-between"
wrap="nowrap"
>
<Grid item xs={6}>
{leftPanel}
</Grid>
<Grid item>
<Divider orientation="vertical" />
</Grid>
<Grid item xs={5}>
{rightPanel}
</Grid>
</Grid>
</CardContent>
</Card>
);
};
const TRIALS_PER_PAGE = 10;
export const usePaginatedTrials = (token: string) => {
const { filters } = useFilterFacets();
const getTrialURL = (
pageIndex: number,
previousPageData: IApiPage<Trial> | null
) => {
if (previousPageData && !previousPageData._items.length) {
return null;
}
return [
`/trial_metadata?${formatQueryString({
include_file_bundles: true,
include_counts: true,
page_size: TRIALS_PER_PAGE,
page_num: pageIndex,
trial_ids: filters.trial_ids
? encodeURIComponent(filters.trial_ids.join(","))
: undefined
})}`,
token
];
};
const { data, isValidating, setSize } = useSWRInfinite<IApiPage<Trial>>(
getTrialURL,
apiFetch // provide this explicitly because that makes mocking in tests easier
);
const trials = data?.flatMap(page => page._items);
const allLoaded =
trials &&
(trials.length === 0 || trials.length % TRIALS_PER_PAGE !== 0);
const loadMore = React.useCallback(() => {
setSize(size => size + 1);
}, [setSize]);
// Clear all results when filters change if data has already loaded
const hasTrials = !!trials;
React.useEffect(() => {
if (hasTrials) {
setSize(1);
}
}, [filters.trial_ids, setSize, hasTrials]);
return {
trials,
allLoaded,
isLoading: isValidating,
loadMore
};
};
export interface ITrialTableProps {
viewToggleButton: React.ReactElement;
}
const TrialTable: React.FC<ITrialTableProps & {
token: string;
}> = ({ token, viewToggleButton }) => {
const { trials, loadMore, isLoading, allLoaded } = usePaginatedTrials(
token
);
return (
<Grid container direction="column" spacing={1}>
<Grid item>
<Grid
container
justify="space-between"
alignItems="center"
spacing={1}
>
<Grid item>
<Box margin={1}>
<Typography color="textSecondary" variant="caption">
Browse trial overviews and download batches of
data
</Typography>
</Box>
</Grid>
<Grid item>{viewToggleButton}</Grid>
</Grid>
</Grid>
{trials ? (
trials.map(t => {
return (
<Grid item key={t.trial_id}>
<TrialCard trial={t} token={token} />
</Grid>
);
})
) : (
<>
{range(5).map(i => (
<Grid key={i} item>
<TrialPlaceholder />
</Grid>
))}
</>
)}
{trials &&
(allLoaded ? (
<Grid item>
<Box textAlign="center">
<Typography
variant="subtitle2"
color="textSecondary"
>
Loaded all results.
</Typography>
</Box>
</Grid>
) : (
<Grid item>
<Button
fullWidth
variant="outlined"
color="primary"
disabled={isLoading}
onClick={() => loadMore()}
>
load more trials
</Button>
</Grid>
))}
</Grid>
);
};
export default withIdToken<ITrialTableProps>(TrialTable);