import React from 'react'
import {
DocumentIcon,
PhotoIcon,
ArrowDownTrayIcon,
} from '@heroicons/react/24/outline'
import { Document, documentService } from '../services/api'
interface DocumentListProps {
documents: Document[]
loading: boolean
}
function DocumentList({ documents, loading }: DocumentListProps) {
const handleDownload = async (document: Document) => {
try {
const response = await documentService.download(document.id)
const blob = new Blob([response.data])
const url = window.URL.createObjectURL(blob)
const link = window.document.createElement('a')
link.href = url
link.download = document.original_filename
link.click()
window.URL.revokeObjectURL(url)
} catch (error) {
console.error('Download failed:', error)
}
}
const getFileIcon = (mimeType: string) => {
if (mimeType.startsWith('image/')) {
return
}
return
}
const formatFileSize = (bytes: number) => {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
const getOcrStatusBadge = (document: Document) => {
if (!document.has_ocr_text) {
return null
}
const confidence = document.ocr_confidence
const status = document.ocr_status
if (status === 'failed') {
return (
OCR Failed
)
}
if (status === 'processing') {
return (
Processing...
)
}
if (confidence !== undefined) {
let badgeClass = 'bg-green-100 text-green-800'
let label = 'OCR'
if (confidence >= 80) {
badgeClass = 'bg-green-100 text-green-800'
label = `OCR ${confidence.toFixed(0)}%`
} else if (confidence >= 60) {
badgeClass = 'bg-yellow-100 text-yellow-800'
label = `OCR ${confidence.toFixed(0)}%`
} else {
badgeClass = 'bg-orange-100 text-orange-800'
label = `OCR ${confidence.toFixed(0)}%`
}
return (
{label}
)
}
return (
OCR
)
}
const getOcrMetrics = (document: Document) => {
if (!document.has_ocr_text || !document.ocr_word_count) {
return null
}
const metrics = []
if (document.ocr_word_count) {
metrics.push(`${document.ocr_word_count} words`)
}
if (document.ocr_processing_time_ms) {
const seconds = (document.ocr_processing_time_ms / 1000).toFixed(1)
metrics.push(`${seconds}s`)
}
return metrics.length > 0 ? ` • ${metrics.join(' • ')}` : null
}
if (loading) {
return (
)
}
if (documents.length === 0) {
return (
)
}
return (
{documents.map((document) => (
-
{getFileIcon(document.mime_type)}
{document.original_filename}
{formatFileSize(document.file_size)} • {document.mime_type}
{getOcrMetrics(document)}
{getOcrStatusBadge(document)}
{new Date(document.created_at).toLocaleDateString()}
))}
)
}
export default DocumentList