mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-21 19:59:24 +00:00

# Description of Changes <!-- Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --> --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Reece Browne <you@example.com>
167 lines
5.4 KiB
TypeScript
167 lines
5.4 KiB
TypeScript
import React, { useEffect, useMemo } from "react";
|
|
import { Button, Stack, Text } from "@mantine/core";
|
|
import { useTranslation } from "react-i18next";
|
|
import DownloadIcon from "@mui/icons-material/Download";
|
|
import { useEndpointEnabled } from "../hooks/useEndpointConfig";
|
|
import { useFileContext } from "../contexts/FileContext";
|
|
import { useToolFileSelection } from "../contexts/FileSelectionContext";
|
|
|
|
import ToolStep, { ToolStepContainer } from "../components/tools/shared/ToolStep";
|
|
import OperationButton from "../components/tools/shared/OperationButton";
|
|
import ErrorNotification from "../components/tools/shared/ErrorNotification";
|
|
import FileStatusIndicator from "../components/tools/shared/FileStatusIndicator";
|
|
import ResultsPreview from "../components/tools/shared/ResultsPreview";
|
|
import SplitSettings from "../components/tools/split/SplitSettings";
|
|
|
|
import { useSplitParameters } from "../hooks/tools/split/useSplitParameters";
|
|
import { useSplitOperation } from "../hooks/tools/split/useSplitOperation";
|
|
import { BaseToolProps } from "../types/tool";
|
|
|
|
const Split = ({ onPreviewFile, onComplete, onError }: BaseToolProps) => {
|
|
const { t } = useTranslation();
|
|
const { setCurrentMode } = useFileContext();
|
|
const { selectedFiles } = useToolFileSelection();
|
|
|
|
const splitParams = useSplitParameters();
|
|
const splitOperation = useSplitOperation();
|
|
|
|
// Endpoint validation
|
|
const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled(
|
|
splitParams.getEndpointName()
|
|
);
|
|
|
|
useEffect(() => {
|
|
splitOperation.resetResults();
|
|
onPreviewFile?.(null);
|
|
}, [splitParams.parameters, selectedFiles]);
|
|
|
|
const handleSplit = async () => {
|
|
try {
|
|
await splitOperation.executeOperation(
|
|
splitParams.parameters,
|
|
selectedFiles
|
|
);
|
|
if (splitOperation.files && onComplete) {
|
|
onComplete(splitOperation.files);
|
|
}
|
|
} catch (error) {
|
|
if (onError) {
|
|
onError(error instanceof Error ? error.message : 'Split operation failed');
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleThumbnailClick = (file: File) => {
|
|
onPreviewFile?.(file);
|
|
sessionStorage.setItem('previousMode', 'split');
|
|
setCurrentMode('viewer');
|
|
};
|
|
|
|
const handleSettingsReset = () => {
|
|
splitOperation.resetResults();
|
|
onPreviewFile?.(null);
|
|
setCurrentMode('split');
|
|
};
|
|
|
|
const hasFiles = selectedFiles.length > 0;
|
|
const hasResults = splitOperation.downloadUrl !== null;
|
|
const filesCollapsed = hasFiles;
|
|
const settingsCollapsed = hasResults;
|
|
|
|
const previewResults = useMemo(() =>
|
|
splitOperation.files?.map((file, index) => ({
|
|
file,
|
|
thumbnail: splitOperation.thumbnails[index]
|
|
})) || [],
|
|
[splitOperation.files, splitOperation.thumbnails]
|
|
);
|
|
|
|
return (
|
|
<ToolStepContainer>
|
|
<Stack gap="sm" h="100%" p="sm" style={{ overflow: 'auto' }}>
|
|
{/* Files Step */}
|
|
<ToolStep
|
|
title="Files"
|
|
isVisible={true}
|
|
isCollapsed={filesCollapsed}
|
|
isCompleted={filesCollapsed}
|
|
completedMessage={hasFiles ? `Selected: ${selectedFiles[0]?.name}` : undefined}
|
|
>
|
|
<FileStatusIndicator
|
|
selectedFiles={selectedFiles}
|
|
placeholder="Select a PDF file in the main view to get started"
|
|
/>
|
|
</ToolStep>
|
|
|
|
{/* Settings Step */}
|
|
<ToolStep
|
|
title="Settings"
|
|
isVisible={hasFiles}
|
|
isCollapsed={settingsCollapsed}
|
|
isCompleted={settingsCollapsed}
|
|
onCollapsedClick={settingsCollapsed ? handleSettingsReset : undefined}
|
|
completedMessage={settingsCollapsed ? "Split completed" : undefined}
|
|
>
|
|
<Stack gap="sm">
|
|
<SplitSettings
|
|
parameters={splitParams.parameters}
|
|
onParameterChange={splitParams.updateParameter}
|
|
disabled={endpointLoading}
|
|
/>
|
|
|
|
{splitParams.parameters.mode && (
|
|
<OperationButton
|
|
onClick={handleSplit}
|
|
isLoading={splitOperation.isLoading}
|
|
disabled={!splitParams.validateParameters() || !hasFiles || !endpointEnabled}
|
|
loadingText={t("loading")}
|
|
submitText={t("split.submit", "Split PDF")}
|
|
/>
|
|
)}
|
|
</Stack>
|
|
</ToolStep>
|
|
|
|
{/* Results Step */}
|
|
<ToolStep
|
|
title="Results"
|
|
isVisible={hasResults}
|
|
>
|
|
<Stack gap="sm">
|
|
{splitOperation.status && (
|
|
<Text size="sm" c="dimmed">{splitOperation.status}</Text>
|
|
)}
|
|
|
|
<ErrorNotification
|
|
error={splitOperation.errorMessage}
|
|
onClose={splitOperation.clearError}
|
|
/>
|
|
|
|
{splitOperation.downloadUrl && (
|
|
<Button
|
|
component="a"
|
|
href={splitOperation.downloadUrl}
|
|
download="split_output.zip"
|
|
leftSection={<DownloadIcon />}
|
|
color="green"
|
|
fullWidth
|
|
mb="md"
|
|
>
|
|
{t("download", "Download")}
|
|
</Button>
|
|
)}
|
|
|
|
<ResultsPreview
|
|
files={previewResults}
|
|
onFileClick={handleThumbnailClick}
|
|
isGeneratingThumbnails={splitOperation.isGeneratingThumbnails}
|
|
title="Split Results"
|
|
/>
|
|
</Stack>
|
|
</ToolStep>
|
|
</Stack>
|
|
</ToolStepContainer>
|
|
);
|
|
}
|
|
|
|
export default Split;
|