Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
executable file
·537 lines (480 loc) · 23.6 KB
/
Copy pathApp.tsx
File metadata and controls
executable file
·537 lines (480 loc) · 23.6 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useState, useEffect, useCallback } from 'react';
import { Sparkles, Moon, Sun, LogOut, History, Mic, Upload, Plus, Menu, ArrowLeft, X, Loader2, FileAudio, FileJson, FileText, ExternalLink } from 'lucide-react';
import AudioRecorder from './components/AudioRecorder';
import FileUploader from './components/FileUploader';
import TranscriptionDisplay from './components/TranscriptionDisplay';
import Button from './components/Button';
import Auth from './components/Auth';
import JobHistory from './components/JobHistory';
import { AppStatus, AudioData, AuthState, Job } from './types';
import * as storageUtils from './utils/storageUtils';
import * as dbUtils from './utils/dbUtils';
import * as jobProcessor from './services/jobProcessor';
function App() {
// --- Auth State ---
const [auth, setAuth] = useState<AuthState>({
isAuthenticated: false,
apiKey: null,
mode: 'session'
});
// --- App Logic State ---
// 'history' view is removed as a main view, it's now a sidebar overlay
const [view, setView] = useState<'create' | 'result'>('create');
const [mode, setMode] = useState<'record' | 'upload'>('record');
const [status, setStatus] = useState<AppStatus>('idle');
// Sidebar State
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const [audioData, setAudioData] = useState<AudioData | null>(null);
const [currentJob, setCurrentJob] = useState<Job | null>(null);
const [jobs, setJobs] = useState<Job[]>([]);
const [error, setError] = useState<string | null>(null);
// Initialize dark mode
const [isDarkMode, setIsDarkMode] = useState(() => {
if (typeof window !== 'undefined') {
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
return false;
});
// Dark Mode Toggle
useEffect(() => {
if (isDarkMode) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}, [isDarkMode]);
const toggleDarkMode = () => setIsDarkMode(!isDarkMode);
// --- Auth Initialization ---
useEffect(() => {
const storedKey = storageUtils.getStoredApiKey();
if (storedKey) {
setAuth({
isAuthenticated: true,
apiKey: storedKey,
mode: 'persistent'
});
refreshJobs(storedKey);
}
}, []);
const refreshJobs = useCallback((key: string) => {
const list = storageUtils.getJobs(key);
setJobs(list);
// Check for any currently running jobs or unfinished business
const active = list.find(j => j.status === 'processing');
if (active) {
setStatus('processing');
setCurrentJob(active);
}
}, []);
// Poll for job updates if processing
useEffect(() => {
if (status === 'processing' && auth.apiKey && currentJob?.id) {
const jobId = currentJob.id;
const interval = setInterval(() => {
const freshJobs = storageUtils.getJobs(auth.apiKey!);
setJobs(freshJobs);
const updatedCurrent = freshJobs.find(j => j.id === jobId);
if (updatedCurrent) {
setCurrentJob(updatedCurrent);
// If we have partial results (segments), switch to view immediately
if (updatedCurrent.result && updatedCurrent.result.segments.length > 0 && view !== 'result') {
setView('result');
}
if (updatedCurrent.status === 'success') {
setStatus('success');
// Ensure view is result (though previous check should catch it)
if (view !== 'result') setView('result');
} else if (updatedCurrent.status === 'error') {
setStatus('error');
setError(updatedCurrent.error || "Transcription failed");
}
} else {
// Job disappeared?
console.warn("Current processing job not found in storage.");
setStatus('error');
setError("Job not found.");
}
}, 2000);
return () => clearInterval(interval);
}
}, [status, auth.apiKey, currentJob?.id, view]);
// --- Handlers ---
const handleLogin = (newAuth: AuthState) => {
setAuth(newAuth);
if (newAuth.mode === 'persistent' && newAuth.apiKey) {
storageUtils.saveApiKey(newAuth.apiKey);
}
if (newAuth.apiKey) {
// Resume any interrupted jobs
jobProcessor.resumeProcessing(newAuth.apiKey);
refreshJobs(newAuth.apiKey);
}
};
const handleLogout = () => {
if (auth.mode === 'persistent') {
storageUtils.clearStoredApiKey();
}
setAuth({ isAuthenticated: false, apiKey: null, mode: 'session' });
setJobs([]);
setCurrentJob(null);
setAudioData(null);
setView('create');
setIsSidebarOpen(false);
};
const handleAudioReady = (data: AudioData) => {
setAudioData(data);
setError(null);
};
const handleTranscribe = async () => {
if (!audioData || !auth.apiKey) return;
setStatus('processing');
setError(null);
try {
const fileName = audioData.blob instanceof File ? audioData.blob.name : `Recording ${new Date().toLocaleTimeString()}`;
// 1. Create Job
const newJob = await jobProcessor.createJob(auth.apiKey, fileName, audioData.blob);
setCurrentJob(newJob);
refreshJobs(auth.apiKey);
// 2. Start Processing (Async)
jobProcessor.processJob(auth.apiKey, newJob);
} catch (err: any) {
console.error(err);
setError("Failed to start transcription: " + err.message);
setStatus('error');
}
};
const handleSelectJob = (job: Job) => {
setCurrentJob(job);
setIsSidebarOpen(false); // Close sidebar on selection
if (job.status === 'success') {
setStatus('success');
setView('result');
} else if (job.status === 'processing') {
setStatus('processing');
setView('result');
}
};
const handleDeleteJob = async (job: Job) => {
if (!auth.apiKey) return;
if (confirm('Are you sure you want to delete this transcript?')) {
storageUtils.deleteJob(auth.apiKey, job.id);
await dbUtils.deleteAudioBlob(job.id);
refreshJobs(auth.apiKey);
if (currentJob?.id === job.id) {
setCurrentJob(null);
setView('create');
}
}
};
const handleRetryJob = async (job: Job) => {
if (!auth.apiKey) return;
await jobProcessor.retryJob(auth.apiKey, job.id);
setCurrentJob({ ...job, status: 'processing', error: undefined });
setStatus('processing');
setView('result');
setIsSidebarOpen(false);
refreshJobs(auth.apiKey);
};
const handleNewTranscription = () => {
setAudioData(null);
setCurrentJob(null);
setStatus('idle');
setError(null);
setView('create');
setIsSidebarOpen(false);
};
// --- Download Handlers ---
const triggerDownload = (blob: Blob, filename: string) => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
const handleDownloadAudio = async () => {
if (!currentJob?.id) return;
try {
const blob = await dbUtils.getAudioBlob(currentJob.id);
if (blob) {
const ext = blob.type.includes('mp4') ? 'mp4' :
blob.type.includes('wav') ? 'wav' :
blob.type.includes('mpeg') ? 'mp3' : 'webm';
triggerDownload(blob, `recording-${new Date().toISOString().slice(0,10)}.${ext}`);
} else {
alert("Audio file not found in storage.");
}
} catch (e) {
console.error("Error downloading audio", e);
alert("Failed to download audio.");
}
};
const handleDownloadFullJson = () => {
if (!currentJob?.result) return;
const jsonString = JSON.stringify(currentJob.result, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
triggerDownload(blob, `transcription-full-${new Date().toISOString().slice(0,10)}.json`);
};
const handleDownloadCleanJson = () => {
if (!currentJob?.result) return;
// Filter out original_transcript
const cleanSegments = currentJob.result.segments.map(s => ({
speaker: s.speaker,
timestamp: s.timestamp,
semantic_correction: s.semantic_correction,
emotion: s.emotion
}));
const cleanData = {
summary: currentJob.result.summary,
segments: cleanSegments
};
const jsonString = JSON.stringify(cleanData, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
triggerDownload(blob, `transcription-corrected-${new Date().toISOString().slice(0,10)}.json`);
};
if (!auth.isAuthenticated) {
return (
<div className="bg-slate-50 dark:bg-slate-950 min-h-screen transition-colors duration-300">
<Auth />
</div>
);
}
return (
<div className="min-h-screen bg-slate-50 dark:bg-slate-950 text-slate-900 dark:text-slate-100 pb-20 transition-colors duration-300 relative overflow-x-hidden">
{/* Sidebar Drawer */}
<div
className={`fixed inset-0 z-50 transform transition-transform duration-300 ease-in-out ${isSidebarOpen ? 'translate-x-0' : '-translate-x-full'}`}
>
{/* Backdrop */}
<div
className={`absolute inset-0 bg-black/50 transition-opacity duration-300 ${isSidebarOpen ? 'opacity-100' : 'opacity-0 pointer-events-none'}`}
=> setIsSidebarOpen(false)}
/>
{/* Sidebar Content */}
<div className="absolute left-0 top-0 h-full w-80 max-w-[80vw] bg-white dark:bg-slate-900 shadow-2xl flex flex-col border-r border-slate-200 dark:border-slate-800">
<div className="p-4 border-b border-slate-200 dark:border-slate-800 bg-slate-50 dark:bg-slate-950">
<Button
=> setIsSidebarOpen(false)}
variant="secondary"
className="w-full mb-4 justify-center"
icon={<ArrowLeft size={18} />}
>
Back to Home
</Button>
<div className="flex justify-between items-center">
<h2 className="font-semibold text-slate-900 dark:text-white flex items-center">
<History size={18} className="mr-2 text-emerald-500" />
Recent Jobs
</h2>
<button
>
className="text-xs bg-emerald-600 text-white px-2 py-1 rounded hover:bg-emerald-700 transition-colors flex items-center"
>
<Plus size={14} className="mr-1" /> New
</button>
</div>
</div>
<div className="flex-1 overflow-y-auto p-3">
<JobHistory
jobs={jobs}
>
>
>
selectedJobId={currentJob?.id}
/>
</div>
<div className="p-4 border-t border-slate-200 dark:border-slate-800 text-center text-xs text-slate-500">
TeamTaiwan 「聽台灣」智慧會議è
8000
½‰錄
</div>
</div>
</div>
{/* Header */}
<header className="bg-white dark:bg-slate-900 border-b border-slate-200 dark:border-slate-800 sticky top-0 z-10 transition-colors duration-300 shadow-sm">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
<div className="flex items-center space-x-2 sm:space-x-3">
{/* Hamburger Menu Button */}
<button
=> setIsSidebarOpen(true)}
className="p-2 -ml-2 text-slate-500 hover:bg-slate-100 dark:text-slate-400 dark:hover:bg-slate-800 rounded-lg transition-colors focus:outline-none"
title="Menu"
>
<Menu size={24} />
</button>
<div className="flex items-center space-x-2 sm:space-x-3 cursor-pointer" => setView('create')}>
<div className="bg-emerald-500 p-1.5 rounded-lg text-white shadow-sm shrink-0">
<Sparkles size={20} fill="currentColor" />
</div>
<div className="flex items-center text-base sm:text-xl tracking-tight whitespace-nowrap">
<span className="font-bold text-slate-900 dark:text-white mr-1 sm:mr-2">TeamTaiwan</span>
<span className="font-bold text-emerald-600 dark:text-emerald-400">「聽台灣」</span>
</div>
</div>
</div>
<div className="flex items-center space-x-2 sm:space-x-4">
<button
>
className="p-2 text-slate-500 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-slate-800 rounded-lg transition-colors focus:outline-none"
title="Toggle Theme"
>
{isDarkMode ? <Sun size={20} /> : <Moon size={20} />}
</button>
<button
>
className="p-2 text-slate-500 dark:text-slate-400 hover:text-slate-700 dark:hover:text-slate-200 hover:bg-slate-100 dark:hover:bg-slate-800 rounded-lg transition-colors focus:outline-none"
title="Logout"
>
<LogOut size={20} />
</button>
</div>
</div>
</header>
<main className="max-w-5xl mx-auto px-4 sm:px-6 py-10">
{view === 'create' && (
<div className="w-full">
<div className="text-center mb-8 sm:mb-12 mt-4">
<h2 className="text-2xl sm:text-4xl font-bold text-slate-900 dark:text-white mb-3 sm:mb-4 tracking-tight">
台灣語音,AI 全懂
</h2>
<p className="text-base sm:text-lg text-slate-500 dark:text-slate-400 font-medium">
精準捕捉台灣在地語音,一鍵生成完整會議紀錄
</p>
</div>
{/* Input Selection Tabs */}
<div className="bg-white dark:bg-slate-900 p-1 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-800 flex mb-8 max-w-2xl mx-auto">
<button
=> { if (status !== 'processing') { setMode('record'); setAudioData(null); } }}
disabled={status === 'processing'}
className={`flex-1 flex items-center justify-center px-4 sm:px-6 py-3 rounded-xl text-sm font-medium transition-all ${
mode === 'record'
? 'bg-emerald-600 text-white shadow-sm'
: 'text-slate-600 dark:text-slate-400 hover:bg-slate-50 dark:hover:bg-slate-800'
} ${status === 'processing' ? 'opacity-50 cursor-not-allowed' : ''}`}
>
<Mic size={18} className="mr-2" />
<span className="hidden sm:inline">Record Audio</span>
<span className="sm:hidden">Record</span>
</button>
<button
=> { if (status !== 'processing') { setMode('upload'); setAudioData(null); } }}
disabled={status === 'processing'}
className={`flex-1 flex items-center justify-center px-4 sm:px-6 py-3 rounded-xl text-sm font-medium transition-all ${
mode === 'upload'
? 'bg-emerald-600 text-white shadow-sm'
: 'text-slate-600 dark:text-slate-400 hover:bg-slate-50 dark:hover:bg-slate-800'
} ${status === 'processing' ? 'opacity-50 cursor-not-allowed' : ''}`}
>
<Upload size={18} className="mr-2" />
<span className="hidden sm:inline">Upload File</span>
<span className="sm:hidden">Upload</span>
</button>
</div>
<div className="bg-white dark:bg-slate-900 rounded-3xl shadow-sm border border-slate-200 dark:border-slate-800 p-6 sm:p-12 transition-colors duration-300">
{mode === 'record' ? (
<AudioRecorder disabled={status === 'processing'} />
) : (
<FileUploader disabled={status === 'processing'} />
)}
{audioData && (
<div className="mt-8 pt-6 border-t border-slate-100 dark:border-slate-800 flex justify-end">
<Button
isLoading={status === 'processing'}
className="w-full sm:w-auto px-8 py-3 text-lg"
icon={<Sparkles size={20} />}
>
Start Transcription
</Button>
</div>
)}
</div>
{error && (
<div className="mt-6 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-xl p-4 text-red-700 dark:text-red-400 text-center">
{error}
</div>
)}
</div>
)}
{(view === 'result' && currentJob) && (
<div className="animate-in fade-in duration-300 w-full">
{status === 'processing' && !currentJob.result ? (
<div className="bg-white dark:bg-slate-900 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-800 p-8 sm:p-12 text-center">
<div className="flex justify-center mb-8">
<div className="relative">
<div className="w-16 h-16 sm:w-20 sm:h-20 border-4 border-emerald-100 dark:border-emerald-900 border-t-emerald-600 dark:border-t-emerald-500 rounded-full animate-spin"></div>
<div className="absolute top-0 left-0 w-full h-full flex items-center justify-center">
<Sparkles size={24} className="text-emerald-600 dark:text-emerald-400 animate-pulse" />
</div>
</div>
</div>
<h3 className="text-xl sm:text-2xl font-bold text-slate-900 dark:text-white mb-2">Processing "{currentJob.fileName}"</h3>
<p className="text-sm sm:text-base text-slate-500 dark:text-slate-400 max-w-md mx-auto mb-6">
We are splitting the audio, identifying speakers, and generating a polished transcript. This may take a while for long files.
</p>
<div className="inline-block bg-emerald-50 dark:bg-emerald-900/30 text-emerald-700 dark:text-emerald-300 px-4 py-2 rounded-full text-xs sm:text-sm font-medium">
You can close this tab and come back later if you enabled "Keep me logged in".
</div>
</div>
) : status === 'error' ? (
<div className="bg-white dark:bg-slate-900 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-800 p-8 sm:p-12 text-center">
<div className="inline-flex bg-red-100 dark:bg-red-900/30 p-4 rounded-full text-red-600 dark:text-red-400 mb-6">
<span className="text-4xl">⚠️</span>
</div>
<h3 className="text-xl sm:text-2xl font-bold text-slate-900 dark:text-white mb-2">Transcription Failed</h3>
<p className="text-red-600 dark:text-red-400 mb-6">{error}</p>
<Button => handleRetryJob(currentJob)} variant="secondary">Try Again</Button>
</div>
) : (
<div className="space-y-6">
{status === 'processing' && (
<div className="bg-emerald-50 dark:bg-emerald-900/30 text-emerald-700 dark:text-emerald-300 px-4 py-3 rounded-xl flex items-center border border-emerald-100 dark:border-emerald-800 animate-pulse">
<Loader2 size={18} className="animate-spin mr-2" />
<span className="text-sm font-medium">Processing remaining audio chunks... The transcript will update automatically.</span>
</div>
)}
<div className="flex flex-col sm:flex-row justify-between items-start sm:items-center bg-white dark:bg-slate-900 p-4 rounded-xl border border-slate-200 dark:border-slate-800 gap-4">
<div>
<h2 className="text-lg sm:text-xl font-bold text-slate-900 dark:text-white truncate max-w-[200px] sm:max-w-xs">{currentJob.fileName}</h2>
<p className="text-sm text-slate-500 dark:text-slate-400">Started on {new Date(currentJob.createdAt).toLocaleDateString()}</p>
</div>
<div className="flex flex-wrap gap-2 w-full sm:w-auto">
<Button variant="secondary" className="text-xs px-3 flex-1 sm:flex-none" icon={<FileAudio size={14}/>}>Audio</Button>
{currentJob.result && (
<>
<Button variant="secondary" className="text-xs px-3 flex-1 sm:flex-none" icon={<FileJson size={14}/>}>Full</Button>
<Button variant="secondary" className="text-xs px-3 flex-1 sm:flex-none" icon={<FileText size={14}/>}>Clean</Button>
</>
)}
<Button variant="primary" className="text-xs px-3 flex-1 sm:flex-none" icon={<Plus size={14}/>}>New Job</Button>
</div>
</div>
{currentJob.result && (
<TranscriptionDisplay
data={currentJob.result}
isProcessing={status === 'processing'}
/>
)}
</div>
)}
</div>
)}
{/* Footer */}
<div className="mt-12 pt-6 text-center border-t border-slate-200 dark:border-slate-800">
<div className="text-xs text-slate-400 dark:text-slate-500">
<span className="font-medium">Powered by NCHC GenAI Team</span>
<span className="mx-2">·</span>
<a href="https://hackmd.io/@whYPD8MBSHWRZV6y-ymFwQ/Sy1d2eN4-l" target="_blank" rel="noreferrer" className="hover:text-emerald-600 dark:hover:text-emerald-400 transition-colors inline-flex items-center">
User Manual <ExternalLink size={10} className="ml-0.5" />
</a>
</div>
</div>
</main>
</div>
);
}
export default App;
You can’t perform that action at this time.