Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
190 lines (168 loc) · 5.35 KB
/
Copy pathApp.tsx
File metadata and controls
190 lines (168 loc) · 5.35 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
import React, { useState, useCallback, useEffect } from 'react';
import { ExtractionMode } from './types';
import { extractRawTextFromImage, structureTextFromText, generatePromptFromImage } from './services/geminiService';
import ModeSelector from './components/ModeSelector';
import Extractor from './components/Extractor';
import ResultDisplay from './components/ResultDisplay';
import Header from './components/Header';
import Footer from './components/Footer';
import CorrectionStep from './components/CorrectionStep';
const App: React.FC = () => {
const [mode, setMode] = useState<ExtractionMode | null>(null);
const [imageData, setImageData] = useState<string | null>(null);
const [ocrText, setOcrText] = useState<string | null>(null);
const [result, setResult] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const handleModeSelect = (selectedMode: ExtractionMode) => {
setMode(selectedMode);
handleResetStateForModeChange();
};
const resetCommonState = () => {
setImageData(null);
setResult(null);
setError(null);
setIsLoading(false);
setOcrText(null);
}
const handleResetStateForModeChange = () => {
resetCommonState();
};
const handleImagePaste = (dataUrl: string) => {
resetCommonState();
setImageData(dataUrl);
};
const handleOcrRequest = useCallback(async () => {
if (!imageData) return;
setIsLoading(true);
setError(null);
setOcrText(null);
try {
const base64Data = imageData.split(',')[1];
const rawText = await extractRawTextFromImage(base64Data);
setOcrText(rawText);
} catch (e: any) {
console.error("OCR failed:", e);
setError('Die Texterkennung ist fehlgeschlagen. Bitte versuchen Sie es erneut.');
} finally {
setIsLoading(false);
}
}, [imageData]);
const handleStructureRequest = useCallback(async () => {
if (!ocrText) return;
setIsLoading(true);
setError(null);
setResult(null);
try {
const structuredText = await structureTextFromText(ocrText);
setResult(structuredText);
} catch (e: any) {
console.error("Structuring failed:", e);
setError('Die Strukturierung des Textes ist fehlgeschlagen. Bitte versuchen Sie es erneut.');
} finally {
setIsLoading(false);
}
}, [ocrText]);
const handlePromptRequest = useCallback(async () => {
if (!imageData) return;
setIsLoading(true);
setError(null);
setResult(null);
try {
const base64Data = imageData.split(',')[1];
const responseText = await generatePromptFromImage(base64Data);
setResult(responseText);
} catch (e: any) {
console.error("Prompt generation failed:", e);
setError('Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.');
} finally {
setIsLoading(false);
}
}, [imageData]);
const handleReset = () => {
setMode(null);
resetCommonState();
};
const handleNewImage = () => {
resetCommonState();
};
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
handleReset();
}
};
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, []);
const renderContent = () => {
if (!mode) {
return <ModeSelector onSelect={handleModeSelect} />;
}
if (mode === ExtractionMode.PROMPT) {
return (
<div className="w-full max-w-2xl">
<Extractor
mode={mode}
imageData={imageData}
onImagePaste={handleImagePaste}
onExtract={handlePromptRequest}
onNewImage={handleNewImage}
isLoading={isLoading}
/>
<ResultDisplay
isLoading={isLoading && !result}
result={result}
error={error}
mode={mode}
/>
</div>
);
}
if (mode === ExtractionMode.TEXT) {
return (
<div className="w-full max-w-2xl">
{!ocrText && !result && (
<Extractor
mode={mode}
imageData={imageData}
onImagePaste={handleImagePaste}
onExtract={handleOcrRequest}
onNewImage={handleNewImage}
isLoading={isLoading}
/>
)}
{ocrText && !result && (
<CorrectionStep
ocrText={ocrText}
onTextChange={setOcrText}
onStructure={handleStructureRequest}
isLoading={isLoading}
onNewImage={handleNewImage}
/>
)}
<ResultDisplay
isLoading={isLoading && !result && !!ocrText}
result={result}
error={error}
mode={mode}
/>
</div>
)
}
};
return (
<div className="min-h-screen text-light flex flex-col items-center justify-center p-4 font-sans">
<div className="w-full max-w-4xl mx-auto flex flex-col flex-grow">
<Header onReset={handleReset} />
<main className="flex-grow flex flex-col items-center justify-center text-center p-4 sm:p-6 md:p-8">
{renderContent()}
</main>
<Footer />
</div>
</div>
);
};
export default App;