Spaces:
Running
Running
File size: 10,537 Bytes
00e8c2a fafa006 00e8c2a fafa006 00e8c2a fafa006 00e8c2a fafa006 00e8c2a |
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
import React, { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import {
Download,
Braces,
FileCode2,
Check,
Share2,
FileJson,
Copy,
Mail,
Link2,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
// Helper functions from ExtractionOutput
function prepareFieldsForOutput(fields, format = "json") {
if (!fields || typeof fields !== "object") {
return fields;
}
const output = { ...fields };
// Remove full_text from top-level if pages array exists (to avoid duplication)
if (output.pages && Array.isArray(output.pages) && output.pages.length > 0) {
delete output.full_text;
// Clean up each page: remove full_text from page.fields (it duplicates page.text)
output.pages = output.pages.map(page => {
const cleanedPage = { ...page };
if (cleanedPage.fields && typeof cleanedPage.fields === "object") {
const cleanedFields = { ...cleanedPage.fields };
// Remove full_text from page fields (duplicates page.text)
delete cleanedFields.full_text;
cleanedPage.fields = cleanedFields;
}
return cleanedPage;
});
}
// For JSON and XML: restructure pages into separate top-level fields (page_1, page_2, etc.)
if ((format === "json" || format === "xml") && output.pages && Array.isArray(output.pages)) {
// Get top-level field keys (these are merged from all pages - avoid duplicating in page fields)
const topLevelKeys = new Set(Object.keys(output).filter(k => k !== "pages" && k !== "full_text"));
output.pages.forEach((page, idx) => {
const pageNum = page.page_number || idx + 1;
const pageFields = page.fields || {};
// Remove duplicate fields from page.fields:
// 1. Remove full_text (duplicates page.text)
// 2. Remove fields that match top-level fields (already shown at root)
const cleanedPageFields = {};
for (const [key, value] of Object.entries(pageFields)) {
// Skip full_text and fields that match top-level exactly
if (key !== "full_text" && (!topLevelKeys.has(key) || (value !== output[key]))) {
cleanedPageFields[key] = value;
}
}
const pageObj = {
text: page.text || "",
confidence: page.confidence || 0,
doc_type: page.doc_type || "other"
};
// Only add fields if there are unique page-specific fields
if (Object.keys(cleanedPageFields).length > 0) {
pageObj.fields = cleanedPageFields;
}
output[`page_${pageNum}`] = pageObj;
});
// Remove pages array - we now have page_1, page_2, etc. as separate fields
delete output.pages;
}
return output;
}
function escapeXML(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function objectToXML(obj, rootName = "extraction") {
// Prepare fields - remove full_text if pages exist
const preparedObj = prepareFieldsForOutput(obj, "xml");
let xml = `<?xml version="1.0" encoding="UTF-8"?>\n<${rootName}>\n`;
const convert = (obj, indent = " ") => {
for (const [key, value] of Object.entries(obj)) {
if (value === null || value === undefined) continue;
// Skip full_text if pages exist (already handled in prepareFieldsForOutput)
if (key === "full_text" && obj.pages && Array.isArray(obj.pages) && obj.pages.length > 0) {
continue;
}
if (Array.isArray(value)) {
value.forEach((item) => {
xml += `${indent}<${key}>\n`;
if (typeof item === "object") {
convert(item, indent + " ");
} else {
xml += `${indent} ${escapeXML(String(item))}\n`;
}
xml += `${indent}</${key}>\n`;
});
} else if (typeof value === "object") {
xml += `${indent}<${key}>\n`;
convert(value, indent + " ");
xml += `${indent}</${key}>\n`;
} else {
xml += `${indent}<${key}>${escapeXML(String(value))}</${key}>\n`;
}
}
};
convert(preparedObj);
xml += `</${rootName}>`;
return xml;
}
export default function ExportButtons({ isComplete, extractionResult }) {
const [downloading, setDownloading] = useState(null);
const [copied, setCopied] = useState(false);
const handleDownload = (format) => {
if (!extractionResult || !extractionResult.fields) {
console.error("No extraction data available");
return;
}
setDownloading(format);
try {
const fields = extractionResult.fields;
let content = "";
let filename = "";
let mimeType = "";
if (format === "json") {
const preparedFields = prepareFieldsForOutput(fields, "json");
content = JSON.stringify(preparedFields, null, 2);
filename = `extraction_${new Date().toISOString().split('T')[0]}.json`;
mimeType = "application/json";
} else if (format === "xml") {
content = objectToXML(fields);
filename = `extraction_${new Date().toISOString().split('T')[0]}.xml`;
mimeType = "application/xml";
}
// Create blob and download
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
setDownloading(null);
} catch (error) {
console.error("Download error:", error);
setDownloading(null);
}
};
const handleCopyLink = () => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
if (!isComplete) return null;
return (
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
className="flex items-center gap-3"
>
{/* JSON Download */}
<Button
onClick={() => handleDownload("json")}
disabled={downloading === "json"}
className={cn(
"h-11 px-5 rounded-xl font-semibold transition-all duration-200",
"bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-700 hover:to-violet-700",
"shadow-lg shadow-indigo-500/25 hover:shadow-xl hover:shadow-indigo-500/30",
"text-white"
)}
>
<AnimatePresence mode="wait">
{downloading === "json" ? (
<motion.div
key="loading"
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
className="flex items-center gap-2"
>
<motion.div
animate={{ rotate: 360 }}
transition={{ duration: 1, repeat: Infinity, ease: "linear" }}
>
<Download className="h-4 w-4" />
</motion.div>
Downloading...
</motion.div>
) : (
<motion.div
key="default"
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
className="flex items-center gap-2"
>
<Braces className="h-4 w-4" />
Download JSON
</motion.div>
)}
</AnimatePresence>
</Button>
{/* XML Download */}
<Button
onClick={() => handleDownload("xml")}
disabled={downloading === "xml"}
variant="outline"
className={cn(
"h-11 px-5 rounded-xl font-semibold transition-all duration-200",
"border-2 border-slate-200 hover:border-slate-300",
"hover:bg-slate-50"
)}
>
<AnimatePresence mode="wait">
{downloading === "xml" ? (
<motion.div
key="loading"
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
className="flex items-center gap-2"
>
<motion.div
animate={{ rotate: 360 }}
transition={{ duration: 1, repeat: Infinity, ease: "linear" }}
>
<Download className="h-4 w-4" />
</motion.div>
Downloading...
</motion.div>
) : (
<motion.div
key="default"
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.8 }}
className="flex items-center gap-2"
>
<FileCode2 className="h-4 w-4" />
Download XML
</motion.div>
)}
</AnimatePresence>
</Button>
{/* More Options Dropdown */}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-11 w-11 rounded-xl">
<Share2 className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-48 rounded-xl p-2">
<DropdownMenuItem
className="rounded-lg cursor-pointer"
onClick={handleCopyLink}
>
{copied ? (
<Check className="h-4 w-4 mr-2 text-emerald-500" />
) : (
<Link2 className="h-4 w-4 mr-2" />
)}
{copied ? "Link copied!" : "Copy share link"}
</DropdownMenuItem>
<DropdownMenuItem className="rounded-lg cursor-pointer">
<Copy className="h-4 w-4 mr-2" />
Copy to clipboard
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem className="rounded-lg cursor-pointer">
<Mail className="h-4 w-4 mr-2" />
Send via email
</DropdownMenuItem>
<DropdownMenuItem className="rounded-lg cursor-pointer">
<FileJson className="h-4 w-4 mr-2" />
Export to Google Sheets
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</motion.div>
);
}
|