File size: 7,069 Bytes
22a6915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import React, { useState, useEffect } from 'react';

function Achievement() {
  const [stats, setStats] = useState({
    total_sessions: 0,
    total_focus_time: 0,
    avg_focus_score: 0,
    streak_days: 0
  });
  const [badges, setBadges] = useState([]);
  const [loading, setLoading] = useState(true);

  // Format total focus time for display.
  const formatTime = (seconds) => {
    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    if (hours > 0) return `${hours}h ${minutes}m`;
    return `${minutes}m`;
  };

  function calculateBadges(data) {
    const earnedBadges = [];

    // First-session badge
    if (data.total_sessions >= 1) {
      earnedBadges.push({
        id: 'first-session',
        name: 'First Step',
        description: 'Complete your first focus session',
        icon: '🎯',
        unlocked: true
      });
    }

    // 10-session badge
    if (data.total_sessions >= 10) {
      earnedBadges.push({
        id: 'ten-sessions',
        name: 'Getting Started',
        description: 'Complete 10 focus sessions',
        icon: '⭐',
        unlocked: true
      });
    }

    // 50-session badge
    if (data.total_sessions >= 50) {
      earnedBadges.push({
        id: 'fifty-sessions',
        name: 'Dedicated',
        description: 'Complete 50 focus sessions',
        icon: 'πŸ†',
        unlocked: true
      });
    }

    // Focus Master badge (average focus score > 80%)
    if (data.avg_focus_score >= 0.8 && data.total_sessions >= 5) {
      earnedBadges.push({
        id: 'focus-master',
        name: 'Focus Master',
        description: 'Maintain 80%+ average focus score',
        icon: '🧠',
        unlocked: true
      });
    }

    // Streak badges
    if (data.streak_days >= 7) {
      earnedBadges.push({
        id: 'week-streak',
        name: 'Week Warrior',
        description: '7 day streak',
        icon: 'πŸ”₯',
        unlocked: true
      });
    }

    if (data.streak_days >= 30) {
      earnedBadges.push({
        id: 'month-streak',
        name: 'Month Master',
        description: '30 day streak',
        icon: 'πŸ’Ž',
        unlocked: true
      });
    }

    // Total focus time badge (10+ hours)
    if (data.total_focus_time >= 36000) {
      earnedBadges.push({
        id: 'ten-hours',
        name: 'Endurance',
        description: '10+ hours total focus time',
        icon: '⏱️',
        unlocked: true
      });
    }

    // Full badge catalog, including locked examples
    const allBadges = [
      {
        id: 'first-session',
        name: 'First Step',
        description: 'Complete your first focus session',
        icon: '🎯',
        unlocked: data.total_sessions >= 1
      },
      {
        id: 'ten-sessions',
        name: 'Getting Started',
        description: 'Complete 10 focus sessions',
        icon: '⭐',
        unlocked: data.total_sessions >= 10
      },
      {
        id: 'fifty-sessions',
        name: 'Dedicated',
        description: 'Complete 50 focus sessions',
        icon: 'πŸ†',
        unlocked: data.total_sessions >= 50
      },
      {
        id: 'focus-master',
        name: 'Focus Master',
        description: 'Maintain 80%+ average focus score',
        icon: '🧠',
        unlocked: data.avg_focus_score >= 0.8 && data.total_sessions >= 5
      },
      {
        id: 'week-streak',
        name: 'Week Warrior',
        description: '7 day streak',
        icon: 'πŸ”₯',
        unlocked: data.streak_days >= 7
      },
      {
        id: 'month-streak',
        name: 'Month Master',
        description: '30 day streak',
        icon: 'πŸ’Ž',
        unlocked: data.streak_days >= 30
      },
      {
        id: 'ten-hours',
        name: 'Endurance',
        description: '10+ hours total focus time',
        icon: '⏱️',
        unlocked: data.total_focus_time >= 36000
      },
      {
        id: 'hundred-sessions',
        name: 'Centurion',
        description: 'Complete 100 focus sessions',
        icon: 'πŸ‘‘',
        unlocked: data.total_sessions >= 100
      }
    ];

    setBadges(allBadges);
  }

  // Load summary statistics.
  useEffect(() => {
    fetch('/api/stats/summary')
      .then(res => res.json())
      .then(data => {
        setStats(data);
        calculateBadges(data);
        setLoading(false);
      })
      .catch(err => {
        console.error('Failed to load stats:', err);
        setLoading(false);
      });
  }, []);

  return (
    <main id="page-c" className="page">
      <h1 className="page-title">My Achievement</h1>

      {loading ? (
        <div style={{ textAlign: 'center', padding: '40px', color: '#888' }}>
          Loading stats...
        </div>
      ) : (
        <>
          <div className="stats-grid">
            <div className="stat-card">
              <div className="stat-number" id="total-sessions">{stats.total_sessions}</div>
              <div className="stat-label">Total Sessions</div>
            </div>
            <div className="stat-card">
              <div className="stat-number" id="total-hours">{formatTime(stats.total_focus_time)}</div>
              <div className="stat-label">Total Focus Time</div>
            </div>
            <div className="stat-card">
              <div className="stat-number" id="avg-focus">{(stats.avg_focus_score * 100).toFixed(1)}%</div>
              <div className="stat-label">Average Focus</div>
            </div>
            <div className="stat-card">
              <div className="stat-number" id="current-streak">{stats.streak_days}</div>
              <div className="stat-label">Day Streak</div>
            </div>
          </div>

          <div className="achievements-section">
            <h2>Badges</h2>
            <div id="badges-container" className="badges-grid">
              {badges.map(badge => (
                <div
                  key={badge.id}
                  className={`badge ${badge.unlocked ? 'unlocked' : 'locked'}`}
                  style={{
                    padding: '20px',
                    textAlign: 'center',
                    border: '2px solid',
                    borderColor: badge.unlocked ? '#00FF00' : '#444',
                    borderRadius: '10px',
                    backgroundColor: badge.unlocked ? 'rgba(0, 255, 0, 0.1)' : 'rgba(68, 68, 68, 0.1)',
                    opacity: badge.unlocked ? 1 : 0.5,
                    transition: 'all 0.3s'
                  }}
                >
                  <div style={{ fontSize: '48px', marginBottom: '10px' }}>
                    {badge.unlocked ? badge.icon : 'πŸ”’'}
                  </div>
                  <div style={{ fontWeight: 'bold', marginBottom: '5px' }}>
                    {badge.name}
                  </div>
                  <div style={{ fontSize: '12px', color: '#888' }}>
                    {badge.description}
                  </div>
                </div>
              ))}
            </div>
          </div>
        </>
      )}
    </main>
  );
}

export default Achievement;