Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-08-19 12:46:09 +01:00
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { useFileContext } from "../contexts/FileContext";
import { useToolFileSelection } from "../contexts/FileSelectionContext";
import { createToolFlow } from "../components/tools/shared/createToolFlow";
import AutomationSelection from "../components/tools/automate/AutomationSelection";
2025-08-20 18:30:33 +01:00
import AutomationCreation, { AutomationMode } from "../components/tools/automate/AutomationCreation";
2025-08-19 15:28:27 +01:00
import ToolSequence from "../components/tools/automate/ToolSequence";
2025-08-19 12:46:09 +01:00
import { useAutomateOperation } from "../hooks/tools/automate/useAutomateOperation";
import { BaseToolProps } from "../types/tool";
2025-08-19 16:50:55 +01:00
import { useFlatToolRegistry } from "../data/useTranslatedToolRegistry";
2025-08-20 18:30:33 +01:00
import { useSavedAutomations } from "../hooks/tools/automate/useSavedAutomations";
2025-08-19 12:46:09 +01:00
const Automate = ({ onPreviewFile, onComplete, onError }: BaseToolProps) => {
const { t } = useTranslation();
const { setCurrentMode } = useFileContext();
const { selectedFiles } = useToolFileSelection();
2025-08-19 15:28:27 +01:00
const [currentStep, setCurrentStep] = useState<'selection' | 'creation' | 'sequence'>('selection');
2025-08-19 12:46:09 +01:00
const [stepData, setStepData] = useState<any>({});
const automateOperation = useAutomateOperation();
2025-08-19 16:50:55 +01:00
const toolRegistry = useFlatToolRegistry();
2025-08-20 18:30:33 +01:00
const { savedAutomations, deleteAutomation } = useSavedAutomations();
2025-08-19 12:46:09 +01:00
const handleStepChange = (data: any) => {
setStepData(data);
setCurrentStep(data.step);
};
const handleComplete = () => {
// Reset to selection step
setCurrentStep('selection');
setStepData({});
onComplete?.([]); // Pass empty array since automation creation doesn't produce files
};
const renderCurrentStep = () => {
switch (currentStep) {
case 'selection':
return (
<AutomationSelection
2025-08-20 18:30:33 +01:00
savedAutomations={savedAutomations}
onCreateNew={() => handleStepChange({ step: 'creation', mode: AutomationMode.CREATE })}
onRun={(automation: any) => handleStepChange({ step: 'sequence', automation })}
onEdit={(automation: any) => handleStepChange({ step: 'creation', mode: AutomationMode.EDIT, automation })}
onDelete={async (automation: any) => {
try {
await deleteAutomation(automation.id);
} catch (error) {
console.error('Failed to delete automation:', error);
onError?.(`Failed to delete automation: ${automation.name}`);
}
}}
2025-08-19 12:46:09 +01:00
/>
);
case 'creation':
return (
<AutomationCreation
mode={stepData.mode}
existingAutomation={stepData.automation}
onBack={() => handleStepChange({ step: 'selection' })}
2025-08-19 15:28:27 +01:00
onComplete={(automation: any) => handleStepChange({ step: 'sequence', automation })}
2025-08-19 16:50:55 +01:00
toolRegistry={toolRegistry}
2025-08-19 15:28:27 +01:00
/>
);
case 'sequence':
return (
<ToolSequence
automation={stepData.automation}
onBack={() => handleStepChange({ step: 'creation', mode: stepData.mode, automation: stepData.automation })}
2025-08-19 12:46:09 +01:00
onComplete={handleComplete}
/>
);
default:
return <div>{t('automate.invalidStep', 'Invalid step')}</div>;
}
};
return createToolFlow({
files: {
selectedFiles: [],
isCollapsed: true, // Hide files step for automate tool
placeholder: t('automate.filesHidden', 'Files will be selected during automation execution')
},
steps: [
{
title: t('automate.stepTitle', 'Automations'),
isVisible: true,
2025-08-19 17:38:29 +01:00
onCollapsedClick: ()=> setCurrentStep('selection'),
2025-08-19 15:28:27 +01:00
content: currentStep === 'selection' ? renderCurrentStep() : null
},
{
title: t('automate.sequenceTitle', 'Tool Sequence'),
isVisible: currentStep === 'creation' || currentStep === 'sequence',
content: currentStep === 'creation' || currentStep === 'sequence' ? renderCurrentStep() : null
2025-08-19 12:46:09 +01:00
}
],
review: {
isVisible: false, // Hide review step for automate tool
operation: automateOperation,
title: t('automate.reviewTitle', 'Automation Results')
}
});
};
export default Automate;