mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-04 03:25:21 +00:00

# Description of Changes This pull request introduces significant updates to the file selection logic, tool rendering, and file context management in the frontend codebase. The changes aim to improve modularity, enhance maintainability, and streamline the handling of file-related operations. Key updates include the introduction of a new `FileSelectionContext`, refactoring of file selection logic, and updates to tool management and rendering. ### File Selection Context and Logic Refactor: * Added a new `FileSelectionContext` to centralize file selection state and provide utility hooks for managing selected files, selection limits, and tool mode. (`frontend/src/contexts/FileSelectionContext.tsx`, [frontend/src/contexts/FileSelectionContext.tsxR1-R77](diffhunk://#diff-bda35f1aaa5eafa0a0dc48e0b1270d862f6da360ba1241234e891f0ca8907327R1-R77)) * Replaced local file selection logic in `FileEditor` with context-based logic, improving consistency and reducing duplication. (`frontend/src/components/fileEditor/FileEditor.tsx`, [[1]](diffhunk://#diff-481d0a2d8a1714d34d21181db63a020b08dfccfbfa80bf47ac9af382dff25310R63-R70) [[2]](diffhunk://#diff-481d0a2d8a1714d34d21181db63a020b08dfccfbfa80bf47ac9af382dff25310R404-R438) ### Tool Management and Rendering: * Refactored `ToolRenderer` to use a `Suspense` fallback for lazy-loaded tools, improving user experience during tool loading. (`frontend/src/components/tools/ToolRenderer.tsx`, [frontend/src/components/tools/ToolRenderer.tsxL32-L64](diffhunk://#diff-2083701113aa92cd1f5ce1b4b52cc233858e31ed7bcf39c5bfb1bcc34e99b6a9L32-L64)) * Simplified `ToolPicker` by reusing the `ToolRegistry` type, reducing redundancy. (`frontend/src/components/tools/ToolPicker.tsx`, [frontend/src/components/tools/ToolPicker.tsxL4-R4](diffhunk://#diff-e47deca9132018344c159925f1264794acdd57f4b65e582eb9b2a4ea69ec126dL4-R4)) ### File Context Enhancements: * Introduced a utility function `getFileId` for consistent file ID extraction, replacing repetitive inline logic. (`frontend/src/contexts/FileContext.tsx`, [[1]](diffhunk://#diff-95b3d103fa434f81fdae55f2ea14eda705f0def45a0f2c5754f81de6f2fd93bcR25) [[2]](diffhunk://#diff-95b3d103fa434f81fdae55f2ea14eda705f0def45a0f2c5754f81de6f2fd93bcL101-R102) * Updated `FileContextProvider` to use more specific types for PDF documents, enhancing type safety. (`frontend/src/contexts/FileContext.tsx`, [[1]](diffhunk://#diff-95b3d103fa434f81fdae55f2ea14eda705f0def45a0f2c5754f81de6f2fd93bcL350-R351) [[2]](diffhunk://#diff-95b3d103fa434f81fdae55f2ea14eda705f0def45a0f2c5754f81de6f2fd93bcL384-R385) ### Compression Tool Enhancements: * Added blob URL cleanup logic to the compression hook to prevent memory leaks. (`frontend/src/hooks/tools/compress/useCompressOperation.ts`, [frontend/src/hooks/tools/compress/useCompressOperation.tsR58-L66](diffhunk://#diff-d7815fea0e89989511ae1786f7031cba492b9f2db39b7ade92d9736d1bd4b673R58-L66)) * Adjusted file ID generation in the compression operation to handle multiple files more effectively. (`frontend/src/hooks/tools/compress/useCompressOperation.ts`, [frontend/src/hooks/tools/compress/useCompressOperation.tsL90-R102](diffhunk://#diff-d7815fea0e89989511ae1786f7031cba492b9f2db39b7ade92d9736d1bd4b673L90-R102)) --- ## 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.
169 lines
5.6 KiB
TypeScript
169 lines
5.6 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 CompressSettings from "../components/tools/compress/CompressSettings";
|
|
|
|
import { useCompressParameters } from "../hooks/tools/compress/useCompressParameters";
|
|
import { useCompressOperation } from "../hooks/tools/compress/useCompressOperation";
|
|
import { BaseToolProps } from "../types/tool";
|
|
|
|
const Compress = ({ onPreviewFile, onComplete, onError }: BaseToolProps) => {
|
|
const { t } = useTranslation();
|
|
const { setCurrentMode } = useFileContext();
|
|
const { selectedFiles } = useToolFileSelection();
|
|
|
|
const compressParams = useCompressParameters();
|
|
const compressOperation = useCompressOperation();
|
|
|
|
// Endpoint validation
|
|
const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled("compress-pdf");
|
|
|
|
useEffect(() => {
|
|
compressOperation.resetResults();
|
|
onPreviewFile?.(null);
|
|
}, [compressParams.parameters, selectedFiles]);
|
|
|
|
const handleCompress = async () => {
|
|
try {
|
|
await compressOperation.executeOperation(
|
|
compressParams.parameters,
|
|
selectedFiles
|
|
);
|
|
if (compressOperation.files && onComplete) {
|
|
onComplete(compressOperation.files);
|
|
}
|
|
} catch (error) {
|
|
if (onError) {
|
|
onError(error instanceof Error ? error.message : 'Compress operation failed');
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleThumbnailClick = (file: File) => {
|
|
onPreviewFile?.(file);
|
|
sessionStorage.setItem('previousMode', 'compress');
|
|
setCurrentMode('viewer');
|
|
};
|
|
|
|
const handleSettingsReset = () => {
|
|
compressOperation.resetResults();
|
|
onPreviewFile?.(null);
|
|
setCurrentMode('compress');
|
|
};
|
|
|
|
const hasFiles = selectedFiles.length > 0;
|
|
const hasResults = compressOperation.files.length > 0 || compressOperation.downloadUrl !== null;
|
|
const filesCollapsed = hasFiles;
|
|
const settingsCollapsed = hasResults;
|
|
|
|
const previewResults = useMemo(() =>
|
|
compressOperation.files?.map((file, index) => ({
|
|
file,
|
|
thumbnail: compressOperation.thumbnails[index]
|
|
})) || [],
|
|
[compressOperation.files, compressOperation.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 ?
|
|
selectedFiles.length === 1
|
|
? `Selected: ${selectedFiles[0].name}`
|
|
: `Selected: ${selectedFiles.length} files`
|
|
: 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 ? "Compression completed" : undefined}
|
|
>
|
|
<Stack gap="sm">
|
|
<CompressSettings
|
|
parameters={compressParams.parameters}
|
|
onParameterChange={compressParams.updateParameter}
|
|
disabled={endpointLoading}
|
|
/>
|
|
|
|
<OperationButton
|
|
onClick={handleCompress}
|
|
isLoading={compressOperation.isLoading}
|
|
disabled={!compressParams.validateParameters() || !hasFiles || !endpointEnabled}
|
|
loadingText={t("loading")}
|
|
submitText="Compress and Review"
|
|
/>
|
|
</Stack>
|
|
</ToolStep>
|
|
|
|
{/* Results Step */}
|
|
<ToolStep
|
|
title="Results"
|
|
isVisible={hasResults}
|
|
>
|
|
<Stack gap="sm">
|
|
{compressOperation.status && (
|
|
<Text size="sm" c="dimmed">{compressOperation.status}</Text>
|
|
)}
|
|
|
|
<ErrorNotification
|
|
error={compressOperation.errorMessage}
|
|
onClose={compressOperation.clearError}
|
|
/>
|
|
|
|
{compressOperation.downloadUrl && (
|
|
<Button
|
|
component="a"
|
|
href={compressOperation.downloadUrl}
|
|
download={compressOperation.downloadFilename}
|
|
leftSection={<DownloadIcon />}
|
|
color="green"
|
|
fullWidth
|
|
mb="md"
|
|
>
|
|
{t("download", "Download")}
|
|
</Button>
|
|
)}
|
|
|
|
<ResultsPreview
|
|
files={previewResults}
|
|
onFileClick={handleThumbnailClick}
|
|
isGeneratingThumbnails={compressOperation.isGeneratingThumbnails}
|
|
title="Compression Results"
|
|
/>
|
|
</Stack>
|
|
</ToolStep>
|
|
</Stack>
|
|
</ToolStepContainer>
|
|
);
|
|
}
|
|
|
|
|
|
export default Compress;
|