import { useState, useEffect } from "react"; import { BifrostOCRResponse, OCRDocument } from "@/lib/types/logs"; import { Button } from "@/components/ui/button"; import { CodeEditor } from "@/components/ui/codeEditor"; import { ChevronLeft, ChevronRight, FileText } from "lucide-react"; function getImageSrc(b64: string): string { if (b64.startsWith("/9j/")) return `data:image/jpeg;base64,${b64}`; if (b64.startsWith("iVBOR")) return `data:image/png;base64,${b64}`; if (b64.startsWith("UklGR")) return `data:image/webp;base64,${b64}`; if (b64.startsWith("R0lGO")) return `data:image/gif;base64,${b64}`; return `data:image/png;base64,${b64}`; } interface OCRViewProps { ocrInput?: OCRDocument; ocrOutput?: BifrostOCRResponse; } export default function OCRView({ ocrInput, ocrOutput }: OCRViewProps) { const pages = ocrOutput?.pages ?? []; const totalPages = pages.length; const [currentIndex, setCurrentIndex] = useState(0); useEffect(() => { if (totalPages === 0) { setCurrentIndex(0); } else { setCurrentIndex((prev) => Math.min(prev, totalPages - 1)); } }, [totalPages]); const goToPrevious = () => setCurrentIndex((prev) => (prev === 0 ? totalPages - 1 : prev - 1)); const goToNext = () => setCurrentIndex((prev) => (prev === totalPages - 1 ? 0 : prev + 1)); const currentPage = pages[currentIndex] ?? null; const pageImages = currentPage?.images?.filter((img) => img.image_base64) ?? []; return (
{/* OCR Input */} {ocrInput && (
OCR Input
TYPE
{ocrInput.type === "document_url" ? "Document" : "Image"}
{(ocrInput.document_url || ocrInput.image_url) && (
{ocrInput.type === "document_url" ? "DOCUMENT URL" : "IMAGE URL"}
{ocrInput.document_url ?? ocrInput.image_url}
)}
)} {/* OCR Output */} {ocrOutput && (
OCR Output
{ocrOutput.usage_info && (
PAGES PROCESSED
{ocrOutput.usage_info.pages_processed}
DOCUMENT SIZE
{(ocrOutput.usage_info.doc_size_bytes / 1024).toFixed(1)} KB
)} {ocrOutput.document_annotation && (
DOCUMENT ANNOTATION
{ocrOutput.document_annotation}
)} {currentPage && ( <> {currentPage.dimensions && (
DIMENSIONS
{currentPage.dimensions.width} × {currentPage.dimensions.height}px
DPI
{currentPage.dimensions.dpi}
)} {currentPage.markdown ? (
MARKDOWN
) : (
No text extracted from this page.
)} {pageImages.length > 0 && (
EXTRACTED IMAGES ({pageImages.length})
{pageImages.map((img) => ( {`Image ))}
)} {totalPages > 1 && (
Page {currentIndex + 1} / {totalPages}
)} )}
)}
); }