|
1 | 1 | import { Card, CardBody, Button, CardHeader } from '@chakra-ui/react' |
| 2 | +import { useQuery } from '@tanstack/react-query' |
2 | 3 | import { getUpstreamVisits, upstreamDataDownloadRequest } from 'loaders/general' |
| 4 | +import { getInstrumentInfo } from 'loaders/general' |
3 | 5 | import React, { useCallback, useEffect } from 'react' |
4 | 6 | import { MdFileDownload } from 'react-icons/md' |
5 | 7 |
|
6 | | -interface SessionId { |
| 8 | +const InstrumentUpstreamVisitsCard = ({ |
| 9 | + sessid, |
| 10 | + instrumentName, |
| 11 | + displayName, |
| 12 | + instrumentVisits, |
| 13 | +}: { |
7 | 14 | sessid: number |
| 15 | + instrumentName: string |
| 16 | + displayName: string |
| 17 | + instrumentVisits: Record<string, string> |
| 18 | +}) => { |
| 19 | + // Display upstream visits for a single instrument |
| 20 | + // Parameters to take: instrument name and upstream visits dict |
| 21 | + return ( |
| 22 | + <Card alignItems="left" cursor={'default'}> |
| 23 | + <CardHeader fontWeight="bold" cursor="default"> |
| 24 | + {displayName} |
| 25 | + </CardHeader> |
| 26 | + <CardBody cursor="default"> |
| 27 | + {/* Map each visit to a button */} |
| 28 | + {!!Object.keys(instrumentVisits).length ? ( |
| 29 | + Object.entries(instrumentVisits).map( |
| 30 | + ([visitName, visitPath]: [string, string]) => { |
| 31 | + return ( |
| 32 | + <Button |
| 33 | + rightIcon={<MdFileDownload />} |
| 34 | + cursor="pointer" |
| 35 | + onClick={() => |
| 36 | + upstreamDataDownloadRequest( |
| 37 | + instrumentName, |
| 38 | + sessid, |
| 39 | + visitName, |
| 40 | + visitPath |
| 41 | + ) |
| 42 | + } |
| 43 | + > |
| 44 | + {visitName} |
| 45 | + </Button> |
| 46 | + ) |
| 47 | + } |
| 48 | + ) |
| 49 | + ) : ( |
| 50 | + <>No related visits found</> |
| 51 | + )} |
| 52 | + </CardBody> |
| 53 | + </Card> |
| 54 | + ) |
8 | 55 | } |
9 | 56 |
|
10 | | -export const UpstreamVisitCard = ({ sessid }: SessionId) => { |
11 | | - const [upstreamVisits, setUpstreamVisits] = React.useState({}) |
| 57 | +export const UpstreamVisitsCard = ({ sessid }: { sessid: number }) => { |
| 58 | + const [upstreamVisits, setUpstreamVisits] = React.useState< |
| 59 | + Record<string, Record<string, string>> |
| 60 | + >({}) |
12 | 61 |
|
| 62 | + // Load all visits associated with current session |
13 | 63 | const resolveVisits = useCallback(async () => { |
14 | 64 | const visits = await getUpstreamVisits(sessid) |
| 65 | + if (!visits) return // Handle null or false-y cases |
15 | 66 | setUpstreamVisits(visits) |
16 | | - console.log(visits) |
17 | 67 | }, [sessid]) |
| 68 | + |
18 | 69 | useEffect(() => { |
19 | 70 | resolveVisits() |
20 | 71 | }, [sessid, resolveVisits]) |
21 | 72 |
|
22 | | - return upstreamVisits ? ( |
23 | | - <Card alignItems="center"> |
24 | | - <CardHeader>Upstream Visit Data Download</CardHeader> |
25 | | - {Object.keys(upstreamVisits).map((k) => { |
26 | | - return ( |
27 | | - <CardBody> |
28 | | - <Button |
29 | | - rightIcon={<MdFileDownload />} |
30 | | - onClick={() => upstreamDataDownloadRequest(k, sessid)} |
31 | | - > |
32 | | - {k} |
33 | | - </Button> |
34 | | - </CardBody> |
35 | | - ) |
36 | | - })} |
| 73 | + // Set up queryClient to load names of all instruments |
| 74 | + type InstrumentInfo = { |
| 75 | + instrument_name: string |
| 76 | + display_name: string |
| 77 | + instrument_url: string |
| 78 | + } |
| 79 | + const { data: instrumentInfo } = useQuery<InstrumentInfo[]>({ |
| 80 | + queryKey: ['instrumentInfo'], |
| 81 | + queryFn: getInstrumentInfo, |
| 82 | + staleTime: 60000, |
| 83 | + }) |
| 84 | + |
| 85 | + // Set up function to get the corresponding display name of the instrument |
| 86 | + const getDisplayName = (instrumentName: string) => { |
| 87 | + if (!instrumentInfo) return instrumentName |
| 88 | + const result = instrumentInfo.find( |
| 89 | + (item) => item.instrument_name === instrumentName |
| 90 | + ) |
| 91 | + // Use instrument name if no results were found or if display name wasn't set |
| 92 | + return result |
| 93 | + ? result.display_name |
| 94 | + ? result.display_name |
| 95 | + : instrumentName |
| 96 | + : instrumentName |
| 97 | + } |
| 98 | + |
| 99 | + return !!Object.keys(upstreamVisits).length && !!instrumentInfo ? ( |
| 100 | + <Card alignItems="left" cursor={'default'}> |
| 101 | + <CardHeader fontWeight="bold" cursor="default"> |
| 102 | + Upstream Visit Data Download |
| 103 | + </CardHeader> |
| 104 | + <CardBody cursor="default"> |
| 105 | + {/* Map each instrument to its own card */} |
| 106 | + {Object.entries(upstreamVisits).map( |
| 107 | + ([instrumentName, instrumentVisits]: [ |
| 108 | + string, |
| 109 | + Record<string, string>, |
| 110 | + ]) => { |
| 111 | + return ( |
| 112 | + <InstrumentUpstreamVisitsCard |
| 113 | + sessid={sessid} |
| 114 | + instrumentName={instrumentName} |
| 115 | + displayName={getDisplayName(instrumentName)} |
| 116 | + instrumentVisits={instrumentVisits} |
| 117 | + /> |
| 118 | + ) |
| 119 | + } |
| 120 | + )} |
| 121 | + </CardBody> |
37 | 122 | </Card> |
38 | 123 | ) : ( |
39 | 124 | <></> |
|
0 commit comments