File size: 9,786 Bytes
582c857
 
 
 
e4fb177
582c857
 
 
 
 
 
e4fb177
d488595
 
582c857
 
 
e4fb177
 
582c857
 
 
 
e4fb177
 
582c857
 
 
 
 
 
e4fb177
 
582c857
 
e4fb177
 
 
582c857
e4fb177
 
 
 
 
 
 
582c857
e4fb177
 
 
 
 
 
 
582c857
 
d488595
582c857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b90a9db
582c857
 
 
 
 
 
 
 
 
 
daddb50
582c857
 
 
 
 
 
 
 
 
 
 
 
daddb50
582c857
 
 
 
 
 
 
a360593
582c857
 
 
 
 
 
e4fb177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582c857
 
daddb50
582c857
 
 
 
 
 
 
 
 
 
 
 
 
daddb50
 
582c857
 
 
 
 
 
e4fb177
582c857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a360593
582c857
 
 
 
 
 
 
 
 
a360593
582c857
 
 
 
 
 
a360593
582c857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d488595
 
 
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
// frontend/src/pages/Dashboard.jsx

import React, { useState } from "react";
import { motion } from "framer-motion";
import { Sparkles, Zap, FileText, TrendingUp, Clock, AlertCircle } from "lucide-react";
import { Button } from "@/components/ui/button";
import UploadZone from "@/components/ocr/UploadZone";
import DocumentPreview from "@/components/ocr/DocumentPreview";
import ExtractionOutput from "@/components/ocr/ExtractionOutput";
import ExportButtons from "@/components/ExportButtons";
import ProcessingStatus from "@/components/ocr/ProcessingStatus";
import { extractDocument } from "@/services/api";

export default function Dashboard() {
  const [selectedFile, setSelectedFile] = useState(null);
  const [isProcessing, setIsProcessing] = useState(false);
  const [isComplete, setIsComplete] = useState(false);
  const [extractionResult, setExtractionResult] = useState(null);
  const [error, setError] = useState(null);

  const handleFileSelect = (file) => {
    setSelectedFile(file);
    setIsComplete(false);
    setExtractionResult(null);
    setError(null);
  };

  const handleClear = () => {
    setSelectedFile(null);
    setIsProcessing(false);
    setIsComplete(false);
    setExtractionResult(null);
    setError(null);
  };

  const handleExtract = async () => {
    if (!selectedFile) return;

    setIsProcessing(true);
    setIsComplete(false);
    setError(null);
    setExtractionResult(null);

    try {
      const result = await extractDocument(selectedFile);
      setExtractionResult(result);
      setIsComplete(true);
    } catch (err) {
      console.error("Extraction error:", err);
      setError(err.message || "Failed to extract document. Please try again.");
      setIsComplete(false);
    } finally {
      setIsProcessing(false);
    }
  };

  return (
    <div className="min-h-screen bg-[#FAFAFA]">
      {/* Header */}
      <header className="bg-white border-b border-slate-200/80 sticky top-0 z-40">
        <div className="px-8 py-4 flex items-center justify-between">
          <div>
            <h1 className="text-xl font-bold text-slate-900 tracking-tight">
              Document Extraction
            </h1>
            <p className="text-sm text-slate-500 mt-0.5">
              Upload any document and extract structured data with AI
            </p>
          </div>
          <div className="flex items-center gap-3">
            {/* Stats Pills */}
            <div className="hidden lg:flex items-center gap-2">
              <div className="flex items-center gap-2 px-3 py-1.5 bg-slate-100 rounded-lg">
                <FileText className="h-4 w-4 text-slate-500" />
                <span className="text-sm font-medium text-slate-700">
                  247 Extracted
                </span>
              </div>
              <div className="flex items-center gap-2 px-3 py-1.5 bg-emerald-50 rounded-lg">
                <TrendingUp className="h-4 w-4 text-emerald-600" />
                <span className="text-sm font-medium text-emerald-700">
                  98.5% Accuracy
                </span>
              </div>
            </div>

            <ExportButtons isComplete={isComplete} extractionResult={extractionResult} />
          </div>
        </div>
      </header>

      {/* Main Content */}
      <div className="p-8">
        {/* Upload Section */}
        <motion.div
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          className="max-w-3xl mx-auto mb-4"
        >
          <UploadZone
            onFileSelect={handleFileSelect}
            selectedFile={selectedFile}
            onClear={handleClear}
          />

          {/* Extract Button */}
          {selectedFile && !isProcessing && !isComplete && (
            <motion.div
              initial={{ opacity: 0, y: 10 }}
              animate={{ opacity: 1, y: 0 }}
              className="mt-4 flex justify-center"
            >
              <Button
                onClick={handleExtract}
                size="lg"
                className="h-14 px-8 rounded-2xl font-semibold text-base bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-700 hover:to-violet-700 shadow-xl shadow-indigo-500/25 hover:shadow-2xl hover:shadow-indigo-500/30 transition-all duration-300 hover:-translate-y-0.5"
              >
                <Sparkles className="h-5 w-5 mr-2" />
                Start Extraction
                <Zap className="h-4 w-4 ml-2 opacity-70" />
              </Button>
            </motion.div>
          )}
        </motion.div>

        {/* Error Message */}
        {error && (
          <motion.div
            initial={{ opacity: 0, y: -10 }}
            animate={{ opacity: 1, y: 0 }}
            className="max-w-3xl mx-auto mb-6"
          >
            <div className="bg-red-50 border border-red-200 rounded-2xl p-4 flex items-start gap-3">
              <AlertCircle className="h-5 w-5 text-red-600 flex-shrink-0 mt-0.5" />
              <div className="flex-1">
                <h3 className="font-semibold text-red-900 mb-1">Extraction Failed</h3>
                <p className="text-sm text-red-700">{error}</p>
              </div>
              <button
                onClick={() => setError(null)}
                className="text-red-400 hover:text-red-600 transition-colors"
              >
                ×
              </button>
            </div>
          </motion.div>
        )}

        {/* Processing Status */}
        {(isProcessing || isComplete) && (
          <div className="max-w-3xl mx-auto mb-4">
            <ProcessingStatus
              isProcessing={isProcessing}
              isComplete={isComplete}
            />
          </div>
        )}

        {/* Split View */}
        {selectedFile && (
          <motion.div
            initial={{ opacity: 0, y: 20 }}
            animate={{ opacity: 1, y: 0 }}
            transition={{ delay: 0.2 }}
            className="grid grid-cols-1 lg:grid-cols-2 gap-4"
            style={{ height: "calc(100vh - 320px)", minHeight: "450px" }}
          >
            <DocumentPreview file={selectedFile} isProcessing={isProcessing} />
            <ExtractionOutput
              hasFile={!!selectedFile}
              isProcessing={isProcessing}
              isComplete={isComplete}
              extractionResult={extractionResult}
            />
          </motion.div>
        )}

        {/* Empty State Features */}
        {!selectedFile && (
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            transition={{ delay: 0.3 }}
            className="max-w-5xl mx-auto mt-12"
          >
            <div className="text-center mb-10">
              <h2 className="text-2xl font-bold text-slate-900 mb-2">
                Powered by Advanced AI
              </h2>
              <p className="text-slate-500">
                Extract structured data from any document
              </p>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
              {[
                {
                  icon: Zap,
                  title: "Lightning Fast",
                  description:
                    "Process documents faster with our optimized AI pipeline",
                  color: "amber",
                },
                {
                  icon: Sparkles,
                  title: "98.5% Accuracy",
                  description:
                    "Industry-leading extraction accuracy",
                  color: "indigo",
                },
                {
                  icon: Clock,
                  title: "Any Format",
                  description:
                    "Support for PDF, images, spreadsheets, and scanned documents",
                  color: "emerald",
                },
              ].map((feature, index) => (
                <motion.div
                  key={feature.title}
                  initial={{ opacity: 0, y: 20 }}
                  animate={{ opacity: 1, y: 0 }}
                  transition={{ delay: 0.4 + index * 0.1 }}
                  className="group bg-white rounded-2xl border border-slate-200 p-6 hover:shadow-xl hover:shadow-slate-200/50 transition-all duration-300 hover:-translate-y-1"
                >
                  <div
                    className={`h-12 w-12 rounded-xl bg-${feature.color}-50 flex items-center justify-center mb-4 group-hover:scale-110 transition-transform duration-300`}
                  >
                    <feature.icon
                      className={`h-6 w-6 text-${feature.color}-600`}
                    />
                  </div>
                  <h3 className="font-semibold text-slate-900 mb-2">
                    {feature.title}
                  </h3>
                  <p className="text-sm text-slate-500 leading-relaxed">
                    {feature.description}
                  </p>
                </motion.div>
              ))}
            </div>

            {/* Supported Formats */}
            <div className="mt-12 text-center">
              <p className="text-xs text-slate-400 uppercase tracking-wider mb-4 font-medium">
                Supported Formats
              </p>
              <div className="flex items-center justify-center gap-6 flex-wrap">
                {["PDF", "PNG", "JPG", "TIFF", "DOCX", "XLSX"].map((format) => (
                  <div
                    key={format}
                    className="flex items-center gap-2 text-slate-400"
                  >
                    <FileText className="h-4 w-4" />
                    <span className="text-sm font-medium">{format}</span>
                  </div>
                ))}
              </div>
            </div>
          </motion.div>
        )}
      </div>
    </div>
  );
}