mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-07-28 08:05:21 +00:00

# Description of Changes <!-- File context for managing files between tools and views Optimisation for large files Updated Split to work with new file system and match Matts stepped design closer --> --- ## 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: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
97 lines
3.1 KiB
TypeScript
97 lines
3.1 KiB
TypeScript
import React, { useState, useCallback, useMemo, useEffect } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import AddToPhotosIcon from "@mui/icons-material/AddToPhotos";
|
|
import ContentCutIcon from "@mui/icons-material/ContentCut";
|
|
import ZoomInMapIcon from "@mui/icons-material/ZoomInMap";
|
|
import SplitPdfPanel from "../tools/Split";
|
|
import CompressPdfPanel from "../tools/Compress";
|
|
import MergePdfPanel from "../tools/Merge";
|
|
import { useMultipleEndpointsEnabled } from "./useEndpointConfig";
|
|
|
|
type ToolRegistryEntry = {
|
|
icon: React.ReactNode;
|
|
name: string;
|
|
component: React.ComponentType<any>;
|
|
view: string;
|
|
};
|
|
|
|
type ToolRegistry = {
|
|
[key: string]: ToolRegistryEntry;
|
|
};
|
|
|
|
const baseToolRegistry = {
|
|
split: { icon: <ContentCutIcon />, component: SplitPdfPanel, view: "split" },
|
|
compress: { icon: <ZoomInMapIcon />, component: CompressPdfPanel, view: "viewer" },
|
|
merge: { icon: <AddToPhotosIcon />, component: MergePdfPanel, view: "pageEditor" },
|
|
};
|
|
|
|
// Tool endpoint mappings
|
|
const toolEndpoints: Record<string, string[]> = {
|
|
split: ["split-pages", "split-pdf-by-sections", "split-by-size-or-count", "split-pdf-by-chapters"],
|
|
compress: ["compress-pdf"],
|
|
merge: ["merge-pdfs"],
|
|
};
|
|
|
|
|
|
export const useToolManagement = () => {
|
|
const { t } = useTranslation();
|
|
|
|
const [selectedToolKey, setSelectedToolKey] = useState<string | null>(null);
|
|
const [toolSelectedFileIds, setToolSelectedFileIds] = useState<string[]>([]);
|
|
|
|
const allEndpoints = Array.from(new Set(Object.values(toolEndpoints).flat()));
|
|
const { endpointStatus, loading: endpointsLoading } = useMultipleEndpointsEnabled(allEndpoints);
|
|
|
|
const isToolAvailable = useCallback((toolKey: string): boolean => {
|
|
if (endpointsLoading) return true;
|
|
const endpoints = toolEndpoints[toolKey] || [];
|
|
return endpoints.some(endpoint => endpointStatus[endpoint] === true);
|
|
}, [endpointsLoading, endpointStatus]);
|
|
|
|
const toolRegistry: ToolRegistry = useMemo(() => {
|
|
const availableToolRegistry: ToolRegistry = {};
|
|
Object.keys(baseToolRegistry).forEach(toolKey => {
|
|
if (isToolAvailable(toolKey)) {
|
|
availableToolRegistry[toolKey] = {
|
|
...baseToolRegistry[toolKey as keyof typeof baseToolRegistry],
|
|
name: t(`home.${toolKey}.title`, toolKey.charAt(0).toUpperCase() + toolKey.slice(1))
|
|
};
|
|
}
|
|
});
|
|
return availableToolRegistry;
|
|
}, [t, isToolAvailable]);
|
|
|
|
useEffect(() => {
|
|
if (!endpointsLoading && selectedToolKey && !toolRegistry[selectedToolKey]) {
|
|
const firstAvailableTool = Object.keys(toolRegistry)[0];
|
|
if (firstAvailableTool) {
|
|
setSelectedToolKey(firstAvailableTool);
|
|
} else {
|
|
setSelectedToolKey(null);
|
|
}
|
|
}
|
|
}, [endpointsLoading, selectedToolKey, toolRegistry]);
|
|
|
|
const selectTool = useCallback((toolKey: string) => {
|
|
setSelectedToolKey(toolKey);
|
|
}, []);
|
|
|
|
const clearToolSelection = useCallback(() => {
|
|
setSelectedToolKey(null);
|
|
}, []);
|
|
|
|
const selectedTool = selectedToolKey ? toolRegistry[selectedToolKey] : null;
|
|
|
|
return {
|
|
selectedToolKey,
|
|
selectedTool,
|
|
toolSelectedFileIds,
|
|
toolRegistry,
|
|
|
|
selectTool,
|
|
clearToolSelection,
|
|
setToolSelectedFileIds,
|
|
|
|
};
|
|
};
|