import React, { useState } from "react"; import axios from "axios"; import { Button, Select, TextInput, Checkbox, Notification, Stack, Paper, } from "@mantine/core"; import { useSearchParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import DownloadIcon from "@mui/icons-material/Download"; import { FileWithUrl } from "../types/file"; import { fileStorage } from "../services/fileStorage"; export interface SplitPdfPanelProps { file: { file: FileWithUrl; url: string } | null; downloadUrl?: string | null; setDownloadUrl: (url: string | null) => void; params: { mode: string; pages: string; hDiv: string; vDiv: string; merge: boolean; splitType: string; splitValue: string; bookmarkLevel: string; includeMetadata: boolean; allowDuplicates: boolean; }; updateParams: (newParams: Partial) => void; } const SplitPdfPanel: React.FC = ({ file, downloadUrl, setDownloadUrl, params, updateParams, }) => { const { t } = useTranslation(); const [searchParams] = useSearchParams(); const [status, setStatus] = useState(""); const [isLoading, setIsLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); const { mode, pages, hDiv, vDiv, merge, splitType, splitValue, bookmarkLevel, includeMetadata, allowDuplicates, } = params; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!file) { setStatus(t("noFileSelected")); return; } const formData = new FormData(); // Handle IndexedDB files if (!file.file.id) { setStatus(t("noFileSelected")); return; } const storedFile = await fileStorage.getFile(file.file.id); if (storedFile) { const blob = new Blob([storedFile.data], { type: storedFile.type }); const actualFile = new File([blob], storedFile.name, { type: storedFile.type, lastModified: storedFile.lastModified }); formData.append("fileInput", actualFile); } let endpoint = ""; switch (mode) { case "byPages": formData.append("pageNumbers", pages); endpoint = "/api/v1/general/split-pages"; break; case "bySections": formData.append("horizontalDivisions", hDiv); formData.append("verticalDivisions", vDiv); formData.append("merge", merge.toString()); endpoint = "/api/v1/general/split-pdf-by-sections"; break; case "bySizeOrCount": formData.append( "splitType", splitType === "size" ? "0" : splitType === "pages" ? "1" : "2" ); formData.append("splitValue", splitValue); endpoint = "/api/v1/general/split-by-size-or-count"; break; case "byChapters": formData.append("bookmarkLevel", bookmarkLevel); formData.append("includeMetadata", includeMetadata.toString()); formData.append("allowDuplicates", allowDuplicates.toString()); endpoint = "/api/v1/general/split-pdf-by-chapters"; break; default: return; } setStatus(t("loading")); setIsLoading(true); setErrorMessage(null); try { const response = await axios.post(endpoint, formData, { responseType: "blob" }); const blob = new Blob([response.data], { type: "application/zip" }); const url = window.URL.createObjectURL(blob); setDownloadUrl(url); setStatus(t("downloadComplete")); } catch (error: any) { console.error(error); let errorMsg = t("error.pdfPassword", "An error occurred while splitting the PDF."); if (error.response?.data && typeof error.response.data === 'string') { errorMsg = error.response.data; } else if (error.message) { errorMsg = error.message; } setErrorMessage(errorMsg); setStatus(t("error._value", "Split failed.")); } finally { setIsLoading(false); } }; return (
v && updateParams({ splitType: v })} data={[ { value: "size", label: t("split-by-size-or-count.type.size", "By Size") }, { value: "pages", label: t("split-by-size-or-count.type.pageCount", "By Page Count") }, { value: "docs", label: t("split-by-size-or-count.type.docCount", "By Document Count") }, ]} /> updateParams({ splitValue: e.target.value })} /> )} {mode === "byChapters" && ( updateParams({ bookmarkLevel: e.target.value })} /> updateParams({ includeMetadata: e.currentTarget.checked })} /> updateParams({ allowDuplicates: e.currentTarget.checked })} /> )} {status &&

{status}

} {errorMessage && ( setErrorMessage(null)}> {errorMessage} )} {status === t("downloadComplete") && downloadUrl && ( )}
); }; export default SplitPdfPanel;