Files
bakery-ia/frontend/src/hooks/business/onboarding/useOnboarding.ts
2025-09-07 22:54:14 +02:00

144 lines
5.1 KiB
TypeScript

/**
* Main onboarding hook - Clean, unified interface for components
* This is the primary hook that all components should use
*/
import { useAuthUser } from '../../../stores/auth.store';
import { useCurrentTenant } from '../../../stores';
import { useOnboardingStore } from './core/store';
import { useOnboardingActions } from './core/actions';
import { useTenantCreation } from './services/useTenantCreation';
import { useSalesProcessing } from './services/useSalesProcessing';
import { useInventorySetup } from './services/useInventorySetup';
import { useTrainingOrchestration } from './services/useTrainingOrchestration';
import { useProgressTracking } from './services/useProgressTracking';
import { useResumeLogic } from './services/useResumeLogic';
export const useOnboarding = () => {
const user = useAuthUser();
const currentTenant = useCurrentTenant();
// Core store and actions
const store = useOnboardingStore();
const actions = useOnboardingActions();
// Service hooks for detailed state access
const tenantCreation = useTenantCreation();
const salesProcessing = useSalesProcessing();
const inventorySetup = useInventorySetup();
const trainingOrchestration = useTrainingOrchestration();
const progressTracking = useProgressTracking();
const resumeLogic = useResumeLogic();
return {
// Core state from store
currentStep: store.currentStep, // Return the index, not the step object
steps: store.steps,
data: store.data,
progress: store.getProgress(),
isLoading: store.isLoading,
error: store.error,
// User context
user,
currentTenant,
// Step data helpers
stepData: {
setup: store.getStepData('setup'),
'smart-inventory-setup': store.getStepData('smart-inventory-setup'),
suppliers: store.getStepData('suppliers'),
'ml-training': store.getStepData('ml-training'),
completion: store.getStepData('completion'),
},
// Service states (for components that need detailed service info)
tenantCreation: {
isLoading: tenantCreation.isLoading,
isSuccess: tenantCreation.isSuccess,
error: tenantCreation.error,
tenantData: tenantCreation.tenantData,
},
salesProcessing: {
isLoading: salesProcessing.isLoading,
error: salesProcessing.error,
stage: salesProcessing.stage,
progress: salesProcessing.progress,
currentMessage: salesProcessing.currentMessage,
validationResults: salesProcessing.validationResults,
suggestions: salesProcessing.suggestions,
},
inventorySetup: {
isLoading: inventorySetup.isLoading,
error: inventorySetup.error,
createdItems: inventorySetup.createdItems,
inventoryMapping: inventorySetup.inventoryMapping,
salesImportResult: inventorySetup.salesImportResult,
isInventoryConfigured: inventorySetup.isInventoryConfigured,
},
trainingOrchestration: {
isLoading: trainingOrchestration.isLoading,
error: trainingOrchestration.error,
status: trainingOrchestration.status,
progress: trainingOrchestration.progress,
currentStep: trainingOrchestration.currentStep,
estimatedTimeRemaining: trainingOrchestration.estimatedTimeRemaining,
job: trainingOrchestration.job,
logs: trainingOrchestration.logs,
metrics: trainingOrchestration.metrics,
},
progressTracking: {
isLoading: progressTracking.isLoading,
error: progressTracking.error,
progress: progressTracking.progress,
isCompleted: progressTracking.isCompleted,
completionPercentage: progressTracking.completionPercentage,
isInitialized: progressTracking.isInitialized,
currentBackendStep: progressTracking.currentBackendStep,
},
resumeLogic: {
isCheckingResume: resumeLogic.isCheckingResume,
resumePoint: resumeLogic.resumePoint,
shouldResume: resumeLogic.shouldResume,
progress: resumeLogic.progress,
isCompleted: resumeLogic.isCompleted,
completionPercentage: resumeLogic.completionPercentage,
},
// Actions from the core actions hook
nextStep: actions.nextStep,
previousStep: actions.previousStep,
goToStep: actions.goToStep,
validateCurrentStep: actions.validateCurrentStep,
// Step data management
updateStepData: store.setStepData,
clearStepData: store.clearStepData,
// Step-specific actions
createTenant: actions.createTenant,
processSalesFile: actions.processSalesFile,
createInventoryFromSuggestions: actions.createInventoryFromSuggestions,
importSalesData: actions.importSalesData,
startTraining: actions.startTraining,
completeOnboarding: actions.completeOnboarding,
// Service-specific actions (for components that need direct service access)
generateSuggestions: salesProcessing.generateSuggestions,
addTrainingLog: trainingOrchestration.addLog,
validateTrainingData: trainingOrchestration.validateTrainingData,
// Resume actions
checkForSavedProgress: resumeLogic.checkForResume,
resumeFromSavedProgress: resumeLogic.resumeFlow,
// Utility actions
clearError: actions.clearError,
reset: actions.reset,
};
};