2025-05-21 21:47:44 +01:00
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
import { Paper, Button, Checkbox, Stack, Text, Group, Loader, Alert } from "@mantine/core";
|
2025-05-27 19:22:26 +01:00
|
|
|
import { useSearchParams } from "react-router-dom";
|
2025-05-29 17:26:32 +01:00
|
|
|
import { useTranslation } from "react-i18next";
|
2025-06-05 11:12:39 +01:00
|
|
|
import { FileWithUrl } from "../types/file";
|
|
|
|
import { fileStorage } from "../services/fileStorage";
|
2025-06-27 18:00:35 +01:00
|
|
|
import { useEndpointEnabled } from "../hooks/useEndpointConfig";
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
export interface MergePdfPanelProps {
|
2025-06-05 11:12:39 +01:00
|
|
|
files: FileWithUrl[];
|
2025-05-21 21:47:44 +01:00
|
|
|
setDownloadUrl: (url: string) => void;
|
2025-05-27 19:22:26 +01:00
|
|
|
params: {
|
|
|
|
order: string;
|
|
|
|
removeDuplicates: boolean;
|
|
|
|
};
|
|
|
|
updateParams: (newParams: Partial<MergePdfPanelProps["params"]>) => void;
|
2025-05-21 21:47:44 +01:00
|
|
|
}
|
|
|
|
|
2025-05-27 19:22:26 +01:00
|
|
|
const MergePdfPanel: React.FC<MergePdfPanelProps> = ({
|
|
|
|
files,
|
|
|
|
setDownloadUrl,
|
|
|
|
params,
|
|
|
|
updateParams,
|
|
|
|
}) => {
|
2025-05-29 17:26:32 +01:00
|
|
|
const { t } = useTranslation();
|
2025-05-21 21:47:44 +01:00
|
|
|
const [selectedFiles, setSelectedFiles] = useState<boolean[]>([]);
|
|
|
|
const [downloadUrl, setLocalDownloadUrl] = useState<string | null>(null);
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
2025-06-27 18:00:35 +01:00
|
|
|
const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled("merge-pdfs");
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSelectedFiles(files.map(() => true));
|
|
|
|
}, [files]);
|
|
|
|
|
|
|
|
const handleMerge = async () => {
|
|
|
|
const filesToMerge = files.filter((_, index) => selectedFiles[index]);
|
|
|
|
if (filesToMerge.length < 2) {
|
2025-05-29 17:26:32 +01:00
|
|
|
setErrorMessage(t("multiPdfPrompt")); // "Select PDFs (2+)"
|
2025-05-21 21:47:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData();
|
2025-06-05 11:12:39 +01:00
|
|
|
|
|
|
|
// Handle IndexedDB files
|
|
|
|
for (const file of filesToMerge) {
|
|
|
|
if (!file.id) {
|
|
|
|
continue; // Skip files without an id
|
|
|
|
}
|
|
|
|
const storedFile = await fileStorage.getFile(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);
|
|
|
|
}
|
|
|
|
}
|
2025-05-21 21:47:44 +01:00
|
|
|
|
|
|
|
setIsLoading(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch("/api/v1/general/merge-pdfs", {
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
const errorText = await response.text();
|
|
|
|
throw new Error(`Failed to merge PDFs: ${errorText}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const blob = await response.blob();
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
setDownloadUrl(url);
|
|
|
|
setLocalDownloadUrl(url);
|
|
|
|
} catch (error: any) {
|
|
|
|
setErrorMessage(error.message || "Unknown error occurred.");
|
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCheckboxChange = (index: number) => {
|
|
|
|
setSelectedFiles((prev) =>
|
|
|
|
prev.map((selected, i) => (i === index ? !selected : selected))
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectedCount = selectedFiles.filter(Boolean).length;
|
|
|
|
|
2025-05-27 19:22:26 +01:00
|
|
|
const { order, removeDuplicates } = params;
|
|
|
|
|
2025-06-27 18:00:35 +01:00
|
|
|
if (endpointLoading) {
|
|
|
|
return (
|
|
|
|
<Stack align="center" justify="center" h={200}>
|
|
|
|
<Loader size="md" />
|
|
|
|
<Text size="sm" c="dimmed">{t("loading", "Loading...")}</Text>
|
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endpointEnabled === false) {
|
|
|
|
return (
|
|
|
|
<Stack align="center" justify="center" h={200}>
|
|
|
|
<Alert color="red" title={t("error._value", "Error")} variant="light">
|
|
|
|
{t("endpointDisabled", "This feature is currently disabled.")}
|
|
|
|
</Alert>
|
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-05-21 21:47:44 +01:00
|
|
|
return (
|
|
|
|
<Stack>
|
2025-05-29 17:26:32 +01:00
|
|
|
<Text fw={500} size="lg">{t("merge.header")}</Text>
|
2025-05-21 21:47:44 +01:00
|
|
|
<Stack gap={4}>
|
|
|
|
{files.map((file, index) => (
|
|
|
|
<Group key={index} gap="xs">
|
|
|
|
<Checkbox
|
|
|
|
checked={selectedFiles[index] || false}
|
|
|
|
onChange={() => handleCheckboxChange(index)}
|
|
|
|
/>
|
|
|
|
<Text size="sm">{file.name}</Text>
|
|
|
|
</Group>
|
|
|
|
))}
|
|
|
|
</Stack>
|
|
|
|
{selectedCount < 2 && (
|
|
|
|
<Text size="sm" c="red">
|
2025-05-29 17:26:32 +01:00
|
|
|
{t("multiPdfPrompt")}
|
2025-05-21 21:47:44 +01:00
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
<Button
|
|
|
|
onClick={handleMerge}
|
|
|
|
loading={isLoading}
|
|
|
|
disabled={selectedCount < 2 || isLoading}
|
|
|
|
mt="md"
|
|
|
|
>
|
2025-05-29 17:26:32 +01:00
|
|
|
{t("merge.submit")}
|
2025-05-21 21:47:44 +01:00
|
|
|
</Button>
|
|
|
|
{errorMessage && (
|
|
|
|
<Alert color="red" mt="sm">
|
|
|
|
{errorMessage}
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
{downloadUrl && (
|
|
|
|
<Button
|
|
|
|
component="a"
|
|
|
|
href={downloadUrl}
|
|
|
|
download="merged.pdf"
|
|
|
|
color="green"
|
|
|
|
variant="light"
|
|
|
|
mt="md"
|
|
|
|
>
|
2025-05-29 17:26:32 +01:00
|
|
|
{t("downloadPdf")}
|
2025-05-21 21:47:44 +01:00
|
|
|
</Button>
|
|
|
|
)}
|
2025-05-27 19:22:26 +01:00
|
|
|
<Checkbox
|
2025-05-29 17:26:32 +01:00
|
|
|
label={t("merge.removeCertSign")}
|
2025-05-27 19:22:26 +01:00
|
|
|
checked={removeDuplicates}
|
|
|
|
onChange={() => updateParams({ removeDuplicates: !removeDuplicates })}
|
|
|
|
/>
|
2025-05-21 21:47:44 +01:00
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MergePdfPanel;
|