2025-08-19 18:00:50 +01:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useEndpointEnabled } from "../hooks/useEndpointConfig";
|
|
|
|
import { useFileContext } from "../contexts/FileContext";
|
2025-08-21 10:37:14 +01:00
|
|
|
import { useNavigationActions } from "../contexts/NavigationContext";
|
2025-08-21 16:43:01 +01:00
|
|
|
import { useFileSelection } from "../contexts/file/fileHooks";
|
2025-08-19 18:00:50 +01:00
|
|
|
|
|
|
|
import { createToolFlow } from "../components/tools/shared/createToolFlow";
|
|
|
|
|
|
|
|
import { useRepairParameters } from "../hooks/tools/repair/useRepairParameters";
|
|
|
|
import { useRepairOperation } from "../hooks/tools/repair/useRepairOperation";
|
|
|
|
import { BaseToolProps } from "../types/tool";
|
|
|
|
|
|
|
|
const Repair = ({ onPreviewFile, onComplete, onError }: BaseToolProps) => {
|
|
|
|
const { t } = useTranslation();
|
2025-08-21 10:37:14 +01:00
|
|
|
const { actions } = useNavigationActions();
|
2025-08-21 16:43:01 +01:00
|
|
|
const { selectedFiles } = useFileSelection();
|
2025-08-19 18:00:50 +01:00
|
|
|
|
|
|
|
const repairParams = useRepairParameters();
|
|
|
|
const repairOperation = useRepairOperation();
|
|
|
|
|
|
|
|
// Endpoint validation
|
|
|
|
const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled(repairParams.getEndpointName());
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
repairOperation.resetResults();
|
|
|
|
onPreviewFile?.(null);
|
|
|
|
}, [repairParams.parameters]);
|
|
|
|
|
|
|
|
const handleRepair = async () => {
|
|
|
|
try {
|
|
|
|
await repairOperation.executeOperation(repairParams.parameters, selectedFiles);
|
|
|
|
if (repairOperation.files && onComplete) {
|
|
|
|
onComplete(repairOperation.files);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (onError) {
|
|
|
|
onError(error instanceof Error ? error.message : t("repair.error.failed", "Repair operation failed"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleThumbnailClick = (file: File) => {
|
|
|
|
onPreviewFile?.(file);
|
|
|
|
sessionStorage.setItem("previousMode", "repair");
|
2025-08-21 10:37:14 +01:00
|
|
|
actions.setMode("viewer");
|
2025-08-19 18:00:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleSettingsReset = () => {
|
|
|
|
repairOperation.resetResults();
|
|
|
|
onPreviewFile?.(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasFiles = selectedFiles.length > 0;
|
|
|
|
const hasResults = repairOperation.files.length > 0 || repairOperation.downloadUrl !== null;
|
|
|
|
|
|
|
|
return createToolFlow({
|
|
|
|
files: {
|
|
|
|
selectedFiles,
|
2025-08-20 16:54:30 +01:00
|
|
|
isCollapsed: hasResults,
|
2025-08-19 18:00:50 +01:00
|
|
|
placeholder: t("repair.files.placeholder", "Select a PDF file in the main view to get started"),
|
|
|
|
},
|
|
|
|
steps: [],
|
|
|
|
executeButton: {
|
|
|
|
text: t("repair.submit", "Repair PDF"),
|
|
|
|
isVisible: !hasResults,
|
|
|
|
loadingText: t("loading"),
|
|
|
|
onClick: handleRepair,
|
|
|
|
disabled: !repairParams.validateParameters() || !hasFiles || !endpointEnabled,
|
|
|
|
},
|
|
|
|
review: {
|
|
|
|
isVisible: hasResults,
|
|
|
|
operation: repairOperation,
|
|
|
|
title: t("repair.results.title", "Repair Results"),
|
|
|
|
onFileClick: handleThumbnailClick,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2025-08-20 16:54:30 +01:00
|
|
|
export default Repair;
|