Overview
AudioTimeContext is a React Context provider that manages playback position persistence and user preferences for audio time tracking. It provides a centralized way to store, retrieve, and manage playback times for podcast episodes using both React state and localStorage.
This Context works alongside the Redux
audioTimeSlice to provide comprehensive playback time management. The Context handles global state and localStorage persistence, while the slice manages Redux-specific state.Provider Setup
TheAudioTimeProvider wraps the application to provide playback time management:
src/context/AudioTimeContext.jsx
Hook: useAudioTime
TheuseAudioTime hook provides access to the audio time context:
Context Values
playbackTimes
Object mapping episode titles to their current playback positions in secondsStructure:
{ [episodeTitle: string]: number }Example:updatePlaybackTime
Updates the playback time for a specific episodeSignature:
(audioTitle: string, currentTime: number) => voidParameters:audioTitle- The title of the podcast episodecurrentTime- Current playback position in seconds
- Only updates if
savePlaybackTimeistrue - Persists to localStorage under key
nsnPlaybackTimes - Merges with existing playback times
src/context/AudioTimeContext.jsx
getPlaybackTime
Retrieves the saved playback time for a specific episodeSignature:
(audioTitle: string) => numberReturns: Playback position in seconds, or 0 if not found or saving is disabledsrc/context/AudioTimeContext.jsx
savePlaybackTime
User preference flag indicating whether playback times should be savedDefault:
truelocalStorage key: nsnSavePlaybackTimetoggleSavePlaybackTime
Toggles the playback time saving preferenceSignature:
() => voidBehavior:- Flips the
savePlaybackTimeboolean - Persists preference to localStorage
- If disabling, clears all stored playback times
src/context/AudioTimeContext.jsx
clearPlaybackTimes
Clears all stored playback timesSignature:
() => voidBehavior:- Removes all playback times from state and localStorage
- Shows success/error toast notification
- Used by Settings component for manual cleanup
src/context/AudioTimeContext.jsx
localStorage Integration
The context uses two localStorage keys:| Key | Type | Purpose |
|---|---|---|
nsnPlaybackTimes | JSON object | Stores all playback positions |
nsnSavePlaybackTime | JSON boolean | Stores user preference for saving times |
Initialization
On mount, the provider loads saved data from localStorage:src/context/AudioTimeContext.jsx
Usage Examples
Saving Playback Position
Resuming Playback
Settings Toggle
Integration with Redux
The context complements the ReduxaudioTimeSlice:
- Context: Provides React-specific state management and localStorage operations
- Redux Slice: Manages playback times within the Redux store for global state consistency
Mobile Detection
The context integrates withuseMobileDetect to position toast notifications appropriately:
src/context/AudioTimeContext.jsx
Best Practices
Related Documentation
- Audio Time Slice - Redux state management for playback times
- PersistentPlayer - Main consumer of playback time data
- Settings - UI for managing playback preferences
