File size: 14,379 Bytes
373c769 |
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 |
import React, { useState, useCallback, useRef, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useDropzone } from 'react-dropzone';
import FlowingMenu from './components/FlowingMenu';
import FileUploader from './components/FileUploader';
import PreviewPanel from './components/PreviewPanel';
import ResultsPanel from './components/ResultsPanel';
import Orb from './components/Orb';
import TrueFocus from './components/TrueFocus';
import LoadingScreen from './components/LoadingScreen';
import ProcessingProgress from './components/ProcessingProgress';
import ApiKeyEncryption from './utils/encryption';
import './index.css';
function App() {
const [apiKey, setApiKey] = useState(() => {
// Load encrypted API key from localStorage on initialization
return ApiKeyEncryption.retrieveApiKey() || '';
});
// Handle API key changes and save encrypted to localStorage
const handleApiKeyChange = (newApiKey) => {
setApiKey(newApiKey);
// Store encrypted API key
if (newApiKey.trim()) {
ApiKeyEncryption.storeApiKey(newApiKey);
console.log('🔐 API key encrypted and stored securely');
} else {
ApiKeyEncryption.clearApiKey();
console.log('🗑️ Encrypted API key cleared');
}
};
const [uploadedFile, setUploadedFile] = useState(null);
const [extractedText, setExtractedText] = useState('');
const [isProcessing, setIsProcessing] = useState(false);
const [processingProgress, setProcessingProgress] = useState({ current: 0, total: 0, status: '', fileName: '' });
const [processingMode, setProcessingMode] = useState('standard');
const [activeTab, setActiveTab] = useState('upload');
const [previewMode, setPreviewMode] = useState('text');
const [showLoading, setShowLoading] = useState(true);
const fileInputRef = useRef(null);
// Check if server has API key configured
const [serverHasApiKey, setServerHasApiKey] = useState(false);
// Migrate old unencrypted API key to encrypted storage
useEffect(() => {
const migrateOldApiKey = () => {
const oldApiKey = localStorage.getItem('gemini-api-key');
if (oldApiKey && !ApiKeyEncryption.retrieveApiKey()) {
console.log('🔄 Migrating old API key to encrypted storage...');
ApiKeyEncryption.storeApiKey(oldApiKey);
localStorage.removeItem('gemini-api-key'); // Remove old unencrypted key
setApiKey(oldApiKey);
console.log('✅ API key migration completed');
}
};
migrateOldApiKey();
}, []);
// Cleanup temp files on app load/reload and check server API key
useEffect(() => {
const initializeApp = async () => {
try {
// Check if server has API key configured
const healthResponse = await fetch('http://localhost:3002/api/health');
if (healthResponse.ok) {
const healthData = await healthResponse.json();
setServerHasApiKey(healthData.hasApiKey || false);
}
// Cleanup temp files
await fetch('http://localhost:3002/api/cleanup', { method: 'POST' });
console.log('✅ Cleanup completed on app load');
} catch (error) {
console.log('⚠️ App initialization failed (server might be starting):', error.message);
}
};
// Run initialization after a short delay to ensure server is ready
setTimeout(initializeApp, 2000);
}, []);
const menuItems = [
{ id: 'upload', label: 'Upload', icon: '↑' },
{ id: 'preview', label: 'Preview', icon: '○' },
{ id: 'results', label: 'RESULTS', icon: '↓' }
];
const onDrop = useCallback(async (acceptedFiles) => {
const file = acceptedFiles[0];
if (file) {
// Trigger cleanup before processing new file
try {
await fetch('http://localhost:3002/api/cleanup', { method: 'POST' });
console.log('✅ Pre-upload cleanup completed');
} catch (error) {
console.log('⚠️ Pre-upload cleanup failed:', error.message);
}
setUploadedFile(file);
setActiveTab('preview');
}
}, []);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: {
'image/*': ['.png', '.jpg', '.jpeg'],
'application/pdf': ['.pdf'],
'text/html': ['.html', '.htm']
},
multiple: false
});
const handlePaste = useCallback((e) => {
const items = e.clipboardData?.items;
if (items) {
for (let item of items) {
if (item.type.indexOf('image') !== -1) {
const file = item.getAsFile();
if (file) {
setUploadedFile(file);
setActiveTab('preview');
}
}
}
}
}, []);
const handleFileSelect = () => {
fileInputRef.current?.click();
};
const handleFileChange = (e) => {
const file = e.target.files?.[0];
if (file) {
setUploadedFile(file);
setActiveTab('preview');
}
};
const processFile = async () => {
if (!uploadedFile || !apiKey) return;
setIsProcessing(true);
// Initialize progress
setProcessingProgress({
current: 0,
total: 1,
status: '🔄 Initializing...',
fileName: uploadedFile.name
});
let progressInterval = null;
try {
// Create FormData for file upload
const formData = new FormData();
formData.append('file', uploadedFile);
formData.append('apiKey', apiKey);
formData.append('mode', processingMode);
console.log('Processing file:', uploadedFile.name, 'Mode:', processingMode);
// Start the OCR request
const ocrPromise = fetch('http://localhost:3002/api/ocr', {
method: 'POST',
body: formData,
});
// Start progress polling immediately
progressInterval = setInterval(async () => {
try {
// We'll get the sessionId from the response, but for now poll a generic endpoint
// In a real implementation, you'd start polling after getting the sessionId
const progressResponse = await fetch(`http://localhost:3002/api/progress-latest`);
if (progressResponse.ok) {
const progressData = await progressResponse.json();
// Progress data received and processed
if (progressData.current > 0) {
setProcessingProgress({
current: progressData.current,
total: progressData.total,
status: progressData.status,
fileName: progressData.fileName || uploadedFile.name,
totalPages: progressData.totalPages || 1,
currentPage: progressData.currentPage || 1,
totalCharacters: progressData.totalCharacters || 0,
pageCharacters: progressData.pageCharacters || 0,
phase: progressData.phase || 'processing',
consoleLogs: progressData.consoleLogs || []
});
}
}
} catch (error) {
// Ignore progress polling errors
console.log('Progress polling error (ignored):', error.message);
}
}, 1000); // Poll every 1 second to reduce spam
// Wait for OCR to complete
const response = await ocrPromise;
const result = await response.json();
// Clear progress polling
if (progressInterval) {
clearInterval(progressInterval);
progressInterval = null;
}
if (result.success) {
// Final progress update
setProcessingProgress(prev => ({
...prev,
current: prev.total,
status: '✅ Processing complete!'
}));
await new Promise(resolve => setTimeout(resolve, 500));
// Use the extracted text from Gemini
setExtractedText(result.data.extractedText);
setActiveTab('results');
console.log('✅ OCR Success:', {
fileName: result.data.fileName,
characters: result.data.metadata.characterCount,
words: result.data.metadata.wordCount,
mode: result.data.processingMode
});
} else {
console.error('❌ OCR Error:', result.error);
alert(`OCR Error: ${result.error}`);
}
} catch (error) {
console.error('❌ Network Error:', error);
// Clear progress polling on error
if (progressInterval) {
clearInterval(progressInterval);
progressInterval = null;
}
// Check if backend is running
if (error.message.includes('fetch')) {
alert(`Network Error: Cannot connect to OCR backend.
Please make sure:
1. Backend server is running on port 3002
2. Run: cd server && npm install && npm start
3. Check console for any backend errors
4. Visit http://localhost:3002 to verify backend is running
Error: ${error.message}`);
} else {
alert(`Processing Error: ${error.message}`);
}
} finally {
// Clean up
if (progressInterval) {
clearInterval(progressInterval);
}
setIsProcessing(false);
setProcessingProgress({ current: 0, total: 0, status: '', fileName: '' });
}
};
return (
<div className="app" onPaste={handlePaste}>
{/* Loading Screen */}
{showLoading && (
<LoadingScreen onComplete={() => setShowLoading(false)} />
)}
{/* Giant Central Orb - Fades in after loading */}
<div className={`giant-orb-background ${!showLoading ? 'loaded' : ''}`}>
<div className="giant-orb-container">
<Orb hue={0} hoverIntensity={0.5} rotateOnHover={true} forceHoverState={false} />
</div>
</div>
<div className="glass-container">
<header className="app-header">
<motion.div
className="logo"
initial={{ opacity: 0, y: -20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.8 }}
>
<TrueFocus
sentence="Luna OCR"
manualMode={true}
blurAmount={8.5}
borderColor="#ffffff"
glowColor="rgba(255, 255, 255, 0.8)"
animationDuration={0.5}
pauseBetweenAnimations={2}
/>
</motion.div>
<motion.div
className="api-key-input"
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
transition={{ duration: 0.8, delay: 0.2 }}
>
{!serverHasApiKey && (
<div className="api-key-container">
<input
type="password"
placeholder={apiKey ? "API Key loaded from storage" : "Enter Google API Key..."}
value={apiKey}
onChange={(e) => handleApiKeyChange(e.target.value)}
className="glass-input"
/>
{apiKey && (
<button
type="button"
onClick={() => handleApiKeyChange('')}
className="clear-api-key-btn"
title="Clear saved API key"
>
×
</button>
)}
</div>
)}
{serverHasApiKey && (
<div className="server-api-notice">
<p>🔒 API key configured on server - no user key required</p>
</div>
)}
</motion.div>
</header>
<FlowingMenu
items={menuItems}
activeItem={activeTab}
onItemClick={setActiveTab}
/>
<main className="app-main">
<AnimatePresence mode="wait">
{activeTab === 'upload' && (
<motion.div
key="upload"
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 20 }}
className="tab-content"
>
<FileUploader
getRootProps={getRootProps}
getInputProps={getInputProps}
isDragActive={isDragActive}
onFileSelect={handleFileSelect}
fileInputRef={fileInputRef}
onFileChange={handleFileChange}
/>
</motion.div>
)}
{activeTab === 'preview' && uploadedFile && (
<motion.div
key="preview"
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 20 }}
className="tab-content"
>
<PreviewPanel
file={uploadedFile}
processingMode={processingMode}
onModeChange={setProcessingMode}
onProcess={processFile}
isProcessing={isProcessing}
apiKey={apiKey}
/>
</motion.div>
)}
{activeTab === 'results' && extractedText && (
<motion.div
key="results"
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 20 }}
className="tab-content"
>
<ResultsPanel
text={extractedText}
previewMode={previewMode}
onPreviewModeChange={setPreviewMode}
fileName={uploadedFile?.name}
/>
</motion.div>
)}
</AnimatePresence>
</main>
</div>
{/* Processing Progress Overlay */}
<ProcessingProgress
progress={processingProgress}
isVisible={isProcessing}
/>
</div>
);
}
export default App; |