Skip to content

Commit 238b2b9

Browse files
authored
Merge pull request #611 from devtron-labs/feat/new-tab-download
feat: add support for opening the download link in new tab
2 parents bcb084e + 044b864 commit 238b2b9

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/Shared/Components/CICDHistory/Artifacts.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ const Artifacts = ({
149149
targetPlatforms,
150150
}: ArtifactType) => {
151151
const { isSuperAdmin } = useSuperAdmin()
152-
const { handleDownload } = useDownload()
152+
const { handleDownload } = useDownload({
153+
shouldOpenInNewTab: true,
154+
})
153155

154156
const { triggerId, buildId } = useParams<{
155157
triggerId: string

src/Shared/Hooks/UseDownload/UseDownload.tsx

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

1717
import { showError } from '@Common/Helper'
1818
import { useState } from 'react'
19-
import { API_STATUS_CODES } from '@Common/Constants'
19+
import { API_STATUS_CODES, Host } from '@Common/Constants'
2020
import { ServerErrors } from '@Common/ServerError'
2121
import { getFileNameFromHeaders } from '@Shared/Helpers'
2222
import { ToastManager, ToastVariantType } from '@Shared/Services'
2323
import { getDownloadResponse } from './service'
24-
import { UseDownloadReturnType } from './types'
24+
import { UseDownloadProps, UseDownloadReturnType } from './types'
2525

26-
const useDownload = (): UseDownloadReturnType => {
26+
const useDownload = ({ shouldOpenInNewTab }: UseDownloadProps = {}): UseDownloadReturnType => {
2727
const [isDownloading, setIsDownloading] = useState<boolean>(false)
2828

2929
/**
@@ -40,9 +40,17 @@ const useDownload = (): UseDownloadReturnType => {
4040
showSuccessfulToast = true,
4141
downloadSuccessToastContent = 'Downloaded Successfully',
4242
}) => {
43+
const downloadUrlWithHost = `${Host}/${downloadUrl}`
44+
45+
if (shouldOpenInNewTab) {
46+
window.open(downloadUrlWithHost, '_blank')
47+
return null
48+
}
49+
4350
setIsDownloading(true)
51+
4452
try {
45-
const response = await getDownloadResponse(downloadUrl)
53+
const response = await getDownloadResponse(downloadUrlWithHost)
4654
if (response.status === API_STATUS_CODES.OK) {
4755
if (showFilePreparingToast) {
4856
ToastManager.showToast({

src/Shared/Hooks/UseDownload/service.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Host } from '@Common/Constants'
18-
19-
export const getDownloadResponse = (downloadUrl: string) => fetch(`${Host}/${downloadUrl}`)
17+
export const getDownloadResponse = (downloadUrl: string) => fetch(downloadUrl)

src/Shared/Hooks/UseDownload/types.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ export interface HandleDownloadProps {
2424
downloadSuccessToastContent?: string
2525
}
2626

27+
export interface UseDownloadProps {
28+
/**
29+
* If true, the download will open in a new tab
30+
*
31+
* @default false
32+
*/
33+
shouldOpenInNewTab?: boolean
34+
}
35+
2736
export interface UseDownloadReturnType {
2837
handleDownload: (props: HandleDownloadProps) => Promise<Error | ServerErrors>
2938
isDownloading: boolean

0 commit comments

Comments
 (0)