|
1 | 1 | import React, { useState, useEffect } from 'react'; |
2 | 2 | import { useAuth } from '../contexts/AuthContext'; |
3 | | -import { getFirestore, collection, query, onSnapshot, doc } from 'firebase/firestore'; |
| 3 | +import { getFirestore, collection, query, onSnapshot, doc, getDoc } from 'firebase/firestore'; |
4 | 4 | import { getFunctions, httpsCallable } from 'firebase/functions'; |
5 | 5 | import DOCUMENT_TYPES, { DocumentType } from '../constants/documentTypes'; |
6 | 6 |
|
@@ -192,10 +192,71 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP |
192 | 192 | return progress; |
193 | 193 | }; |
194 | 194 |
|
| 195 | + // Function to inspect document data |
| 196 | + const inspectDocumentData = async (documentId: string) => { |
| 197 | + if (!currentUser) return; |
| 198 | + |
| 199 | + setStatus(`Inspecting document ${documentId}...`); |
| 200 | + |
| 201 | + try { |
| 202 | + // Get document from Firestore |
| 203 | + const db = getFirestore(); |
| 204 | + const docRef = doc(db, 'users', currentUser.uid, 'documents', documentId); |
| 205 | + const docSnap = await getDoc(docRef); |
| 206 | + |
| 207 | + if (docSnap.exists()) { |
| 208 | + const data = docSnap.data(); |
| 209 | + console.log("===== DOCUMENT DATA ====="); |
| 210 | + console.log(`Document ID: ${documentId}`); |
| 211 | + console.log(`Type: ${data.documentType}`); |
| 212 | + console.log(`Status: ${data.status}`); |
| 213 | + console.log(`Name: ${data.name}`); |
| 214 | + |
| 215 | + // Log text data if available (limited to first 500 chars for readability) |
| 216 | + if (data.text) { |
| 217 | + console.log(`Text length: ${data.text.length} characters`); |
| 218 | + console.log("First 500 characters:"); |
| 219 | + console.log(data.text.substring(0, 500) + "..."); |
| 220 | + } |
| 221 | + |
| 222 | + setStatus(`Document data logged to console for ${documentId}`); |
| 223 | + } else { |
| 224 | + setError(`Document not found: ${documentId}`); |
| 225 | + } |
| 226 | + } catch (err: any) { |
| 227 | + console.error('Error inspecting document:', err); |
| 228 | + setError(`Inspection failed: ${err.message || 'Unknown error'}`); |
| 229 | + } |
| 230 | + }; |
| 231 | + |
| 232 | + // Function to inspect formatted data |
| 233 | + const inspectFormattedData = async () => { |
| 234 | + if (!currentUser) return; |
| 235 | + |
| 236 | + setStatus('Fetching formatted data...'); |
| 237 | + |
| 238 | + try { |
| 239 | + const db = getFirestore(); |
| 240 | + const formattedDataRef = doc(db, 'users', currentUser.uid, 'data', 'formatted_data'); |
| 241 | + const docSnap = await getDoc(formattedDataRef); |
| 242 | + |
| 243 | + if (docSnap.exists()) { |
| 244 | + const data = docSnap.data(); |
| 245 | + console.log("===== FORMATTED DATA ====="); |
| 246 | + console.log(JSON.stringify(data.formatted_data, null, 2)); |
| 247 | + setStatus('Formatted data logged to console'); |
| 248 | + } else { |
| 249 | + console.log("No formatted data document found"); |
| 250 | + setError('No formatted data available'); |
| 251 | + } |
| 252 | + } catch (err: any) { |
| 253 | + console.error('Error fetching formatted data:', err); |
| 254 | + setError(`Failed to fetch formatted data: ${err.message || 'Unknown error'}`); |
| 255 | + } |
| 256 | + }; |
| 257 | + |
195 | 258 | const progress = calculateProgress(); |
196 | 259 | const canFormat = documentCounts.extracted > 0 && !isFormatting; |
197 | | - // We'll keep this comment to document what we're checking, but remove the unused variable |
198 | | - // const hasErrors = documentCounts.error > 0; |
199 | 260 |
|
200 | 261 | return ( |
201 | 262 | <div style={styles.container}> |
@@ -265,13 +326,21 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP |
265 | 326 | </div> |
266 | 327 |
|
267 | 328 | {canFormat && ( |
268 | | - <button |
269 | | - onClick={handleFormatDocuments} |
270 | | - style={styles.formatButton} |
271 | | - disabled={isFormatting} |
272 | | - > |
273 | | - {isFormatting ? 'Formatting...' : 'Format Documents'} |
274 | | - </button> |
| 329 | + <div> |
| 330 | + <button |
| 331 | + onClick={handleFormatDocuments} |
| 332 | + style={styles.formatButton} |
| 333 | + disabled={isFormatting} |
| 334 | + > |
| 335 | + {isFormatting ? 'Formatting...' : 'Format Documents'} |
| 336 | + </button> |
| 337 | + <button |
| 338 | + onClick={inspectFormattedData} |
| 339 | + style={{...styles.formatButton, backgroundColor: '#666', marginLeft: '10px'}} |
| 340 | + > |
| 341 | + Inspect Formatted Data |
| 342 | + </button> |
| 343 | + </div> |
275 | 344 | )} |
276 | 345 |
|
277 | 346 | {documents.length > 0 && ( |
@@ -302,22 +371,30 @@ const DocumentProcessingStatus: React.FC<DocumentProcessingStatusProps> = ({ onP |
302 | 371 | </span> |
303 | 372 | </td> |
304 | 373 | <td style={styles.tableCell}> |
305 | | - {doc.status?.toLowerCase() === 'uploaded' && ( |
| 374 | + <div style={{ display: 'flex', gap: '10px' }}> |
| 375 | + {doc.status?.toLowerCase() === 'uploaded' && ( |
| 376 | + <button |
| 377 | + onClick={() => handleRetryProcessing(doc.id)} |
| 378 | + style={styles.actionButton} |
| 379 | + > |
| 380 | + Process Document |
| 381 | + </button> |
| 382 | + )} |
| 383 | + {doc.status?.toLowerCase() === 'error' && ( |
| 384 | + <button |
| 385 | + onClick={() => handleRetryProcessing(doc.id)} |
| 386 | + style={styles.actionButton} |
| 387 | + > |
| 388 | + Retry Processing |
| 389 | + </button> |
| 390 | + )} |
306 | 391 | <button |
307 | | - onClick={() => handleRetryProcessing(doc.id)} |
308 | | - style={styles.actionButton} |
| 392 | + onClick={() => inspectDocumentData(doc.id)} |
| 393 | + style={{...styles.actionButton, backgroundColor: '#666'}} |
309 | 394 | > |
310 | | - Process Document |
| 395 | + Inspect |
311 | 396 | </button> |
312 | | - )} |
313 | | - {doc.status?.toLowerCase() === 'error' && ( |
314 | | - <button |
315 | | - onClick={() => handleRetryProcessing(doc.id)} |
316 | | - style={styles.actionButton} |
317 | | - > |
318 | | - Retry Processing |
319 | | - </button> |
320 | | - )} |
| 397 | + </div> |
321 | 398 | {doc.error && ( |
322 | 399 | <div style={styles.errorMessage}> |
323 | 400 | Error: {doc.error} |
|
0 commit comments