Spaces:
Build error
Build error
Upload components/WorkoutGenerator.jsx with huggingface_hub
Browse files- components/WorkoutGenerator.jsx +123 -0
components/WorkoutGenerator.jsx
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState, useEffect } from 'react';
|
| 2 |
+
import { Shuffle, Filter, Sparkles } from 'lucide-react';
|
| 3 |
+
import WorkoutCard from './WorkoutCard';
|
| 4 |
+
|
| 5 |
+
const categories = [
|
| 6 |
+
{ id: 'all', label: 'All Snacks', icon: '🎯' },
|
| 7 |
+
{ id: 'strength', label: 'Strength', icon: '💪' },
|
| 8 |
+
{ id: 'cardio', label: 'Cardio', icon: '❤️' },
|
| 9 |
+
{ id: 'stretch', label: 'Stretch', icon: '🧘' },
|
| 10 |
+
{ id: 'mindfulness', label: 'Mind', icon: '🧠' },
|
| 11 |
+
{ id: 'wellness', label: 'Wellness', icon: '✨' }
|
| 12 |
+
];
|
| 13 |
+
|
| 14 |
+
export default function WorkoutGenerator({ favorites, onToggleFavorite }) {
|
| 15 |
+
const [workouts, setWorkouts] = useState([]);
|
| 16 |
+
const [currentWorkout, setCurrentWorkout] = useState(null);
|
| 17 |
+
const [selectedCategory, setSelectedCategory] = useState('all');
|
| 18 |
+
const [loading, setLoading] = useState(false);
|
| 19 |
+
|
| 20 |
+
useEffect(() => {
|
| 21 |
+
fetchWorkouts();
|
| 22 |
+
}, [selectedCategory]);
|
| 23 |
+
|
| 24 |
+
const fetchWorkouts = async () => {
|
| 25 |
+
setLoading(true);
|
| 26 |
+
try {
|
| 27 |
+
const res = await fetch(`/api/workouts?category=${selectedCategory}`);
|
| 28 |
+
const data = await res.json();
|
| 29 |
+
setWorkouts(data);
|
| 30 |
+
if (data.length > 0) {
|
| 31 |
+
setCurrentWorkout(data[Math.floor(Math.random() * data.length)]);
|
| 32 |
+
}
|
| 33 |
+
} catch (error) {
|
| 34 |
+
console.error('Failed to fetch workouts:', error);
|
| 35 |
+
}
|
| 36 |
+
setLoading(false);
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
const generateRandom = async () => {
|
| 40 |
+
setLoading(true);
|
| 41 |
+
try {
|
| 42 |
+
const res = await fetch(`/api/workouts?category=${selectedCategory}&random=true`);
|
| 43 |
+
const data = await res.json();
|
| 44 |
+
if (data.length > 0) {
|
| 45 |
+
setCurrentWorkout(data[0]);
|
| 46 |
+
}
|
| 47 |
+
} catch (error) {
|
| 48 |
+
console.error('Failed to generate workout:', error);
|
| 49 |
+
}
|
| 50 |
+
setLoading(false);
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
return (
|
| 54 |
+
<div className="space-y-6">
|
| 55 |
+
<div className="bg-gradient-to-r from-primary-600 to-primary-700 rounded-2xl p-8 text-white shadow-xl">
|
| 56 |
+
<div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-4 mb-6">
|
| 57 |
+
<div>
|
| 58 |
+
<h2 className="text-3xl font-bold mb-2 flex items-center gap-2">
|
| 59 |
+
<Sparkles className="w-8 h-8 text-yellow-300" />
|
| 60 |
+
Workout Snack Generator
|
| 61 |
+
</h2>
|
| 62 |
+
<p className="text-primary-100">Quick desk-friendly exercises to keep you energized throughout the day</p>
|
| 63 |
+
</div>
|
| 64 |
+
<a
|
| 65 |
+
href="https://huggingface.co/spaces/akhaliq/anycoder"
|
| 66 |
+
target="_blank"
|
| 67 |
+
rel="noopener noreferrer"
|
| 68 |
+
className="text-sm bg-white/20 hover:bg-white/30 px-4 py-2 rounded-full transition-colors backdrop-blur-sm"
|
| 69 |
+
>
|
| 70 |
+
Built with anycoder →
|
| 71 |
+
</a>
|
| 72 |
+
</div>
|
| 73 |
+
|
| 74 |
+
<div className="flex flex-wrap gap-2 mb-6">
|
| 75 |
+
{categories.map(cat => (
|
| 76 |
+
<button
|
| 77 |
+
key={cat.id}
|
| 78 |
+
onClick={() => setSelectedCategory(cat.id)}
|
| 79 |
+
className={`px-4 py-2 rounded-full text-sm font-medium transition-all duration-200 ${selectedCategory === cat.id ? 'bg-white text-primary-700 shadow-lg' : 'bg-primary-500/50 text-white hover:bg-primary-500'}`}
|
| 80 |
+
>
|
| 81 |
+
<span className="mr-1">{cat.icon}</span> {cat.label}
|
| 82 |
+
</button>
|
| 83 |
+
))}
|
| 84 |
+
</div>
|
| 85 |
+
|
| 86 |
+
<button
|
| 87 |
+
onClick={generateRandom}
|
| 88 |
+
disabled={loading}
|
| 89 |
+
className="w-full md:w-auto bg-white text-primary-700 hover:bg-primary-50 font-bold py-3 px-8 rounded-xl shadow-lg hover:shadow-xl transition-all duration-200 flex items-center justify-center gap-2 disabled:opacity-50"
|
| 90 |
+
>
|
| 91 |
+
<Shuffle className={`w-5 h-5 ${loading ? 'animate-spin' : ''}`} />
|
| 92 |
+
{loading ? 'Generating...' : 'Generate Random Snack'}
|
| 93 |
+
</button>
|
| 94 |
+
</div>
|
| 95 |
+
|
| 96 |
+
{currentWorkout && (
|
| 97 |
+
<div className="max-w-2xl mx-auto">
|
| 98 |
+
<div className="text-center mb-4">
|
| 99 |
+
<span className="inline-block px-4 py-1 bg-accent-100 text-accent-700 rounded-full text-sm font-semibold">
|
| 100 |
+
Your Daily Snack
|
| 101 |
+
</span>
|
| 102 |
+
</div>
|
| 103 |
+
<WorkoutCard
|
| 104 |
+
workout={currentWorkout}
|
| 105 |
+
isFavorite={favorites.includes(currentWorkout.id)}
|
| 106 |
+
onToggleFavorite={onToggleFavorite}
|
| 107 |
+
/>
|
| 108 |
+
</div>
|
| 109 |
+
)}
|
| 110 |
+
|
| 111 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
| 112 |
+
{workouts.filter(w => w.id !== currentWorkout?.id).map(workout => (
|
| 113 |
+
<WorkoutCard
|
| 114 |
+
key={workout.id}
|
| 115 |
+
workout={workout}
|
| 116 |
+
isFavorite={favorites.includes(workout.id)}
|
| 117 |
+
onToggleFavorite={onToggleFavorite}
|
| 118 |
+
/>
|
| 119 |
+
))}
|
| 120 |
+
</div>
|
| 121 |
+
</div>
|
| 122 |
+
);
|
| 123 |
+
}
|