mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-24 04:26:14 +00:00

Added Filewithid type Updated code where file was being used to use filewithid Updated places we identified files by name or composite keys to use UUID Updated places we should have been using quickkey Updated pageeditor issue where we parsed pagenumber from pageid instead of using pagenumber directly --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: James Brunton <jbrunton96@gmail.com>
127 lines
3.4 KiB
TypeScript
127 lines
3.4 KiB
TypeScript
import { useEffect, useCallback } from 'react';
|
|
import { useFileSelection } from '../../../contexts/FileContext';
|
|
import { useEndpointEnabled } from '../../useEndpointConfig';
|
|
import { BaseToolProps } from '../../../types/tool';
|
|
import { ToolOperationHook } from './useToolOperation';
|
|
import { BaseParametersHook } from './useBaseParameters';
|
|
import { StirlingFile } from '../../../types/fileContext';
|
|
|
|
interface BaseToolReturn<TParams> {
|
|
// File management
|
|
selectedFiles: StirlingFile[];
|
|
|
|
// Tool-specific hooks
|
|
params: BaseParametersHook<TParams>;
|
|
operation: ToolOperationHook<TParams>;
|
|
|
|
// Endpoint validation
|
|
endpointEnabled: boolean | null;
|
|
endpointLoading: boolean;
|
|
|
|
// Standard handlers
|
|
handleExecute: () => Promise<void>;
|
|
handleThumbnailClick: (file: File) => void;
|
|
handleSettingsReset: () => void;
|
|
handleUndo: () => Promise<void>;
|
|
|
|
// Standard computed state
|
|
hasFiles: boolean;
|
|
hasResults: boolean;
|
|
settingsCollapsed: boolean;
|
|
}
|
|
|
|
/**
|
|
* Base tool hook for tool components. Manages standard behaviour for tools.
|
|
*/
|
|
export function useBaseTool<TParams>(
|
|
toolName: string,
|
|
useParams: () => BaseParametersHook<TParams>,
|
|
useOperation: () => ToolOperationHook<TParams>,
|
|
props: BaseToolProps,
|
|
): BaseToolReturn<TParams> {
|
|
const { onPreviewFile, onComplete, onError } = props;
|
|
|
|
// File selection
|
|
const { selectedFiles } = useFileSelection();
|
|
|
|
// Tool-specific hooks
|
|
const params = useParams();
|
|
const operation = useOperation();
|
|
|
|
// Endpoint validation using parameters hook
|
|
const { enabled: endpointEnabled, loading: endpointLoading } = useEndpointEnabled(params.getEndpointName());
|
|
|
|
// Reset results when parameters change
|
|
useEffect(() => {
|
|
operation.resetResults();
|
|
onPreviewFile?.(null);
|
|
}, [params.parameters]);
|
|
|
|
// Reset results when selected files change
|
|
useEffect(() => {
|
|
if (selectedFiles.length > 0) {
|
|
operation.resetResults();
|
|
onPreviewFile?.(null);
|
|
}
|
|
}, [selectedFiles.length]);
|
|
|
|
// Standard handlers
|
|
const handleExecute = useCallback(async () => {
|
|
try {
|
|
await operation.executeOperation(params.parameters, selectedFiles);
|
|
if (operation.files && onComplete) {
|
|
onComplete(operation.files);
|
|
}
|
|
} catch (error) {
|
|
if (onError) {
|
|
const message = error instanceof Error ? error.message : `${toolName} operation failed`;
|
|
onError(message);
|
|
}
|
|
}
|
|
}, [operation, params.parameters, selectedFiles, onComplete, onError, toolName]);
|
|
|
|
const handleThumbnailClick = useCallback((file: File) => {
|
|
onPreviewFile?.(file);
|
|
sessionStorage.setItem('previousMode', toolName);
|
|
}, [onPreviewFile, toolName]);
|
|
|
|
const handleSettingsReset = useCallback(() => {
|
|
operation.resetResults();
|
|
onPreviewFile?.(null);
|
|
}, [operation, onPreviewFile]);
|
|
|
|
const handleUndo = useCallback(async () => {
|
|
await operation.undoOperation();
|
|
onPreviewFile?.(null);
|
|
}, [operation, onPreviewFile]);
|
|
|
|
// Standard computed state
|
|
const hasFiles = selectedFiles.length > 0;
|
|
const hasResults = operation.files.length > 0 || operation.downloadUrl !== null;
|
|
const settingsCollapsed = !hasFiles || hasResults;
|
|
|
|
return {
|
|
// File management
|
|
selectedFiles,
|
|
|
|
// Tool-specific hooks
|
|
params,
|
|
operation,
|
|
|
|
// Endpoint validation
|
|
endpointEnabled,
|
|
endpointLoading,
|
|
|
|
// Handlers
|
|
handleExecute,
|
|
handleThumbnailClick,
|
|
handleSettingsReset,
|
|
handleUndo,
|
|
|
|
// State
|
|
hasFiles,
|
|
hasResults,
|
|
settingsCollapsed
|
|
};
|
|
}
|