import { useEffect, useRef, useState } from "react";
import {
ChevronDownIcon,
DocumentIcon,
DownloadIcon,
PackageIcon,
} from "../ui/Icons";
import { Spinner } from "../ui/Spinner";
const FORMATS = [
{
format: "xml",
icon: ,
label: "SWORD XML",
desc: "Metadatos en formato Atom",
},
{
format: "zip",
icon: ,
label: "Paquete ZIP",
desc: "XML + ficheros adjuntos",
},
];
/**
* SWORD export dropdown. Delegates the actual download to `onExport(format)`
* so it can be wired up either to the real API or to a mock layer from the
* parent page.
*
* `exportingFormat` (optional) lets the parent keep the button in a loading
* state between clicks (e.g. while waiting for the backend blob).
*/
export function ExportDropdown({
onExport,
exportingFormat = null,
selectedCount = 0,
}) {
const [open, setOpen] = useState(false);
const rootRef = useRef(null);
useEffect(() => {
function handleClick(event) {
if (rootRef.current && !rootRef.current.contains(event.target)) {
setOpen(false);
}
}
document.addEventListener("mousedown", handleClick);
return () => document.removeEventListener("mousedown", handleClick);
}, []);
const isBusy = Boolean(exportingFormat);
const hasSelection = selectedCount > 0;
function handlePick(format) {
setOpen(false);
onExport(format);
}
const idleLabel = hasSelection
? `Exportar seleccionadas (${selectedCount})`
: "Exportar todas";
return (
{open && (
{FORMATS.map(({ format, icon, label, desc }, idx) => (
))}
)}
);
}