import React, { useEffect, useState } from 'react'; interface ReorderPanelProps { pageCount: number; thumbnails: string[] | null; isBusy: boolean; hasPdf: boolean; rotations: Record; onRotate: (pageIndex: number) => void; onExportReordered: (order: number[]) => void; reorderDownloadUrl: string | null; reorderFilename: string | null; } const ReorderPanel: React.FC = ({ pageCount, thumbnails, isBusy, hasPdf, rotations, onRotate, onExportReordered, reorderDownloadUrl, reorderFilename, }) => { const [order, setOrder] = useState([]); const [draggingIndex, setDraggingIndex] = useState(null); useEffect(() => { if (pageCount > 0) { setOrder(Array.from({ length: pageCount }, (_, i) => i)); } else { setOrder([]); } }, [pageCount]); const handleDragStart = (index: number) => (e: React.DragEvent) => { setDraggingIndex(index); e.dataTransfer.effectAllowed = 'move'; }; const handleDragOver = (index: number) => (e: React.DragEvent) => { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; }; const handleDrop = (index: number) => (e: React.DragEvent) => { e.preventDefault(); if (draggingIndex === null || draggingIndex === index) return; setOrder((prev) => { const updated = [...prev]; const [moved] = updated.splice(draggingIndex, 1); updated.splice(index, 0, moved); return updated; }); setDraggingIndex(null); }; const handleDragEnd = () => { setDraggingIndex(null); }; const handleDelete = (visualIndex: number) => () => { setOrder((prev) => prev.filter((_, idx) => idx !== visualIndex)); }; const handleRotateClick = (pageIndex: number) => () => { onRotate(pageIndex); }; const handleExport = () => { if (!hasPdf || order.length === 0) return; onExportReordered(order); }; if (!hasPdf) { return (

Reorder pages

Load a PDF first to reorder, delete, or rotate its pages.

); } return (

Reorder / delete / rotate

Drag pages to reorder them. Use rotate and delete controls below each thumbnail. All changes stay in memory until you export a new PDF.

{order.map((pageIndex, visualIndex) => { const thumb = thumbnails?.[pageIndex]; const isDragging = visualIndex === draggingIndex; const rotation = rotations[pageIndex] ?? 0; return (
{thumb ? ( {`Page ) : (
)} Page {pageIndex + 1} Pos {visualIndex + 1} · Rot {rotation}°
); })}
{reorderDownloadUrl && reorderFilename && (
Reordered result:{' '} Download {reorderFilename}
)}
); }; export default ReorderPanel;