mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-27 14:49:23 +00:00
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
![]() |
import { useState, useEffect, useCallback } from 'react';
|
||
|
import { AutomationConfig } from '../../../services/automationStorage';
|
||
|
|
||
|
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]);
|
||
|
|
||
|
// Load automations on mount
|
||
|
useEffect(() => {
|
||
|
loadSavedAutomations();
|
||
|
}, [loadSavedAutomations]);
|
||
|
|
||
|
return {
|
||
|
savedAutomations,
|
||
|
loading,
|
||
|
error,
|
||
|
refreshAutomations,
|
||
|
deleteAutomation
|
||
|
};
|
||
|
}
|