-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The useDownload hook in conjunction with the Download component should replace the withDownload HOC in future.
- Loading branch information
1 parent
f781093
commit 17c66e9
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* SPDX-FileCopyrightText: 2024 Greenbone AG | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import {useRef, useCallback} from 'react'; | ||
|
||
import logger from 'gmp/log'; | ||
|
||
import {hasValue} from 'gmp/utils/identity'; | ||
|
||
const log = logger.getLogger('web.components.form.useDownload'); | ||
|
||
/** | ||
* Hook to download a file | ||
* | ||
* Should be used in combination with the Download component | ||
* | ||
* @returns Array of downloadRef and download function | ||
*/ | ||
const useDownload = () => { | ||
const downloadRef = useRef(null); | ||
|
||
const download = useCallback(({filename, data, mimetype}) => { | ||
if (!hasValue(downloadRef.current)) { | ||
log.warn('Download ref not set.'); | ||
return; | ||
} | ||
|
||
downloadRef.current.setFilename(filename); | ||
downloadRef.current.setData(data, mimetype); | ||
downloadRef.current.download(); | ||
}, []); | ||
return [downloadRef, download]; | ||
}; | ||
|
||
export default useDownload; |