Skip to content

Commit 33d903d

Browse files
committed
chore: move util to shared helpers
1 parent cef017b commit 33d903d

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ 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'
9-
import { getDefaultFileName } from './utils'
1010

1111
const useDownload = () => {
1212
const [isDownloading, setIsDownloading] = useState<boolean>(false)
13-
const [downloadError, setDownloadError] = useState(null)
13+
const [downloadError, setDownloadError] = useState<Error | ServerErrors>(null)
1414

1515
/**
1616
* @param downloadUrl - API url for downloading file
@@ -47,7 +47,7 @@ const useDownload = () => {
4747
const a = document.createElement('a')
4848
a.href = blobUrl
4949

50-
a.download = fileName || getDefaultFileName(response.headers) || 'file.tgz'
50+
a.download = fileName || getFileNameFromHeaders(response.headers) || 'file.tgz'
5151

5252
// Append the link element to the DOM
5353
document.body.appendChild(a)

src/Shared/Hooks/UseDownload/utils.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)