Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

269 lines
8.9 KiB
TypeScript
Raw Normal View History

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";
2025-05-29 17:26:32 +01:00
import { useTranslation } from "react-i18next";
import DownloadIcon from "@mui/icons-material/Download";
2025-06-04 19:04:56 +01:00
import { FileWithUrl } from "../types/file";
import { fileStorage } from "../services/fileStorage";
export interface SplitPdfPanelProps {
2025-06-04 19:04:56 +01:00
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<SplitPdfPanelProps["params"]>) => void;
}
const SplitPdfPanel: React.FC<SplitPdfPanelProps> = ({
file,
downloadUrl,
setDownloadUrl,
params,
updateParams,
}) => {
2025-05-29 17:26:32 +01:00
const { t } = useTranslation();
const [searchParams] = useSearchParams();
const [status, setStatus] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const {
mode,
pages,
hDiv,
vDiv,
merge,
splitType,
splitValue,
bookmarkLevel,
includeMetadata,
allowDuplicates,
} = params;
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!file) {
2025-05-29 17:26:32 +01:00
setStatus(t("noFileSelected"));
return;
}
const formData = new FormData();
2025-06-04 19:04:56 +01:00
// 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;
}
2025-05-29 17:26:32 +01:00
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);
2025-05-29 17:26:32 +01:00
setStatus(t("downloadComplete"));
} catch (error: any) {
console.error(error);
2025-06-04 23:02:53 +01:00
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);
2025-05-29 17:26:32 +01:00
setStatus(t("error._value", "Split failed."));
} finally {
setIsLoading(false);
}
};
return (
2025-06-06 17:20:06 +01:00
<form onSubmit={handleSubmit} className="app-surface p-app-md rounded-app-md">
<Stack gap="sm" mb={16}>
<Select
2025-05-29 17:26:32 +01:00
label={t("split-by-size-or-count.type.label", "Split Mode")}
value={mode}
onChange={(v) => v && updateParams({ mode: v })}
data={[
2025-05-29 17:26:32 +01:00
{ value: "byPages", label: t("split.header", "Split by Pages") + " (e.g. 1,3,5-10)" },
{ value: "bySections", label: t("split-by-sections.title", "Split by Grid Sections") },
{ value: "bySizeOrCount", label: t("split-by-size-or-count.title", "Split by Size or Count") },
{ value: "byChapters", label: t("splitByChapters.title", "Split by Chapters") },
]}
/>
{mode === "byPages" && (
<TextInput
2025-05-29 17:26:32 +01:00
label={t("split.splitPages", "Pages")}
placeholder={t("pageSelectionPrompt", "e.g. 1,3,5-10")}
value={pages}
onChange={(e) => updateParams({ pages: e.target.value })}
/>
)}
{mode === "bySections" && (
<Stack gap="sm">
<TextInput
2025-05-29 17:26:32 +01:00
label={t("split-by-sections.horizontal.label", "Horizontal Divisions")}
type="number"
min="0"
max="300"
value={hDiv}
onChange={(e) => updateParams({ hDiv: e.target.value })}
2025-05-29 17:26:32 +01:00
placeholder={t("split-by-sections.horizontal.placeholder", "Enter number of horizontal divisions")}
/>
<TextInput
2025-05-29 17:26:32 +01:00
label={t("split-by-sections.vertical.label", "Vertical Divisions")}
type="number"
min="0"
max="300"
value={vDiv}
onChange={(e) => updateParams({ vDiv: e.target.value })}
2025-05-29 17:26:32 +01:00
placeholder={t("split-by-sections.vertical.placeholder", "Enter number of vertical divisions")}
/>
<Checkbox
2025-05-29 17:26:32 +01:00
label={t("split-by-sections.merge", "Merge sections into one PDF")}
checked={merge}
onChange={(e) => updateParams({ merge: e.currentTarget.checked })}
/>
</Stack>
)}
{mode === "bySizeOrCount" && (
<Stack gap="sm">
<Select
2025-05-29 17:26:32 +01:00
label={t("split-by-size-or-count.type.label", "Split Type")}
value={splitType}
onChange={(v) => v && updateParams({ splitType: v })}
data={[
2025-05-29 17:26:32 +01:00
{ 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") },
]}
/>
<TextInput
2025-05-29 17:26:32 +01:00
label={t("split-by-size-or-count.value.label", "Split Value")}
placeholder={t("split-by-size-or-count.value.placeholder", "e.g. 10MB or 5 pages")}
value={splitValue}
onChange={(e) => updateParams({ splitValue: e.target.value })}
/>
</Stack>
)}
{mode === "byChapters" && (
<Stack gap="sm">
<TextInput
2025-05-29 17:26:32 +01:00
label={t("splitByChapters.bookmarkLevel", "Bookmark Level")}
type="number"
value={bookmarkLevel}
onChange={(e) => updateParams({ bookmarkLevel: e.target.value })}
/>
<Checkbox
2025-05-29 17:26:32 +01:00
label={t("splitByChapters.includeMetadata", "Include Metadata")}
checked={includeMetadata}
onChange={(e) => updateParams({ includeMetadata: e.currentTarget.checked })}
/>
<Checkbox
2025-05-29 17:26:32 +01:00
label={t("splitByChapters.allowDuplicates", "Allow Duplicate Bookmarks")}
checked={allowDuplicates}
onChange={(e) => updateParams({ allowDuplicates: e.currentTarget.checked })}
/>
</Stack>
)}
<Button type="submit" loading={isLoading} fullWidth>
2025-05-29 17:26:32 +01:00
{isLoading ? t("loading") : t("split.submit", "Split PDF")}
</Button>
2025-06-06 17:20:06 +01:00
{status && <p className="text-xs text-text-muted">{status}</p>}
{errorMessage && (
2025-05-29 17:26:32 +01:00
<Notification color="red" title={t("error._value", "Error")} onClose={() => setErrorMessage(null)}>
{errorMessage}
</Notification>
)}
2025-05-29 17:26:32 +01:00
{status === t("downloadComplete") && downloadUrl && (
<Button
component="a"
href={downloadUrl}
download="split_output.zip"
leftSection={<DownloadIcon />}
color="green"
fullWidth
>
2025-05-29 17:26:32 +01:00
{t("downloadPdf", "Download Split PDF")}
</Button>
)}
</Stack>
</form>
);
};
export default SplitPdfPanel;