|
| 1 | +import React, { FC, useEffect, useRef } from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + logVMCreationStarted, |
| 5 | + mapWizardStepToCreationMethodTelemetry, |
| 6 | +} from '@kubevirt-utils/extensions/telemetry/vm-creation'; |
| 7 | +import { useIsAdmin } from '@kubevirt-utils/hooks/useIsAdmin'; |
| 8 | +import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation'; |
| 9 | +import { getValidNamespace } from '@kubevirt-utils/utils/utils'; |
| 10 | +import useClusterParam from '@multicluster/hooks/useClusterParam'; |
| 11 | +import { useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk'; |
| 12 | +import { Wizard, WizardHeader, WizardStep } from '@patternfly/react-core'; |
| 13 | +import DefaultWizardFooter from '@virtualmachines/creation-wizard-new/components/DefaultWizardFooter'; |
| 14 | +import useCloseWizard from '@virtualmachines/creation-wizard-new/hooks/useCloseWizard'; |
| 15 | +import { useSyncDeploymentDetails } from '@virtualmachines/creation-wizard-new/hooks/useSyncDeploymentDetails'; |
| 16 | +import useVMGenerationNavItem from '@virtualmachines/creation-wizard-new/hooks/useVMGenerationNavItem'; |
| 17 | +import useWizardStepValidation from '@virtualmachines/creation-wizard-new/hooks/useWizardStepValidation'; |
| 18 | +import useVMWizardStore from '@virtualmachines/creation-wizard-new/state/vm-wizard-store/useVMWizardStore'; |
| 19 | +import CloneSourceStep from '@virtualmachines/creation-wizard-new/steps/CloneSourceStep/CloneSourceStep'; |
| 20 | +import CustomizationStep from '@virtualmachines/creation-wizard-new/steps/CustomizationStep/CustomizationStep'; |
| 21 | +import DeploymentDetailsStepFooter from '@virtualmachines/creation-wizard-new/steps/DeploymentDetailsStep/components/DeploymentDetailsStepFooter'; |
| 22 | +import BootSourceStep from '@virtualmachines/creation-wizard-new/steps/InstanceTypesSteps/BootSourceStep/BootSourceStep'; |
| 23 | +import ComputeResourcesStepFooter from '@virtualmachines/creation-wizard-new/steps/InstanceTypesSteps/ComputeResourcesStep/components/ComputeResourcesStepFooter'; |
| 24 | +import ComputeResourcesStep from '@virtualmachines/creation-wizard-new/steps/InstanceTypesSteps/ComputeResourcesStep/ComputeResourcesStep'; |
| 25 | +import GuestOSStep from '@virtualmachines/creation-wizard-new/steps/InstanceTypesSteps/GuestOSStep/GuestOSStep'; |
| 26 | +import ReviewAndCreateStepFooter from '@virtualmachines/creation-wizard-new/steps/ReviewAndCreateStep/components/ReviewAndCreateStepFooter'; |
| 27 | +import ReviewAndCreateStep from '@virtualmachines/creation-wizard-new/steps/ReviewAndCreateStep/ReviewAndCreateStep'; |
| 28 | +import TemplateStepFooter from '@virtualmachines/creation-wizard-new/steps/TemplateStep/components/TemplateStepFooter'; |
| 29 | +import TemplateStep from '@virtualmachines/creation-wizard-new/steps/TemplateStep/TemplateStep'; |
| 30 | +import { VMWizardStep } from '@virtualmachines/creation-wizard-new/utils/constants'; |
| 31 | +import { |
| 32 | + isCloneCreationMethod, |
| 33 | + isInstanceTypeCreationMethod, |
| 34 | + isTemplateCreationMethod, |
| 35 | +} from '@virtualmachines/creation-wizard-new/utils/utils'; |
| 36 | + |
| 37 | +import TemplatesDrawerWrapper from './components/TemplatesDrawerWrapper'; |
| 38 | +import DeploymentDetailsStep from './steps/DeploymentDetailsStep/DeploymentDetailsStep'; |
| 39 | + |
| 40 | +import './Wizard.scss'; |
| 41 | + |
| 42 | +const VMCreationWizard: FC = () => { |
| 43 | + const { t } = useKubevirtTranslation(); |
| 44 | + const { |
| 45 | + creationMethod, |
| 46 | + initializeVMCreationWizardValues, |
| 47 | + markStepVisited, |
| 48 | + resetWizardState, |
| 49 | + setTemplatesDrawerIsOpen, |
| 50 | + } = useVMWizardStore(); |
| 51 | + const syncDeploymentDetails = useSyncDeploymentDetails(); |
| 52 | + const { isNextDisabledForStep, isStepDisabled } = useWizardStepValidation(); |
| 53 | + const { navItemWithVMGeneration } = useVMGenerationNavItem(creationMethod); |
| 54 | + const clusterParam = useClusterParam(); |
| 55 | + const hasInitialized = useRef(false); |
| 56 | + const hasLoggedCreationStarted = useRef(false); |
| 57 | + const closeWizard = useCloseWizard(); |
| 58 | + const isAdmin = useIsAdmin(); |
| 59 | + const [activeNamespace] = useActiveNamespace(); |
| 60 | + const namespace = getValidNamespace(activeNamespace); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (!hasInitialized.current) { |
| 64 | + setTemplatesDrawerIsOpen(false); |
| 65 | + initializeVMCreationWizardValues({ cluster: clusterParam, isAdmin, namespace }); |
| 66 | + hasInitialized.current = true; |
| 67 | + } |
| 68 | + |
| 69 | + return () => resetWizardState(); |
| 70 | + }, [ |
| 71 | + clusterParam, |
| 72 | + isAdmin, |
| 73 | + namespace, |
| 74 | + resetWizardState, |
| 75 | + setTemplatesDrawerIsOpen, |
| 76 | + initializeVMCreationWizardValues, |
| 77 | + ]); |
| 78 | + |
| 79 | + const isInstanceTypeMethod = isInstanceTypeCreationMethod(creationMethod); |
| 80 | + const isCloneMethod = isCloneCreationMethod(creationMethod); |
| 81 | + const isTemplateMethod = isTemplateCreationMethod(creationMethod); |
| 82 | + |
| 83 | + return ( |
| 84 | + <TemplatesDrawerWrapper> |
| 85 | + <Wizard |
| 86 | + onStepChange={(_, currentStep, prevStep) => { |
| 87 | + syncDeploymentDetails(currentStep, prevStep); |
| 88 | + if (currentStep?.id !== VMWizardStep.TEMPLATE) setTemplatesDrawerIsOpen(false); |
| 89 | + if (currentStep?.id) markStepVisited(String(currentStep.id)); |
| 90 | + |
| 91 | + const creationMethodTelemetry = mapWizardStepToCreationMethodTelemetry( |
| 92 | + String(currentStep?.id), |
| 93 | + ); |
| 94 | + if (!hasLoggedCreationStarted.current && creationMethodTelemetry) { |
| 95 | + hasLoggedCreationStarted.current = true; |
| 96 | + logVMCreationStarted(creationMethodTelemetry); |
| 97 | + } |
| 98 | + }} |
| 99 | + className="vm-creation-wizard" |
| 100 | + header={<WizardHeader isCloseHidden title={t('Create VirtualMachine')} />} |
| 101 | + onClose={closeWizard} |
| 102 | + title={t('Create VirtualMachine')} |
| 103 | + > |
| 104 | + <WizardStep |
| 105 | + footer={<DeploymentDetailsStepFooter />} |
| 106 | + id={VMWizardStep.DEPLOYMENT_DETAILS} |
| 107 | + name={t('Deployment details')} |
| 108 | + > |
| 109 | + <DeploymentDetailsStep /> |
| 110 | + </WizardStep> |
| 111 | + <WizardStep |
| 112 | + footer={{ |
| 113 | + isNextDisabled: isNextDisabledForStep(VMWizardStep.GUEST_OS), |
| 114 | + }} |
| 115 | + id={VMWizardStep.GUEST_OS} |
| 116 | + isDisabled={isStepDisabled(VMWizardStep.GUEST_OS)} |
| 117 | + isHidden={!isInstanceTypeMethod} |
| 118 | + name={t('Guest OS')} |
| 119 | + > |
| 120 | + <GuestOSStep /> |
| 121 | + </WizardStep> |
| 122 | + <WizardStep |
| 123 | + footer={{ |
| 124 | + isNextDisabled: isNextDisabledForStep(VMWizardStep.BOOT_SOURCE), |
| 125 | + }} |
| 126 | + id={VMWizardStep.BOOT_SOURCE} |
| 127 | + isDisabled={isStepDisabled(VMWizardStep.BOOT_SOURCE)} |
| 128 | + isHidden={!isInstanceTypeMethod} |
| 129 | + name={t('Boot source')} |
| 130 | + > |
| 131 | + <BootSourceStep /> |
| 132 | + </WizardStep> |
| 133 | + <WizardStep |
| 134 | + footer={<ComputeResourcesStepFooter />} |
| 135 | + id={VMWizardStep.COMPUTE_RESOURCES} |
| 136 | + isDisabled={isStepDisabled(VMWizardStep.COMPUTE_RESOURCES)} |
| 137 | + isHidden={!isInstanceTypeMethod} |
| 138 | + name={t('Compute resources')} |
| 139 | + > |
| 140 | + <ComputeResourcesStep /> |
| 141 | + </WizardStep> |
| 142 | + |
| 143 | + <WizardStep |
| 144 | + footer={<TemplateStepFooter />} |
| 145 | + id={VMWizardStep.TEMPLATE} |
| 146 | + isDisabled={isStepDisabled(VMWizardStep.TEMPLATE)} |
| 147 | + isHidden={!isTemplateMethod} |
| 148 | + name={t('Template')} |
| 149 | + > |
| 150 | + <TemplateStep /> |
| 151 | + </WizardStep> |
| 152 | + <WizardStep |
| 153 | + footer={<DefaultWizardFooter />} |
| 154 | + id={VMWizardStep.CUSTOMIZATION} |
| 155 | + isDisabled={isStepDisabled(VMWizardStep.CUSTOMIZATION)} |
| 156 | + isHidden={isCloneMethod} |
| 157 | + name={t('Customization')} |
| 158 | + navItem={navItemWithVMGeneration} |
| 159 | + > |
| 160 | + <CustomizationStep /> |
| 161 | + </WizardStep> |
| 162 | + <WizardStep |
| 163 | + footer={{ |
| 164 | + isNextDisabled: isNextDisabledForStep(VMWizardStep.CLONE), |
| 165 | + }} |
| 166 | + id={VMWizardStep.CLONE} |
| 167 | + isDisabled={isStepDisabled(VMWizardStep.CLONE)} |
| 168 | + isHidden={!isCloneMethod} |
| 169 | + name={t('Source')} |
| 170 | + > |
| 171 | + <CloneSourceStep /> |
| 172 | + </WizardStep> |
| 173 | + <WizardStep |
| 174 | + footer={<ReviewAndCreateStepFooter />} |
| 175 | + id={VMWizardStep.REVIEW_AND_CREATE} |
| 176 | + isDisabled={isStepDisabled(VMWizardStep.REVIEW_AND_CREATE)} |
| 177 | + name={t('Review and create')} |
| 178 | + navItem={navItemWithVMGeneration} |
| 179 | + > |
| 180 | + <ReviewAndCreateStep /> |
| 181 | + </WizardStep> |
| 182 | + </Wizard> |
| 183 | + </TemplatesDrawerWrapper> |
| 184 | + ); |
| 185 | +}; |
| 186 | + |
| 187 | +export default VMCreationWizard; |
0 commit comments