Skip to content

Commit a15914d

Browse files
committed
review fix
1 parent b2046ae commit a15914d

File tree

4 files changed

+123
-43
lines changed

4 files changed

+123
-43
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as React from 'react'
2+
import { useTranslation } from 'react-i18next'
3+
4+
import {
5+
ALIGN_CENTER,
6+
Flex,
7+
Icon,
8+
LegacyStyledText,
9+
Link,
10+
SPACING,
11+
TYPOGRAPHY,
12+
} from '@opentrons/components'
13+
import { useCsvFileRawQuery } from '@opentrons/react-api-client'
14+
import { downloadFile } from './utils'
15+
16+
interface DownloadCsvFileLinkProps {
17+
fileId: string
18+
fileName: string
19+
}
20+
export function DownloadCsvFileLink(
21+
props: DownloadCsvFileLinkProps
22+
): JSX.Element {
23+
const { fileId, fileName } = props
24+
const { t } = useTranslation('run_details')
25+
const { data: csvFileRaw } = useCsvFileRawQuery(fileId)
26+
27+
return (
28+
<Link
29+
role="button"
30+
css={
31+
csvFileRaw == null
32+
? TYPOGRAPHY.darkLinkLabelSemiBoldDisabled
33+
: TYPOGRAPHY.linkPSemiBold
34+
}
35+
onClick={() => {
36+
if (csvFileRaw != null) {
37+
downloadFile(csvFileRaw, fileName)
38+
}
39+
}}
40+
>
41+
<Flex alignItems={ALIGN_CENTER} gridGap={SPACING.spacing4}>
42+
<LegacyStyledText as="p">{t('download')}</LegacyStyledText>
43+
<Icon name="download" size="1rem" />
44+
</Flex>
45+
</Link>
46+
)
47+
}

app/src/organisms/Devices/HistoricalProtocolRunDrawer.tsx

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import {
1111
COLORS,
1212
DIRECTION_COLUMN,
1313
Flex,
14-
Icon,
1514
InfoScreen,
1615
JUSTIFY_FLEX_START,
1716
LegacyStyledText,
18-
Link,
1917
LocationIcon,
2018
OVERFLOW_HIDDEN,
2119
SPACING,
@@ -27,11 +25,8 @@ import {
2725
getLoadedLabwareDefinitionsByUri,
2826
getModuleDisplayName,
2927
} from '@opentrons/shared-data'
30-
import {
31-
useAllCsvFilesQuery,
32-
useCsvFileRawQuery,
33-
} from '@opentrons/react-api-client'
34-
import { downloadFile } from './utils'
28+
import { useAllCsvFilesQuery } from '@opentrons/react-api-client'
29+
import { DownloadCsvFileLink } from './DownloadCsvFileLink'
3530
import { useFeatureFlag } from '../../redux/config'
3631
import { Banner } from '../../atoms/Banner'
3732
import { useMostRecentCompletedAnalysis } from '../LabwarePositionCheck/useMostRecentCompletedAnalysis'
@@ -101,7 +96,7 @@ export function HistoricalProtocolRunDrawer(
10196
) : null
10297

10398
const protocolFilesData =
104-
allProtocolDataFiles.length === 0 ? (
99+
allProtocolDataFiles.length === 1 ? (
105100
<InfoScreen contentType="noFiles" t={t} backgroundColor={COLORS.grey35} />
106101
) : (
107102
<Flex flexDirection={DIRECTION_COLUMN} gridGap={SPACING.spacing4}>
@@ -173,7 +168,7 @@ export function HistoricalProtocolRunDrawer(
173168
</LegacyStyledText>
174169
</Box>
175170
<Box width="34%">
176-
<DownloadLink fileId={fileId} fileName={fileName} />
171+
<DownloadCsvFileLink fileId={fileId} fileName={fileName} />
177172
</Box>
178173
</Flex>
179174
)
@@ -300,34 +295,3 @@ export function HistoricalProtocolRunDrawer(
300295
</Flex>
301296
)
302297
}
303-
304-
interface DownloadLinkProps {
305-
fileId: string
306-
fileName: string
307-
}
308-
function DownloadLink(props: DownloadLinkProps): JSX.Element {
309-
const { fileId, fileName } = props
310-
const { t } = useTranslation('run_details')
311-
const { data: csvFileRaw } = useCsvFileRawQuery(fileId)
312-
313-
return (
314-
<Link
315-
role="button"
316-
css={
317-
csvFileRaw == null
318-
? TYPOGRAPHY.darkLinkLabelSemiBoldDisabled
319-
: TYPOGRAPHY.linkPSemiBold
320-
}
321-
onClick={() => {
322-
if (csvFileRaw != null) {
323-
downloadFile(csvFileRaw, fileName)
324-
}
325-
}}
326-
>
327-
<Flex alignItems={ALIGN_CENTER}>
328-
<LegacyStyledText as="p">{t('download')}</LegacyStyledText>
329-
<Icon name="download" size="1rem" marginLeft="0.4375rem" />
330-
</Flex>
331-
</Link>
332-
)
333-
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as React from 'react'
2+
import { describe, it, expect, beforeEach, vi } from 'vitest'
3+
import { QueryClient, QueryClientProvider } from 'react-query'
4+
import { renderHook, waitFor } from '@testing-library/react'
5+
import { getCsvFileRaw } from '@opentrons/api-client'
6+
import { useHost } from '../../api'
7+
import { useCsvFileRawQuery } from '..'
8+
9+
import type {
10+
HostConfig,
11+
Response,
12+
Protocol,
13+
DownloadedCsvFileResponse,
14+
} from '@opentrons/api-client'
15+
16+
vi.mock('@opentrons/api-client')
17+
vi.mock('../../api/useHost')
18+
19+
const HOST_CONFIG: HostConfig = { hostname: 'localhost' }
20+
const FILE_ID = 'file123'
21+
const FILE_CONTENT_RESPONSE = 'content,of,my,csv\nfile,' as DownloadedCsvFileResponse
22+
23+
describe('useCsvFileRawQuery hook', () => {
24+
let wrapper: React.FunctionComponent<{ children: React.ReactNode }>
25+
26+
beforeEach(() => {
27+
const queryClient = new QueryClient()
28+
const clientProvider: React.FunctionComponent<{
29+
children: React.ReactNode
30+
}> = ({ children }) => (
31+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
32+
)
33+
34+
wrapper = clientProvider
35+
})
36+
37+
it('should return no data if no host', () => {
38+
vi.mocked(useHost).mockReturnValue(null)
39+
40+
const { result } = renderHook(() => useCsvFileRawQuery(FILE_ID), {
41+
wrapper,
42+
})
43+
44+
expect(result.current.data).toBeUndefined()
45+
})
46+
47+
it('should return no data if the get file request fails', () => {
48+
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
49+
vi.mocked(getCsvFileRaw).mockRejectedValue('oh no')
50+
51+
const { result } = renderHook(() => useCsvFileRawQuery(FILE_ID), {
52+
wrapper,
53+
})
54+
expect(result.current.data).toBeUndefined()
55+
})
56+
57+
it('should return file data if successful request', async () => {
58+
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
59+
vi.mocked(getCsvFileRaw).mockResolvedValue({
60+
data: FILE_CONTENT_RESPONSE,
61+
} as Response<DownloadedCsvFileResponse>)
62+
63+
const { result } = renderHook(() => useCsvFileRawQuery(FILE_ID), {
64+
wrapper,
65+
})
66+
67+
await waitFor(() => {
68+
expect(result.current.data).toEqual(FILE_CONTENT_RESPONSE)
69+
})
70+
})
71+
})

react-api-client/src/dataFiles/useCsvFileRawQuery.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ export function useCsvFileRawQuery(
2121
const query = useQuery<DownloadedCsvFileResponse>(
2222
[host, `/dataFiles/${fileId}/download`],
2323
() =>
24-
getCsvFileRaw(host as HostConfig, fileId as string).then(
25-
response => response.data
26-
),
24+
getCsvFileRaw(host as HostConfig, fileId).then(response => response.data),
2725
allOptions
2826
)
2927
return query

0 commit comments

Comments
 (0)