/**
* Demo component for shuffle-triggered cache refresh functionality
* Shows how the cache refresh works when shuffle is toggled
*/
import React, { useState } from 'react';
import { useShuffleCacheRefresh } from '../hooks/useShuffleCacheRefresh';
import { OfflineModeIndicator } from './OfflineModeIndicator';
import './ShuffleCacheRefreshDemo.css';
export const ShuffleCacheRefreshDemo: React.FC = () => {
const { refreshState, triggerManualRefresh, isRefreshing, clearError } = useShuffleCacheRefresh();
const [shuffleEnabled, setShuffleEnabled] = useState(false);
const handleShuffleToggle = async () => {
const newShuffleState = !shuffleEnabled;
setShuffleEnabled(newShuffleState);
// Simulate the shuffle toggle event that would normally come from PlaybackManager
window.dispatchEvent(new CustomEvent('shuffle-toggled', {
detail: {
shuffleEnabled: newShuffleState,
currentTrack: {
Id: 'demo-track-123',
Name: 'Demo Song',
Artists: ['Demo Artist'],
Album: 'Demo Album'
},
playlistLength: 150
}
}));
};
const handleManualRefresh = async () => {
await triggerManualRefresh(shuffleEnabled);
};
return (
🔀 Shuffle Cache Refresh Demo
This demo shows how the cache refreshes when shuffle is toggled
{/* Shuffle Control */}
{/* Cache Refresh Status */}
Cache Refresh Status
{refreshState.error && (
❌
{refreshState.error}
)}
{refreshState.isRefreshing && (
🔄
Refreshing Cache...
{refreshState.refreshedSongs} / {refreshState.totalSongs} songs
{Math.round(refreshState.progress)}%
{refreshState.status && (
{refreshState.status}
)}
)}
{!refreshState.isRefreshing && refreshState.progress === 100 && (
✅
Cache refreshed successfully!
)}
{!refreshState.isRefreshing && !refreshState.error && refreshState.progress === 0 && (
💾
Cache is ready. Toggle shuffle to refresh.
)}
{/* Offline Mode Indicator */}
Offline Mode Indicator
{/* Feature Description */}
How It Works
- Shuffle Toggle: When you toggle shuffle, the cache is automatically refreshed
- Queue Reordering: Shuffle ON = random order, Shuffle OFF = sequential order
- Progress Tracking: Real-time progress updates during cache refresh
- Error Handling: Graceful error handling with retry capabilities
- Visual Feedback: Clear indicators for all cache states
{/* Requirements Mapping */}
Requirements Implemented
- 3.1: Cache clears and rebuilds when shuffle is toggled
- 3.2: Shuffle ON creates randomly ordered cache
- 3.3: Shuffle OFF creates sequential cache
- 5.4: Progress and completion feedback is shown
);
};
export default ShuffleCacheRefreshDemo;