Module: use-required-downloads
Table of contents
Functions
Functions
useRequiredDownloads
▸ useRequiredDownloads(requiredDownloads_
): object
Custom React hook for managing and tracking required downloads within an application. It allows for initiating downloads, monitoring download progress, and checking completion status for a predefined set of downloads. This hook leverages the Electron IPC mechanism to communicate with the main process for file operations, ensuring that required files are downloaded and available for use within the application.
The hook encapsulates the logic for:
- Adding downloads to a queue based on a list of required downloads.
- Initiating the download process for each item in the queue.
- Monitoring the progress of each download and updating the state accordingly.
- Determining when all required downloads have been completed.
It maintains internal state to track the number of downloads, their cumulative progress, and whether the download process is active or completed. This state can be accessed via the returned object, allowing the consuming component to react to changes in the download process, display progress to the user, or trigger additional actions once downloads are complete.
Parameters
Name | Type | Description |
---|---|---|
requiredDownloads_ | RequiredDownload [] | An array of objects, each representing a download task with properties such as source URL, destination path, and unique identifier. |
Returns
object
An object containing the following properties:
- {boolean} isCompleted - A boolean flag indicating whether all required downloads have been completed.
- {boolean} isDownloading - A boolean flag indicating whether the download process is currently active.
- {number} downloadCount - The number of downloads that have been completed.
- {number} percent - The current progress percentage of the ongoing download task. This is a cumulative value if multiple downloads are being processed.
- {RequiredDownload[]} requiredDownloads - An array of download tasks that are yet to be completed.
- {Function} download - A function to initiate the download process for the required files.
Example
// Define a list of required downloads for your application.
const requiredDownloads = [
{
id: 'unique-download-id-1',
source: 'https://example.com/file1.zip',
destination: 'path/to/save',
label: 'File 1',
},
{
id: 'unique-download-id-2',
source: 'https://example.com/file2.zip',
destination: 'path/to/save',
label: 'File 2',
}
];
// Use the hook in your component to manage these downloads.
const {
isCompleted,
isDownloading,
downloadCount,
percent,
requiredDownloads,
download,
} = useRequiredDownloads(requiredDownloads);
// Initiate the download process.
useEffect(() => {
if (!isCompleted && requiredDownloads.length > 0) {
download();
}
}, [isCompleted, requiredDownloads, download]);
// Render progress or completion state to the user.
return (
<div>
{isDownloading && <p>Downloading: {percent}% completed</p>}
{isCompleted && <p>All downloads completed!</p>}
</div>
);
Defined in
react/dist/esm/use-required-downloads/index.js:79