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-20 18:47:34 +01:00
|
|
|
import AutomationRun from "../components/tools/automate/AutomationRun";
|
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-20 18:47:34 +01:00
|
|
|
const [currentStep, setCurrentStep] = useState<'selection' | 'creation' | 'run'>('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:47:34 +01:00
|
|
|
const hasResults = automateOperation.files.length > 0 || automateOperation.downloadUrl !== null;
|
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 })}
|
2025-08-20 18:47:34 +01:00
|
|
|
onRun={(automation: any) => handleStepChange({ step: 'run', automation })}
|
2025-08-20 18:30:33 +01:00
|
|
|
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-20 18:47:34 +01:00
|
|
|
onComplete={() => handleStepChange({ step: 'selection' })}
|
2025-08-19 16:50:55 +01:00
|
|
|
toolRegistry={toolRegistry}
|
2025-08-19 15:28:27 +01:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2025-08-20 18:47:34 +01:00
|
|
|
case 'run':
|
2025-08-19 15:28:27 +01:00
|
|
|
return (
|
2025-08-20 18:47:34 +01:00
|
|
|
<AutomationRun
|
2025-08-19 15:28:27 +01:00
|
|
|
automation={stepData.automation}
|
2025-08-20 18:47:34 +01:00
|
|
|
onBack={() => handleStepChange({ step: 'selection'})}
|
2025-08-19 12:46:09 +01:00
|
|
|
onComplete={handleComplete}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return <div>{t('automate.invalidStep', 'Invalid step')}</div>;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return createToolFlow({
|
|
|
|
files: {
|
|
|
|
selectedFiles: [],
|
2025-08-20 18:47:34 +01:00
|
|
|
isCollapsed: hasResults, // Hide files step for automate tool
|
2025-08-19 12:46:09 +01:00
|
|
|
},
|
|
|
|
steps: [
|
|
|
|
{
|
2025-08-20 18:47:34 +01:00
|
|
|
title: t('automate.selection.title', 'Automation Selection'),
|
2025-08-19 12:46:09 +01:00
|
|
|
isVisible: true,
|
2025-08-20 18:47:34 +01:00
|
|
|
isCollapsed: currentStep !== 'selection',
|
|
|
|
onCollapsedClick: () => setCurrentStep('selection'),
|
2025-08-19 15:28:27 +01:00
|
|
|
content: currentStep === 'selection' ? renderCurrentStep() : null
|
|
|
|
},
|
|
|
|
{
|
2025-08-20 18:47:34 +01:00
|
|
|
title: stepData.mode === AutomationMode.EDIT
|
|
|
|
? t('automate.creation.editTitle', 'Edit Automation')
|
|
|
|
: t('automate.creation.createTitle', 'Create Automation'),
|
|
|
|
isVisible: currentStep === 'creation',
|
|
|
|
isCollapsed: false,
|
|
|
|
content: currentStep === 'creation' ? renderCurrentStep() : null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t('automate.run.title', 'Run Automation'),
|
|
|
|
isVisible: currentStep === 'run',
|
|
|
|
isCollapsed: false,
|
|
|
|
content: currentStep === 'run' ? renderCurrentStep() : null
|
2025-08-19 12:46:09 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
review: {
|
2025-08-20 18:47:34 +01:00
|
|
|
isVisible: hasResults, // Hide review step for automate tool
|
2025-08-19 12:46:09 +01:00
|
|
|
operation: automateOperation,
|
|
|
|
title: t('automate.reviewTitle', 'Automation Results')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Automate;
|