-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
220 lines (195 loc) · 8.09 KB
/
Copy pathApp.tsx
File metadata and controls
220 lines (195 loc) · 8.09 KB
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
import type React from 'react';
import { useEffect, useReducer, useState } from 'react';
import { match } from 'ts-pattern';
import { GameArea } from './components/GameArea';
import { LevelSelector } from './components/LevelSelector';
import { RealSongGameArea } from './components/RealSongGameArea';
import { SEOHead } from './components/SEOHead';
import { TheoryModal } from './components/TheoryModal';
import { useTranslation } from './i18n/I18nContext';
import type { Language } from './i18n/types';
import { fetchTheoryLesson } from './services/contentService';
import { gameReducer, initialGameState } from './services/gameReducer';
import { generateProgression } from './services/theoryService';
import type { LevelConfig, RealSong } from './types';
// Type Guard
const isLanguage = (lang: string): lang is Language => {
return match(lang as Language)
.with('en', 'es', 'pt', 'ja', () => true)
.otherwise(() => false);
};
const Header: React.FC<{ onBack: (isRepeatClick?: boolean) => void }> = ({ onBack }) => {
const { t, language, setLanguage } = useTranslation();
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
const handleOnline = () => setIsOnline(true);
const handleOffline = () => setIsOnline(false);
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
return (
<header className="sticky top-0 z-40 border-slate-800 border-b bg-slate-900/50 p-6 backdrop-blur">
<div className="mx-auto flex max-w-7xl items-center justify-between">
<button
type="button"
className="flex cursor-pointer items-center gap-3 transition-opacity hover:opacity-80 focus:outline-none"
onClick={(e) => onBack(e.detail > 1)}
aria-label={t('common.back')}
>
<div className="flex h-8 w-8 items-center justify-center rounded-lg bg-gradient-to-br from-indigo-500 to-cyan-500 shadow-indigo-500/20 shadow-lg">
<span className="font-bold text-white text-xs">EV</span>
</div>
<h1 className="font-bold text-white text-xl tracking-tight">{t('common.appName')}</h1>
</button>
<div className="flex items-center gap-4">
{!isOnline && (
<div
className="hidden animate-pulse items-center gap-2 rounded-full border border-red-500/20 bg-red-500/10 px-3 py-1 text-red-400 md:flex"
title={t('common.offline')}
>
<span className="h-2 w-2 rounded-full bg-red-500"></span>
<span className="font-bold text-xs uppercase tracking-wider">
{t('common.offline')}
</span>
</div>
)}
<a
href="https://github.com/WilsonNet/EarVibes"
target="_blank"
rel="noopener noreferrer"
className="p-2 text-slate-400 transition-colors hover:text-white"
aria-label="GitHub Repository"
title="GitHub Repository"
>
<span className="sr-only">GitHub Repository</span>
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<title>GitHub Repository</title>
<path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"></path>
</svg>
</a>
<div className="group relative">
<select
value={language}
onChange={(e) => {
const val = e.target.value;
if (isLanguage(val)) {
setLanguage(val);
}
}}
className="cursor-pointer appearance-none rounded-full border border-slate-700 bg-slate-800/80 py-1.5 pr-8 pl-4 font-bold font-mono text-slate-400 text-xs uppercase tracking-wider transition-all hover:border-indigo-500/50 hover:bg-slate-700 hover:text-slate-200 focus:outline-none focus:ring-2 focus:ring-indigo-500/50"
aria-label={t('common.selectLanguage')}
>
<option value="en">English</option>
<option value="es">Español</option>
<option value="pt">Português</option>
<option value="ja">日本語</option>
</select>
<div className="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2.5 text-slate-500 transition-colors group-hover:text-indigo-400">
<svg
className="h-3 w-3"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2.5}
aria-hidden="true"
>
<title>Language selector dropdown</title>
<path strokeLinecap="round" strokeLinejoin="round" d="M19 9l-7 7-7-7" />
</svg>
</div>
</div>
</div>
</div>
{/* Mobile Offline Banner */}
{!isOnline && (
<div className="absolute top-full right-0 left-0 bg-red-600 py-1 text-center font-bold text-[10px] text-white shadow-lg md:hidden">
{t('common.offline')}
</div>
)}
</header>
);
};
function App() {
const { t } = useTranslation();
const [gameState, dispatch] = useReducer(gameReducer, initialGameState);
// Fetch theory content whenever level or language changes
useEffect(() => {
let isMounted = true;
if (gameState.status === 'THEORY' && gameState.level) {
fetchTheoryLesson(gameState.level.id, t).then((content) => {
if (isMounted) {
dispatch({ type: 'THEORY_LOADED', content });
}
});
}
return () => {
isMounted = false;
};
}, [gameState.status, gameState.level, t]);
const handleSelectLevel = (level: LevelConfig, isRepeatClick = false) => {
dispatch({ type: 'SELECT_LEVEL', level, isRepeatClick });
};
const handleSelectRealSong = (song: RealSong, isRepeatClick = false) => {
dispatch({ type: 'SELECT_REAL_SONG', song, isRepeatClick });
};
const startGame = (isRepeatClick = false) => {
if (isRepeatClick || !gameState.level) return;
dispatch({
type: 'START_GAME',
progression: generateProgression(gameState.level.id, gameState.level.type),
isRepeatClick,
});
};
const handleBack = (isRepeatClick = false) => {
dispatch({ type: 'RESET', isRepeatClick });
};
return (
<div className="min-h-screen bg-slate-900 font-sans text-slate-200 selection:bg-indigo-500/30">
<SEOHead />
<Header onBack={handleBack} />
<main className="relative z-0 flex min-h-[calc(100vh-80px)] w-full flex-col items-center justify-center">
{gameState.status === 'IDLE' && (
<LevelSelector
onSelectLevel={handleSelectLevel}
onSelectRealSong={handleSelectRealSong}
/>
)}
{gameState.status === 'THEORY' && (
<TheoryModal
content={gameState.theoryContent}
onStart={startGame}
onBack={handleBack}
isLoading={gameState.isLoading}
/>
)}
{gameState.status === 'REAL_SONG' && gameState.activeRealSong && (
<RealSongGameArea
song={gameState.activeRealSong}
gameState={gameState}
dispatch={dispatch}
onBack={handleBack}
/>
)}
{(gameState.status === 'PLAYING' || gameState.status === 'FEEDBACK') && (
<GameArea gameState={gameState} dispatch={dispatch} onBack={handleBack} />
)}
</main>
</div>
);
}
export default App;