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

- [x] Cleanup Automation output name garbage - [x] Remove Cross button on first two tools - [x] Automation creation name title to make clearer to the user - [x] Colours for dark mode on automation tool settings are bad - [x] Fix tool names not using correct translated ones - [x] suggested Automation Password needs adding to description - [x] Allow different filetypes in automation - [x] Custom Icons for automation - [x] split Tool wasn't working with merge to single pdf --------- Co-authored-by: Connor Yoh <connor@stirlingpdf.com> Co-authored-by: James Brunton <jbrunton96@gmail.com>
94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
import { useState, useEffect, useCallback } from 'react';
|
|
import { AutomationConfig } from '../../../services/automationStorage';
|
|
import { SuggestedAutomation } from '../../../types/automation';
|
|
|
|
export interface SavedAutomation extends AutomationConfig {}
|
|
|
|
export function useSavedAutomations() {
|
|
const [savedAutomations, setSavedAutomations] = useState<SavedAutomation[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<Error | null>(null);
|
|
|
|
const loadSavedAutomations = useCallback(async () => {
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
const { automationStorage } = await import('../../../services/automationStorage');
|
|
const automations = await automationStorage.getAllAutomations();
|
|
setSavedAutomations(automations);
|
|
} catch (err) {
|
|
console.error('Error loading saved automations:', err);
|
|
setError(err as Error);
|
|
setSavedAutomations([]);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
const refreshAutomations = useCallback(() => {
|
|
loadSavedAutomations();
|
|
}, [loadSavedAutomations]);
|
|
|
|
const deleteAutomation = useCallback(async (id: string) => {
|
|
try {
|
|
const { automationStorage } = await import('../../../services/automationStorage');
|
|
await automationStorage.deleteAutomation(id);
|
|
// Refresh the list after deletion
|
|
refreshAutomations();
|
|
} catch (err) {
|
|
console.error('Error deleting automation:', err);
|
|
throw err;
|
|
}
|
|
}, [refreshAutomations]);
|
|
|
|
const copyFromSuggested = useCallback(async (suggestedAutomation: SuggestedAutomation) => {
|
|
try {
|
|
const { automationStorage } = await import('../../../services/automationStorage');
|
|
|
|
// Map suggested automation icons to MUI icon keys
|
|
const getIconKey = (suggestedIcon: {id: string}): string => {
|
|
// Check the automation ID or name to determine the appropriate icon
|
|
switch (suggestedAutomation.id) {
|
|
case 'secure-pdf-ingestion':
|
|
case 'secure-workflow':
|
|
return 'SecurityIcon'; // Security icon for security workflows
|
|
case 'email-preparation':
|
|
return 'CompressIcon'; // Compression icon
|
|
case 'process-images':
|
|
return 'StarIcon'; // Star icon for process images
|
|
default:
|
|
return 'SettingsIcon'; // Default fallback
|
|
}
|
|
};
|
|
|
|
// Convert suggested automation to saved automation format
|
|
const savedAutomation = {
|
|
name: suggestedAutomation.name,
|
|
description: suggestedAutomation.description,
|
|
icon: getIconKey(suggestedAutomation.icon),
|
|
operations: suggestedAutomation.operations
|
|
};
|
|
|
|
await automationStorage.saveAutomation(savedAutomation);
|
|
// Refresh the list after saving
|
|
refreshAutomations();
|
|
} catch (err) {
|
|
console.error('Error copying suggested automation:', err);
|
|
throw err;
|
|
}
|
|
}, [refreshAutomations]);
|
|
|
|
// Load automations on mount
|
|
useEffect(() => {
|
|
loadSavedAutomations();
|
|
}, [loadSavedAutomations]);
|
|
|
|
return {
|
|
savedAutomations,
|
|
loading,
|
|
error,
|
|
refreshAutomations,
|
|
deleteAutomation,
|
|
copyFromSuggested
|
|
};
|
|
} |