Skip to content

Commit f0cefdf

Browse files
authored
Merge pull request #213 from devtron-labs/feat/use-download-global
feat: use useDownload globally
2 parents 939eb49 + 8f4a726 commit f0cefdf

File tree

9 files changed

+34
-35
lines changed

9 files changed

+34
-35
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.1.9-beta-1",
3+
"version": "0.1.9-beta-2",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ export const EXCLUDED_FALSY_VALUES = [undefined, null, '', NaN] as const
477477

478478
export const API_STATUS_CODES = {
479479
OK: 200,
480+
NO_CONTENT: 204,
480481
BAD_REQUEST: 400,
481482
UNAUTHORIZED: 401,
482483
PERMISSION_DENIED: 403,

src/Shared/Components/CICDHistory/Artifacts.tsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
import { useEffect, useState } from 'react'
1818
import { useParams } from 'react-router'
19+
import { useDownload } from '@Shared/Hooks'
1920
import {
20-
showError,
2121
GenericEmptyState,
2222
ImageTagsContainer,
2323
ClipboardButton,
@@ -122,7 +122,7 @@ const Artifacts = ({
122122
artifact,
123123
blobStorageEnabled,
124124
isArtifactUploaded,
125-
getArtifactPromise,
125+
downloadArtifactUrl,
126126
ciPipelineId,
127127
artifactId,
128128
isJobView,
@@ -137,6 +137,7 @@ const Artifacts = ({
137137
renderCIListHeader,
138138
}: ArtifactType) => {
139139
const { isSuperAdmin } = useSuperAdmin()
140+
const { handleDownload } = useDownload()
140141

141142
const { triggerId, buildId } = useParams<{
142143
triggerId: string
@@ -152,17 +153,10 @@ const Artifacts = ({
152153
}, [copied])
153154

154155
async function handleArtifact() {
155-
// TODO: Use useDownload() Hook instead to download file/folder
156-
try {
157-
const response = await getArtifactPromise()
158-
const b = await (response as any).blob()
159-
const a = document.createElement('a')
160-
a.href = URL.createObjectURL(b)
161-
a.download = `${buildId || triggerId}.zip`
162-
a.click()
163-
} catch (err) {
164-
showError(err)
165-
}
156+
await handleDownload({
157+
downloadUrl: downloadArtifactUrl,
158+
fileName: `${buildId || triggerId}.zip`,
159+
})
166160
}
167161

168162
if (status.toLowerCase() === TERMINAL_STATUS_MAP.RUNNING || status.toLowerCase() === TERMINAL_STATUS_MAP.STARTING) {
@@ -252,7 +246,7 @@ const Artifacts = ({
252246
</CIListItem>
253247
)}
254248
{blobStorageEnabled &&
255-
getArtifactPromise &&
249+
downloadArtifactUrl &&
256250
(type === HistoryComponentType.CD || isArtifactUploaded || isJobView) && (
257251
<CIListItem
258252
type="report"

src/Shared/Components/CICDHistory/TriggerOutput.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import {
6363
VirtualHistoryArtifactProps,
6464
RunSourceType,
6565
} from './types'
66-
import { getTagDetails, getTriggerDetails, cancelCiTrigger, cancelPrePostCdTrigger, getCDBuildReport } from './service'
66+
import { getTagDetails, getTriggerDetails, cancelCiTrigger, cancelPrePostCdTrigger } from './service'
6767
import { DEFAULT_ENV, TIMEOUT_VALUE, WORKER_POD_BASE_URL } from './constants'
6868
import { GitTriggers } from '../../types'
6969
import warn from '../../../Assets/Icon/ic-warning.svg'
@@ -561,6 +561,8 @@ const HistoryLogs: React.FC<{
561561
workflowId: triggerDetails.id,
562562
}
563563

564+
const CDBuildReportUrl = `app/cd-pipeline/workflow/download/${appId}/${envId}/${pipelineId}/${triggerId}`
565+
564566
const [ref, scrollToTop, scrollToBottom] = useScrollable({
565567
autoBottomScroll: triggerDetails.status.toLowerCase() !== 'succeeded',
566568
})
@@ -665,7 +667,7 @@ const HistoryLogs: React.FC<{
665667
tagsEditable={tagsEditable}
666668
appReleaseTagNames={appReleaseTags}
667669
hideImageTaggingHardDelete={hideImageTaggingHardDelete}
668-
getArtifactPromise={() => getCDBuildReport(appId, envId, pipelineId, triggerId)}
670+
downloadArtifactUrl={CDBuildReportUrl}
669671
type={HistoryComponentType.CD}
670672
renderCIListHeader={renderCIListHeader}
671673
/>

src/Shared/Components/CICDHistory/service.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ export const getDeploymentDiffSelector = (
268268
return get(url)
269269
}
270270

271-
export function getCDBuildReport(appId, envId, pipelineId, workflowId) {
272-
return get(`app/cd-pipeline/workflow/download/${appId}/${envId}/${pipelineId}/${workflowId}`)
273-
}
274-
275271
export const getTriggerHistory = async ({
276272
appId,
277273
envId,

src/Shared/Components/CICDHistory/types.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ export interface ArtifactType {
551551
artifact: string
552552
blobStorageEnabled: boolean
553553
isArtifactUploaded?: boolean
554-
getArtifactPromise?: () => Promise<any>
554+
downloadArtifactUrl?: string
555555
isJobView?: boolean
556556
isJobCI?: boolean
557557
type: HistoryComponentType

src/Shared/Helpers.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,11 @@ export const decode = (data, isEncoded: boolean = false) =>
707707
agg[curr.key] = curr.value
708708
return agg
709709
}, {})
710+
711+
export const getFileNameFromHeaders = (headers: Headers) =>
712+
headers
713+
?.get('content-disposition')
714+
?.split(';')
715+
?.find((n) => n.includes('filename='))
716+
?.replace('filename=', '')
717+
.trim()

src/Shared/Hooks/UseDownload/UseDownload.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useState } from 'react'
44
import { toast } from 'react-toastify'
55
import { API_STATUS_CODES } from '@Common/Constants'
66
import { ServerErrors } from '@Common/ServerError'
7+
import { getFileNameFromHeaders } from '@Shared/Helpers'
78
import { getDownloadResponse } from './service'
89
import { HandleDownloadProps } from './types'
910

@@ -23,7 +24,7 @@ const useDownload = () => {
2324
fileName,
2425
showSuccessfulToast = true,
2526
downloadSuccessToastContent = 'Downloaded Successfully',
26-
}: HandleDownloadProps) => {
27+
}: HandleDownloadProps): Promise<Error | ServerErrors> => {
2728
setIsDownloading(true)
2829
if (showFilePreparingToast) {
2930
toast.info(
@@ -45,14 +46,7 @@ const useDownload = () => {
4546
const a = document.createElement('a')
4647
a.href = blobUrl
4748

48-
// Get filename from response headers
49-
const defaultFileName = response.headers
50-
.get('content-disposition')
51-
.split(';')
52-
.find((n) => n.includes('filename='))
53-
.replace('filename=', '')
54-
.trim()
55-
a.download = fileName || defaultFileName || 'file.tgz'
49+
a.download = fileName || getFileNameFromHeaders(response.headers) || 'file.tgz'
5650

5751
// Append the link element to the DOM
5852
document.body.appendChild(a)
@@ -69,15 +63,19 @@ const useDownload = () => {
6963
if (showSuccessfulToast) {
7064
toast.success(downloadSuccessToastContent)
7165
}
66+
} else if (response.status === API_STATUS_CODES.NO_CONTENT) {
67+
throw new Error('No content to download')
7268
} else {
7369
const jsonResponseError: ServerErrors = await response?.json()
7470
throw new ServerErrors(jsonResponseError)
7571
}
7672
} catch (error) {
77-
showError(error)
78-
} finally {
7973
setIsDownloading(false)
74+
showError(error)
75+
return error
8076
}
77+
setIsDownloading(false)
78+
return null
8179
}
8280

8381
return { handleDownload, isDownloading }

0 commit comments

Comments
 (0)