Spaces:
Build error

File size: 4,587 Bytes
3ed7101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useState, useEffect } from 'react';
import { Shuffle, Filter, Sparkles } from 'lucide-react';
import WorkoutCard from './WorkoutCard';

const categories = [
  { id: 'all', label: 'All Snacks', icon: '🎯' },
  { id: 'strength', label: 'Strength', icon: '💪' },
  { id: 'cardio', label: 'Cardio', icon: '❤️' },
  { id: 'stretch', label: 'Stretch', icon: '🧘' },
  { id: 'mindfulness', label: 'Mind', icon: '🧠' },
  { id: 'wellness', label: 'Wellness', icon: '✨' }
];

export default function WorkoutGenerator({ favorites, onToggleFavorite }) {
  const [workouts, setWorkouts] = useState([]);
  const [currentWorkout, setCurrentWorkout] = useState(null);
  const [selectedCategory, setSelectedCategory] = useState('all');
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    fetchWorkouts();
  }, [selectedCategory]);

  const fetchWorkouts = async () => {
    setLoading(true);
    try {
      const res = await fetch(`/api/workouts?category=${selectedCategory}`);
      const data = await res.json();
      setWorkouts(data);
      if (data.length > 0) {
        setCurrentWorkout(data[Math.floor(Math.random() * data.length)]);
      }
    } catch (error) {
      console.error('Failed to fetch workouts:', error);
    }
    setLoading(false);
  };

  const generateRandom = async () => {
    setLoading(true);
    try {
      const res = await fetch(`/api/workouts?category=${selectedCategory}&random=true`);
      const data = await res.json();
      if (data.length > 0) {
        setCurrentWorkout(data[0]);
      }
    } catch (error) {
      console.error('Failed to generate workout:', error);
    }
    setLoading(false);
  };

  return (
    <div className="space-y-6">
      <div className="bg-gradient-to-r from-primary-600 to-primary-700 rounded-2xl p-8 text-white shadow-xl">
        <div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-4 mb-6">
          <div>
            <h2 className="text-3xl font-bold mb-2 flex items-center gap-2">
              <Sparkles className="w-8 h-8 text-yellow-300" />
              Workout Snack Generator
            </h2>
            <p className="text-primary-100">Quick desk-friendly exercises to keep you energized throughout the day</p>
          </div>
          <a 
            href="https://huggingface.co/spaces/akhaliq/anycoder" 
            target="_blank" 
            rel="noopener noreferrer"
            className="text-sm bg-white/20 hover:bg-white/30 px-4 py-2 rounded-full transition-colors backdrop-blur-sm"
          >
            Built with anycoder →
          </a>
        </div>

        <div className="flex flex-wrap gap-2 mb-6">
          {categories.map(cat => (
            <button
              key={cat.id}
              onClick={() => setSelectedCategory(cat.id)}
              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'}`}
            >
              <span className="mr-1">{cat.icon}</span> {cat.label}
            </button>
          ))}
        </div>

        <button
          onClick={generateRandom}
          disabled={loading}
          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"
        >
          <Shuffle className={`w-5 h-5 ${loading ? 'animate-spin' : ''}`} />
          {loading ? 'Generating...' : 'Generate Random Snack'}
        </button>
      </div>

      {currentWorkout && (
        <div className="max-w-2xl mx-auto">
          <div className="text-center mb-4">
            <span className="inline-block px-4 py-1 bg-accent-100 text-accent-700 rounded-full text-sm font-semibold">
              Your Daily Snack
            </span>
          </div>
          <WorkoutCard 
            workout={currentWorkout} 
            isFavorite={favorites.includes(currentWorkout.id)}
            onToggleFavorite={onToggleFavorite}
          />
        </div>
      )}

      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        {workouts.filter(w => w.id !== currentWorkout?.id).map(workout => (
          <WorkoutCard 
            key={workout.id} 
            workout={workout}
            isFavorite={favorites.includes(workout.id)}
            onToggleFavorite={onToggleFavorite}
          />
        ))}
      </div>
    </div>
  );
}