Overview
TheuseDownload hook provides a complete solution for downloading podcast audio files with real-time progress tracking, retry logic, and cancellation support. It includes built-in UI feedback through toast notifications and handles network errors gracefully.
Hook Signature
Return Values
Indicates whether a download is currently in progress
Download progress percentage (0-100)
Indicates whether the current download has been cancelled
Function to initiate a downloadParameters:
audioUrl(string): The URL of the audio file to downloadfileName(string): The name to save the file as
Function to cancel an ongoing download. No parameters required.
Features
Progress Tracking
The hook tracks download progress in real-time and updates the UI with the current percentage:Retry Logic
Automatic retry with exponential backoff (up to 3 attempts):Cancellation Support
Downloads can be cancelled at any time using the AbortController API:Toast Notifications
Built-in toast notifications for:- Download progress updates
- Download completion
- Download cancellation
- Download errors
Usage Examples
Basic Usage
From/home/daytona/workspace/source/src/components/PodcastDetail/PodcastDetail.jsx:52:
MP3 Player Integration
From/home/daytona/workspace/source/src/components/MP3Player/MP3Player.jsx:52:
Last Podcast Component
From/home/daytona/workspace/source/src/components/LastPodcast/LastPodcast.jsx:48:
Implementation Details
State Management
The hook uses multiple useState and useRef hooks to manage download state:Download Process
- Create AbortController for cancellation support
- Fetch audio file with retry logic
- Read response as stream
- Track progress and update UI
- Create blob from chunks
- Trigger browser download
- Clean up resources
Error Handling
The hook handles multiple error scenarios:- Network errors (with retry)
- HTTP errors
- User cancellation
- Timeout errors
Best Practices
Browser Compatibility
This hook uses:- Fetch API with AbortController
- Streams API (ReadableStream)
- Blob API
- URL.createObjectURL
