@@ -13,12 +13,16 @@ const useDownload = () => {
13
13
/**
14
14
* @param downloadUrl - API url for downloading file
15
15
* @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
17
19
*/
18
20
const handleDownload = async ( {
19
21
downloadUrl,
20
22
showFilePreparingToast = false ,
21
- fileName = 'file.tgz' ,
23
+ fileName,
24
+ showSuccessfulToast = true ,
25
+ downloadSuccessToastContent = 'Downloaded Successfully' ,
22
26
} : HandleDownloadProps ) => {
23
27
setIsDownloading ( true )
24
28
if ( showFilePreparingToast ) {
@@ -40,7 +44,15 @@ const useDownload = () => {
40
44
// Create a link element
41
45
const a = document . createElement ( 'a' )
42
46
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'
44
56
45
57
// Append the link element to the DOM
46
58
document . body . appendChild ( a )
@@ -54,7 +66,9 @@ const useDownload = () => {
54
66
document . body . removeChild ( a )
55
67
} , 0 )
56
68
57
- toast . success ( 'Downloaded Successfully' )
69
+ if ( showSuccessfulToast ) {
70
+ toast . success ( downloadSuccessToastContent )
71
+ }
58
72
} else {
59
73
const jsonResponseError : ServerErrors = await response ?. json ( )
60
74
throw new ServerErrors ( jsonResponseError )
0 commit comments