Skip to content

Commit c0fb5b4

Browse files
committed
feat: add props for download toast in use-download hook
1 parent 6c92c54 commit c0fb5b4

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
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.8-beta-3",
3+
"version": "0.1.8-beta-4",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Hooks/UseDownload/UseDownload.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ const useDownload = () => {
1313
/**
1414
* @param downloadUrl - API url for downloading file
1515
* @param filterType - Show toast 'Preparing file for download'
16-
* @param query - fileName of the downloaded file
16+
* @param fileName - fileName of the downloaded file
17+
* @param showSuccessfulToast - show toast on successful download
18+
* @param downloadSuccessToastContent - Content to show in toast on successful download
1719
*/
1820
const handleDownload = async ({
1921
downloadUrl,
2022
showFilePreparingToast = false,
21-
fileName = 'file.tgz',
23+
fileName,
24+
showSuccessfulToast = true,
25+
downloadSuccessToastContent = 'Downloaded Successfully',
2226
}: HandleDownloadProps) => {
2327
setIsDownloading(true)
2428
if (showFilePreparingToast) {
@@ -40,7 +44,15 @@ const useDownload = () => {
4044
// Create a link element
4145
const a = document.createElement('a')
4246
a.href = blobUrl
43-
a.download = fileName
47+
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'
4456

4557
// Append the link element to the DOM
4658
document.body.appendChild(a)
@@ -54,7 +66,9 @@ const useDownload = () => {
5466
document.body.removeChild(a)
5567
}, 0)
5668

57-
toast.success('Downloaded Successfully')
69+
if (showSuccessfulToast) {
70+
toast.success(downloadSuccessToastContent)
71+
}
5872
} else {
5973
const jsonResponseError: ServerErrors = await response?.json()
6074
throw new ServerErrors(jsonResponseError)

src/Shared/Hooks/UseDownload/types.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ export interface HandleDownloadProps {
22
downloadUrl: string
33
showFilePreparingToast?: boolean
44
fileName?: string
5+
showSuccessfulToast?: boolean
6+
downloadSuccessToastContent?: string
57
}

0 commit comments

Comments
 (0)