2025-08-26 09:44:30 +01:00
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
2025-08-22 14:40:27 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { AutomationTool, AutomationConfig, AutomationMode } from '../../../types/automation';
|
|
|
|
import { AUTOMATION_CONSTANTS } from '../../../constants/automation';
|
|
|
|
import { ToolRegistryEntry } from '../../../data/toolsTaxonomy';
|
|
|
|
|
|
|
|
interface UseAutomationFormProps {
|
|
|
|
mode: AutomationMode;
|
|
|
|
existingAutomation?: AutomationConfig;
|
|
|
|
toolRegistry: Record<string, ToolRegistryEntry>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAutomationForm({ mode, existingAutomation, toolRegistry }: UseAutomationFormProps) {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
const [automationName, setAutomationName] = useState('');
|
2025-08-26 16:59:03 +01:00
|
|
|
const [automationDescription, setAutomationDescription] = useState('');
|
|
|
|
const [automationIcon, setAutomationIcon] = useState<string>('');
|
2025-08-22 14:40:27 +01:00
|
|
|
const [selectedTools, setSelectedTools] = useState<AutomationTool[]>([]);
|
|
|
|
|
2025-08-26 09:44:30 +01:00
|
|
|
const getToolName = useCallback((operation: string) => {
|
2025-08-22 14:40:27 +01:00
|
|
|
const tool = toolRegistry?.[operation] as any;
|
|
|
|
return tool?.name || t(`tools.${operation}.name`, operation);
|
2025-08-26 09:44:30 +01:00
|
|
|
}, [toolRegistry, t]);
|
2025-08-22 14:40:27 +01:00
|
|
|
|
2025-08-26 09:44:30 +01:00
|
|
|
const getToolDefaultParameters = useCallback((operation: string): Record<string, any> => {
|
2025-08-22 14:40:27 +01:00
|
|
|
const config = toolRegistry[operation]?.operationConfig;
|
|
|
|
if (config?.defaultParameters) {
|
|
|
|
return { ...config.defaultParameters };
|
|
|
|
}
|
|
|
|
return {};
|
2025-08-26 09:44:30 +01:00
|
|
|
}, [toolRegistry]);
|
2025-08-22 14:40:27 +01:00
|
|
|
|
|
|
|
// Initialize based on mode and existing automation
|
|
|
|
useEffect(() => {
|
|
|
|
if ((mode === AutomationMode.SUGGESTED || mode === AutomationMode.EDIT) && existingAutomation) {
|
|
|
|
setAutomationName(existingAutomation.name || '');
|
2025-08-26 16:59:03 +01:00
|
|
|
setAutomationDescription(existingAutomation.description || '');
|
|
|
|
setAutomationIcon(existingAutomation.icon || '');
|
2025-08-22 14:40:27 +01:00
|
|
|
|
|
|
|
const operations = existingAutomation.operations || [];
|
|
|
|
const tools = operations.map((op, index) => {
|
|
|
|
const operation = typeof op === 'string' ? op : op.operation;
|
|
|
|
return {
|
|
|
|
id: `${operation}-${Date.now()}-${index}`,
|
|
|
|
operation: operation,
|
|
|
|
name: getToolName(operation),
|
|
|
|
configured: mode === AutomationMode.EDIT ? true : false,
|
|
|
|
parameters: typeof op === 'object' ? op.parameters || {} : {}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
setSelectedTools(tools);
|
|
|
|
} else if (mode === AutomationMode.CREATE && selectedTools.length === 0) {
|
|
|
|
// Initialize with default empty tools for new automation
|
|
|
|
const defaultTools = Array.from({ length: AUTOMATION_CONSTANTS.DEFAULT_TOOL_COUNT }, (_, index) => ({
|
|
|
|
id: `tool-${index + 1}-${Date.now()}`,
|
|
|
|
operation: '',
|
|
|
|
name: t('automate.creation.tools.selectTool', 'Select a tool...'),
|
|
|
|
configured: false,
|
|
|
|
parameters: {}
|
|
|
|
}));
|
|
|
|
setSelectedTools(defaultTools);
|
|
|
|
}
|
2025-08-26 09:44:30 +01:00
|
|
|
}, [mode, existingAutomation, t, getToolName]);
|
2025-08-22 14:40:27 +01:00
|
|
|
|
|
|
|
const addTool = (operation: string) => {
|
|
|
|
const newTool: AutomationTool = {
|
|
|
|
id: `${operation}-${Date.now()}`,
|
|
|
|
operation,
|
|
|
|
name: getToolName(operation),
|
|
|
|
configured: false,
|
|
|
|
parameters: getToolDefaultParameters(operation)
|
|
|
|
};
|
|
|
|
|
|
|
|
setSelectedTools([...selectedTools, newTool]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeTool = (index: number) => {
|
|
|
|
if (selectedTools.length <= AUTOMATION_CONSTANTS.MIN_TOOL_COUNT) return;
|
|
|
|
setSelectedTools(selectedTools.filter((_, i) => i !== index));
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateTool = (index: number, updates: Partial<AutomationTool>) => {
|
|
|
|
const updatedTools = [...selectedTools];
|
|
|
|
updatedTools[index] = { ...updatedTools[index], ...updates };
|
|
|
|
setSelectedTools(updatedTools);
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasUnsavedChanges = () => {
|
|
|
|
return (
|
|
|
|
automationName.trim() !== '' ||
|
|
|
|
selectedTools.some(tool => tool.operation !== '' || tool.configured)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const canSaveAutomation = () => {
|
|
|
|
return (
|
|
|
|
automationName.trim() !== '' &&
|
|
|
|
selectedTools.length > 0 &&
|
|
|
|
selectedTools.every(tool => tool.configured && tool.operation !== '')
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
automationName,
|
|
|
|
setAutomationName,
|
2025-08-26 16:59:03 +01:00
|
|
|
automationDescription,
|
|
|
|
setAutomationDescription,
|
|
|
|
automationIcon,
|
|
|
|
setAutomationIcon,
|
2025-08-22 14:40:27 +01:00
|
|
|
selectedTools,
|
|
|
|
setSelectedTools,
|
|
|
|
addTool,
|
|
|
|
removeTool,
|
|
|
|
updateTool,
|
|
|
|
hasUnsavedChanges,
|
|
|
|
canSaveAutomation,
|
|
|
|
getToolName,
|
|
|
|
getToolDefaultParameters
|
|
|
|
};
|
|
|
|
}
|