diff --git a/libs/ui-lib/lib/ocm/components/AddHosts/day2Wizard/Day2ClusterDetails.tsx b/libs/ui-lib/lib/ocm/components/AddHosts/day2Wizard/Day2ClusterDetails.tsx index 7ee0d87e07..e14db480b2 100644 --- a/libs/ui-lib/lib/ocm/components/AddHosts/day2Wizard/Day2ClusterDetails.tsx +++ b/libs/ui-lib/lib/ocm/components/AddHosts/day2Wizard/Day2ClusterDetails.tsx @@ -23,7 +23,8 @@ import Day2WizardFooter from './Day2WizardFooter'; import Day2HostStaticIpConfigurations from './Day2StaticIpHostConfigurations'; import { mapClusterCpuArchToInfraEnvCpuArch } from '../../../services/CpuArchitectureService'; import CpuArchitectureDropdown from '../../clusterConfiguration/CpuArchitectureDropdown'; -import { useOpenshiftVersions, usePullSecret } from '../../../hooks'; +import { usePullSecret } from '../../../hooks'; +import { useOpenshiftVersionsContext } from '../../clusterWizard/OpenshiftVersionsContext'; import { Cluster, InfraEnv, @@ -86,7 +87,7 @@ const Day2ClusterDetails = () => { const [initialValues, setInitialValues] = React.useState(null); const [isSubmitting, setSubmitting] = React.useState(false); const [isAlternativeCpuSelected, setIsAlternativeCpuSelected] = React.useState(false); - const { getCpuArchitectures } = useOpenshiftVersions(); + const { getCpuArchitectures } = useOpenshiftVersionsContext(); const cpuArchitecturesByVersionImage = getCpuArchitectures(day2Cluster.openshiftVersion); const day1CpuArchitecture = mapClusterCpuArchToInfraEnvCpuArch(day2Cluster.cpuArchitecture); const [errorState, setErrorState] = React.useState(null); diff --git a/libs/ui-lib/lib/ocm/components/HostsClusterDetailTab/HostsClusterDetailTabMock.tsx b/libs/ui-lib/lib/ocm/components/HostsClusterDetailTab/HostsClusterDetailTabMock.tsx index 024fc4ab58..bd95a35d31 100644 --- a/libs/ui-lib/lib/ocm/components/HostsClusterDetailTab/HostsClusterDetailTabMock.tsx +++ b/libs/ui-lib/lib/ocm/components/HostsClusterDetailTab/HostsClusterDetailTabMock.tsx @@ -18,6 +18,7 @@ import { import { OcmClusterType } from '../AddHosts'; import HostsClusterDetailTab from './HostsClusterDetailTab'; import clustersAPI from '../../../common/api/assisted-service/ClustersAPI'; +import { OpenshiftVersionsContextProvider } from '../clusterWizard/OpenshiftVersionsContext'; const clusterWithoutMetrics = { id: 'ocm-cluster-id', @@ -81,11 +82,13 @@ export const HostsClusterDetailTabMock: React.FC< return ( {tabShown ? ( - + + + ) : ( onAddHosts('no-metrics')}> diff --git a/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmClusterDetailsFormFields.tsx b/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmClusterDetailsFormFields.tsx index 30d37279cc..28550fa831 100644 --- a/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmClusterDetailsFormFields.tsx +++ b/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmClusterDetailsFormFields.tsx @@ -28,7 +28,7 @@ import CpuArchitectureDropdown, { import { OcmBaseDomainField } from './OcmBaseDomainField'; import OcmSNOControlGroup from './OcmSNOControlGroup'; import useSupportLevelsAPI from '../../hooks/useSupportLevelsAPI'; -import { useOpenshiftVersions } from '../../hooks'; +import { useOpenshiftVersionsContext } from '../clusterWizard/OpenshiftVersionsContext'; import { ExternalPlatformDropdown } from './platformIntegration/ExternalPlatformDropdown'; import { HostsNetworkConfigurationType } from '../../services/types'; import { useNewFeatureSupportLevel } from '../../../common/components/newFeatureSupportLevels'; @@ -68,7 +68,7 @@ export const OcmClusterDetailsFormFields = ({ const isSingleClusterFeatureEnabled = useFeature('ASSISTED_INSTALLER_SINGLE_CLUSTER_FEATURE'); const isOracleCloudPlatformIntegrationEnabled = useFeature('ASSISTED_INSTALLER_PLATFORM_OCI'); const { openshiftVersion, platform } = values; - const { getCpuArchitectures } = useOpenshiftVersions(); + const { getCpuArchitectures } = useOpenshiftVersionsContext(); const cpuArchitecturesByVersionImage = getCpuArchitectures(openshiftVersion); const clusterWizardContext = useClusterWizardContext(); const featureSupportLevelData = useSupportLevelsAPI( diff --git a/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmOpenShiftVersionSelect.tsx b/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmOpenShiftVersionSelect.tsx index 2abd954924..c979639cc6 100644 --- a/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmOpenShiftVersionSelect.tsx +++ b/libs/ui-lib/lib/ocm/components/clusterConfiguration/OcmOpenShiftVersionSelect.tsx @@ -6,7 +6,7 @@ import { OpenShiftVersionDropdown } from '../../../common/components/ui/OpenShif import { OpenShiftVersionModal } from './OpenShiftVersionModal'; import { useFormikContext } from 'formik'; import { getOpenshiftVersionHelperText } from './OpenshiftVersionHelperText'; -import { useOpenshiftVersions } from '../../hooks'; +import { useOpenshiftVersionsContext } from '../clusterWizard/OpenshiftVersionsContext'; type OcmOpenShiftVersionSelectProps = { versions: OpenshiftVersionOptionType[]; @@ -50,7 +50,7 @@ const OcmOpenShiftVersionSelect = ({ versions }: OcmOpenShiftVersionSelectProps) return []; }, [selectOptions, customOpenshiftSelect]); - const { versions: allVersions } = useOpenshiftVersions(false); + const { allVersions } = useOpenshiftVersionsContext(); const getHelperText = (value: string | undefined, inModal?: boolean) => { return getOpenshiftVersionHelperText(allVersions, value, t, inModal); diff --git a/libs/ui-lib/lib/ocm/components/clusterConfiguration/OpenShiftVersionModal.tsx b/libs/ui-lib/lib/ocm/components/clusterConfiguration/OpenShiftVersionModal.tsx index 48a04976ab..0111b33a0b 100644 --- a/libs/ui-lib/lib/ocm/components/clusterConfiguration/OpenShiftVersionModal.tsx +++ b/libs/ui-lib/lib/ocm/components/clusterConfiguration/OpenShiftVersionModal.tsx @@ -2,7 +2,7 @@ import React, { useState } from 'react'; import { Button, ButtonVariant, FormGroup, Modal, ModalVariant } from '@patternfly/react-core'; import './OpenshiftVersionModal.css'; import { OpenshiftSelectWithSearch } from '../../../common/components/ui/OpenshiftSelectWithSearch'; -import { useOpenshiftVersions } from '../../hooks'; +import { useOpenshiftVersionsContext } from '../clusterWizard/OpenshiftVersionsContext'; import { HelperTextType } from '../../../common/components/ui/OpenShiftVersionDropdown'; import { useFormikContext } from 'formik'; import { ClusterDetailsValues, OpenshiftVersionOptionType } from '../../../common'; @@ -17,7 +17,7 @@ export const OpenShiftVersionModal = ({ getHelperText, }: OpenShiftVersionModalProps) => { const { setFieldValue } = useFormikContext(); - const { versions } = useOpenshiftVersions(false); + const { allVersions: versions } = useOpenshiftVersionsContext(); const onClose = () => setOpenshiftVersionModalOpen(false); const [customOpenshiftSelect, setCustomOpenshiftSelect] = useState(); // Cambiar el tipo según lo que esperes aquí diff --git a/libs/ui-lib/lib/ocm/components/clusterConfiguration/review/ReviewPreflightChecks.tsx b/libs/ui-lib/lib/ocm/components/clusterConfiguration/review/ReviewPreflightChecks.tsx index 87973029cd..130fff874f 100644 --- a/libs/ui-lib/lib/ocm/components/clusterConfiguration/review/ReviewPreflightChecks.tsx +++ b/libs/ui-lib/lib/ocm/components/clusterConfiguration/review/ReviewPreflightChecks.tsx @@ -22,7 +22,7 @@ import { UiIcon, } from '../../../../common'; import { useClusterWizardContext } from '../../clusterWizard/ClusterWizardContext'; -import { useOpenshiftVersions } from '../../../hooks'; +import { useOpenshiftVersionsContext } from '../../clusterWizard/OpenshiftVersionsContext'; import { wizardStepNames } from '../../clusterWizard/constants'; import { ClusterWizardStepsType, @@ -118,7 +118,7 @@ const getCheckIcon = (validationStatuses: string[]) => { const PreflightChecksDetailCollapsed = ({ cluster }: { cluster: Cluster }) => { const { t } = useTranslation(); const featureSupportLevelData = useNewFeatureSupportLevel(); - const { isSupportedOpenShiftVersion } = useOpenshiftVersions(); + const { isSupportedOpenShiftVersion } = useOpenshiftVersionsContext(); const { isFullySupported } = React.useMemo( () => getSupportLevelInfo(cluster, featureSupportLevelData, isSupportedOpenShiftVersion, t), diff --git a/libs/ui-lib/lib/ocm/components/clusterDetail/OpenShiftVersionDetail.tsx b/libs/ui-lib/lib/ocm/components/clusterDetail/OpenShiftVersionDetail.tsx index ba6f3a62a8..f40efbf1c2 100644 --- a/libs/ui-lib/lib/ocm/components/clusterDetail/OpenShiftVersionDetail.tsx +++ b/libs/ui-lib/lib/ocm/components/clusterDetail/OpenShiftVersionDetail.tsx @@ -1,6 +1,6 @@ import React from 'react'; import { UiIcon, getOpenshiftVersionText } from '../../../common'; -import { useOpenshiftVersions } from '../../hooks'; +import { useOpenshiftVersionsContext } from '../clusterWizard/OpenshiftVersionsContext'; import { ExclamationTriangleIcon } from '@patternfly/react-icons/dist/js/icons/exclamation-triangle-icon'; import { Popover, Text, TextContent, TextVariants } from '@patternfly/react-core'; import { useTranslation } from '../../../common/hooks/use-translation-wrapper'; @@ -28,7 +28,7 @@ const UnsupportedVersion = ({ version }: { version: string }) => { const OpenShiftVersionDetail = ({ cluster }: { cluster: Cluster }) => { const { openshiftVersion } = cluster; - const { isSupportedOpenShiftVersion, versions } = useOpenshiftVersions(false); + const { isSupportedOpenShiftVersion, latestVersions: versions } = useOpenshiftVersionsContext(); const isSupported = isSupportedOpenShiftVersion(openshiftVersion); const version = React.useMemo(() => { diff --git a/libs/ui-lib/lib/ocm/components/clusterWizard/ClusterDetails.tsx b/libs/ui-lib/lib/ocm/components/clusterWizard/ClusterDetails.tsx index 1889f690d3..4f461c23d6 100644 --- a/libs/ui-lib/lib/ocm/components/clusterWizard/ClusterDetails.tsx +++ b/libs/ui-lib/lib/ocm/components/clusterWizard/ClusterDetails.tsx @@ -7,7 +7,8 @@ import { getApiErrorMessage, handleApiError, isUnknownServerError } from '../../ import { setServerUpdateError, updateCluster } from '../../store/slices/current-cluster/slice'; import { useClusterWizardContext } from './ClusterWizardContext'; import { canNextClusterDetails, ClusterWizardFlowStateNew } from './wizardTransition'; -import { useOpenshiftVersions, useManagedDomains, useUsedClusterNames } from '../../hooks'; +import { useManagedDomains, useUsedClusterNames } from '../../hooks'; +import { useOpenshiftVersionsContext } from './OpenshiftVersionsContext'; import ClusterDetailsForm from './ClusterDetailsForm'; import ClusterWizardNavigation from './ClusterWizardNavigation'; import { routeBasePath } from '../../config'; @@ -35,8 +36,8 @@ const ClusterDetails = ({ cluster, infraEnv }: ClusterDetailsProps) => { const { error: errorOCPVersions, loading: loadingOCPVersions, - versions, - } = useOpenshiftVersions(true); + latestVersions: versions, + } = useOpenshiftVersionsContext(); const handleClusterUpdate = React.useCallback( async ( diff --git a/libs/ui-lib/lib/ocm/components/clusterWizard/OpenshiftVersionsContext.tsx b/libs/ui-lib/lib/ocm/components/clusterWizard/OpenshiftVersionsContext.tsx new file mode 100644 index 0000000000..88c493f74c --- /dev/null +++ b/libs/ui-lib/lib/ocm/components/clusterWizard/OpenshiftVersionsContext.tsx @@ -0,0 +1,131 @@ +import React, { createContext } from 'react'; +import { CpuArchitecture, OpenshiftVersionOptionType } from '../../../common'; +import { getApiErrorMessage, handleApiError } from '../../../common/api'; +import { SupportedOpenshiftVersionsAPI } from '../../services/apis'; +import { Cluster, OpenshiftVersion } from '@openshift-assisted/types/assisted-installer-service'; +import { getKeys } from '../../../common/utils'; + +const supportedVersionLevels = ['production', 'maintenance']; + +type OpenShiftVersion = Cluster['openshiftVersion']; +type OpenshiftVersionsContextType = { + allVersions: OpenshiftVersionOptionType[]; + latestVersions: OpenshiftVersionOptionType[]; + isSupportedOpenShiftVersion: (version: OpenShiftVersion) => boolean; + getCpuArchitectures: (version: OpenShiftVersion) => CpuArchitecture[]; + error?: { title: string; message: string }; + loading: boolean; +}; + +const OpenshiftVersionsContext = createContext(null); + +export const OpenshiftVersionsContextProvider = ({ children }: { children: React.ReactNode }) => { + const [allVersions, setAllVersions] = React.useState([]); + const [latestVersions, setLatestVersions] = React.useState([]); + const [error, setError] = React.useState<{ title: string; message: string }>(); + + const sortVersions = (versions: OpenshiftVersionOptionType[]) => { + return versions.sort((version1, version2) => + version1.value.localeCompare(version2.value, undefined, { numeric: true }), + ); + }; + + const getVersions = React.useCallback( + async (latest: boolean, setVersions: (versions: OpenshiftVersionOptionType[]) => void) => { + try { + const { data } = await SupportedOpenshiftVersionsAPI.list(latest); + + const versions = getKeys(data).map((key) => { + const versionItem = data[key] as OpenshiftVersion; + const version = versionItem.displayName; + + return { + label: `OpenShift ${version}`, + value: key as string, + version, + default: Boolean(versionItem.default), + supportLevel: versionItem.supportLevel, + cpuArchitectures: versionItem.cpuArchitectures as CpuArchitecture[], + }; + }) as OpenshiftVersionOptionType[]; + + setVersions(sortVersions(versions)); + } catch (e) { + handleApiError(e, (e) => { + setError({ + title: 'Failed to retrieve list of supported OpenShift versions.', + message: getApiErrorMessage(e), + }); + }); + } + }, + [], + ); + + React.useEffect(() => { + if (!allVersions.length) { + void getVersions(false, setAllVersions); + } + }, [allVersions, getVersions]); + + React.useEffect(() => { + if (!latestVersions.length) { + void getVersions(true, setLatestVersions); + } + }, [getVersions, latestVersions]); + + const findVersionItemByVersion = React.useCallback( + (version: OpenShiftVersion) => { + return allVersions?.find(({ value: versionKey }) => { + // For version 4.10 match 4.10, 4.10.3, not 4.1, 4.1.5 + const versionNameMatch = new RegExp(`^${versionKey}(\\..+)?$`); + return versionNameMatch.test(version || ''); + }); + }, + [allVersions], + ); + + const isSupportedOpenShiftVersion = React.useCallback( + (version: OpenShiftVersion) => { + if (allVersions?.length === 0) { + // Till the data are loaded + return true; + } + const selectedVersion = findVersionItemByVersion(version); + return supportedVersionLevels.includes(selectedVersion?.supportLevel || ''); + }, + [findVersionItemByVersion, allVersions], + ); + + const getCpuArchitectures = React.useCallback( + (version: OpenShiftVersion) => { + // TODO (multi-arch) confirm this is correctly retrieving the associated version + const matchingVersion = findVersionItemByVersion(version); + return matchingVersion?.cpuArchitectures ?? []; + }, + [findVersionItemByVersion], + ); + + return ( + + {children} + + ); +}; + +export const useOpenshiftVersionsContext = () => { + const context = React.useContext(OpenshiftVersionsContext); + if (!context) { + throw new Error('useOpenshiftVersionsContext must be used within OpenshiftVersionsContext'); + } + return context; +}; diff --git a/libs/ui-lib/lib/ocm/components/clusters/ClusterPage.tsx b/libs/ui-lib/lib/ocm/components/clusters/ClusterPage.tsx index 629954e6af..6560b04be3 100644 --- a/libs/ui-lib/lib/ocm/components/clusters/ClusterPage.tsx +++ b/libs/ui-lib/lib/ocm/components/clusters/ClusterPage.tsx @@ -33,6 +33,7 @@ import { NewFeatureSupportLevelProvider } from '../featureSupportLevels'; import { usePullSecret } from '../../hooks'; import { Cluster, InfraEnv } from '@openshift-assisted/types/assisted-installer-service'; import { AssistedInstallerHeader } from './AssistedInstallerHeader'; +import { OpenshiftVersionsContextProvider } from '../clusterWizard/OpenshiftVersionsContext'; type MatchParams = { clusterId: string; @@ -72,7 +73,9 @@ const ClusterPageGeneric: React.FC<{ clusterId: string; showBreadcrumbs?: boolea }; return ( - + + + ); } else if ( @@ -168,23 +171,25 @@ const ClusterPageGeneric: React.FC<{ clusterId: string; showBreadcrumbs?: boolea loadingUI={} errorUI={} > - } - cluster={cluster} - cpuArchitecture={infraEnv.cpuArchitecture as CpuArchitecture} - openshiftVersion={cluster.openshiftVersion} - platformType={cluster.platform?.type} - > - {/* TODO(mlibra): Will be reworked within https://issues.redhat.com/browse/AGENT-522 - - */} - {getContent(cluster, infraEnv)} - {uiState === ResourceUIState.POLLING_ERROR && } - {uiState === ResourceUIState.UPDATE_ERROR && } - - - - + + } + cluster={cluster} + cpuArchitecture={infraEnv.cpuArchitecture as CpuArchitecture} + openshiftVersion={cluster.openshiftVersion} + platformType={cluster.platform?.type} + > + {/* TODO(mlibra): Will be reworked within https://issues.redhat.com/browse/AGENT-522 + + */} + {getContent(cluster, infraEnv)} + {uiState === ResourceUIState.POLLING_ERROR && } + {uiState === ResourceUIState.UPDATE_ERROR && } + + + + + diff --git a/libs/ui-lib/lib/ocm/components/clusters/NewClusterPage.tsx b/libs/ui-lib/lib/ocm/components/clusters/NewClusterPage.tsx index 5a3a061a5f..f93b83470c 100644 --- a/libs/ui-lib/lib/ocm/components/clusters/NewClusterPage.tsx +++ b/libs/ui-lib/lib/ocm/components/clusters/NewClusterPage.tsx @@ -11,6 +11,7 @@ import { ClusterUiError } from './ClusterPageErrors'; import { NewFeatureSupportLevelProvider } from '../featureSupportLevels'; import { AssistedInstallerHeader } from './AssistedInstallerHeader'; import { ModalDialogsContextProvider } from '../hosts/ModalDialogsContext'; +import { OpenshiftVersionsContextProvider } from '../clusterWizard/OpenshiftVersionsContext'; const NewClusterPageGeneric: React.FC<{ pageTitleSection?: ReactNode }> = ({ pageTitleSection, @@ -23,14 +24,16 @@ const NewClusterPageGeneric: React.FC<{ pageTitleSection?: ReactNode }> = ({ loadingUI={} errorUI={} > - }> - {pageTitleSection} - - - - - - + + }> + {pageTitleSection} + + + + + + + diff --git a/libs/ui-lib/lib/ocm/components/featureSupportLevels/FeatureSupportLevelProvider.tsx b/libs/ui-lib/lib/ocm/components/featureSupportLevels/FeatureSupportLevelProvider.tsx index f7defe59f3..483480d19e 100644 --- a/libs/ui-lib/lib/ocm/components/featureSupportLevels/FeatureSupportLevelProvider.tsx +++ b/libs/ui-lib/lib/ocm/components/featureSupportLevels/FeatureSupportLevelProvider.tsx @@ -6,7 +6,7 @@ import { SupportedCpuArchitecture, getDefaultCpuArchitecture, } from '../../../common'; -import { useOpenshiftVersions, usePullSecret } from '../../hooks'; +import { usePullSecret } from '../../hooks'; import { getNewFeatureDisabledReason, isFeatureSupportedAndAvailable } from './featureStateUtils'; import useInfraEnv from '../../hooks/useInfraEnv'; import { @@ -21,6 +21,7 @@ import { SupportLevel, SupportLevels, } from '@openshift-assisted/types/assisted-installer-service'; +import { useOpenshiftVersionsContext } from '../clusterWizard/OpenshiftVersionsContext'; export type NewSupportLevelProviderProps = PropsWithChildren<{ clusterFeatureUsage?: string; @@ -43,7 +44,7 @@ export const NewFeatureSupportLevelProvider: React.FC { - const { loading: loadingOCPVersions } = useOpenshiftVersions(); + const { loading: loadingOCPVersions } = useOpenshiftVersionsContext(); const pullSecret = usePullSecret(); const { infraEnv, isLoading: isInfraEnvLoading } = useInfraEnv( cluster?.id || '', diff --git a/libs/ui-lib/lib/ocm/components/featureSupportLevels/ReviewClusterFeatureSupportLevels.tsx b/libs/ui-lib/lib/ocm/components/featureSupportLevels/ReviewClusterFeatureSupportLevels.tsx index 66b95432d6..ab80fe40c4 100644 --- a/libs/ui-lib/lib/ocm/components/featureSupportLevels/ReviewClusterFeatureSupportLevels.tsx +++ b/libs/ui-lib/lib/ocm/components/featureSupportLevels/ReviewClusterFeatureSupportLevels.tsx @@ -16,7 +16,7 @@ import { DetailItem, UiIcon } from '../../../common'; import { getLimitedFeatureSupportLevels } from '../../../common/components/newFeatureSupportLevels/utils'; import { WithErrorBoundary } from '../../../common/components/ErrorHandling/WithErrorBoundary'; import { useTranslation } from '../../../common/hooks/use-translation-wrapper'; -import useOpenshiftVersions from '../../hooks/useOpenshiftVersions'; +import { useOpenshiftVersionsContext } from '../clusterWizard/OpenshiftVersionsContext'; import { TFunction } from 'i18next'; import { NewFeatureSupportLevelData, @@ -141,7 +141,7 @@ export const getSupportLevelInfo = ( const SupportLevel = ({ cluster }: SupportLevelProps) => { const { t } = useTranslation(); const featureSupportLevelData = useNewFeatureSupportLevel(); - const { isSupportedOpenShiftVersion } = useOpenshiftVersions(); + const { isSupportedOpenShiftVersion } = useOpenshiftVersionsContext(); const { limitedClusterFeatures, hasSupportedVersion, isFullySupported } = React.useMemo( diff --git a/libs/ui-lib/lib/ocm/hooks/index.ts b/libs/ui-lib/lib/ocm/hooks/index.ts index 131a2bcb8f..51bdb62cbd 100644 --- a/libs/ui-lib/lib/ocm/hooks/index.ts +++ b/libs/ui-lib/lib/ocm/hooks/index.ts @@ -1,4 +1,3 @@ -export { default as useOpenshiftVersions } from './useOpenshiftVersions'; export { default as useManagedDomains } from './useManagedDomains'; export { default as useUsedClusterNames } from './useUsedClusterNames'; export { default as useInfraEnvId } from './useInfraEnvId'; diff --git a/libs/ui-lib/lib/ocm/hooks/useOpenshiftVersions.tsx b/libs/ui-lib/lib/ocm/hooks/useOpenshiftVersions.tsx deleted file mode 100644 index 94e8daf826..0000000000 --- a/libs/ui-lib/lib/ocm/hooks/useOpenshiftVersions.tsx +++ /dev/null @@ -1,101 +0,0 @@ -import React from 'react'; -import { CpuArchitecture, OpenshiftVersionOptionType } from '../../common'; -import { getApiErrorMessage, handleApiError } from '../../common/api'; -import { SupportedOpenshiftVersionsAPI } from '../services/apis'; -import { getKeys } from '../../common/utils'; -import { Cluster, OpenshiftVersion } from '@openshift-assisted/types/assisted-installer-service'; - -type OpenShiftVersion = Cluster['openshiftVersion']; - -type UseOpenshiftVersionsType = { - versions: OpenshiftVersionOptionType[]; - isSupportedOpenShiftVersion: (version: OpenShiftVersion) => boolean; - getCpuArchitectures: (version: OpenShiftVersion) => CpuArchitecture[]; - error?: { title: string; message: string }; - loading: boolean; -}; - -const sortVersions = (versions: OpenshiftVersionOptionType[]) => { - return versions.sort((version1, version2) => - version1.value.localeCompare(version2.value, undefined, { numeric: true }), - ); -}; - -const supportedVersionLevels = ['production', 'maintenance']; - -export default function useOpenshiftVersions(latest_release?: boolean): UseOpenshiftVersionsType { - const [versions, setVersions] = React.useState([]); - const [error, setError] = React.useState(); - - const findVersionItemByVersion = React.useCallback( - (version: OpenShiftVersion) => { - return versions.find(({ value: versionKey }) => { - // For version 4.10 match 4.10, 4.10.3, not 4.1, 4.1.5 - const versionNameMatch = new RegExp(`^${versionKey}(\\..+)?$`); - return versionNameMatch.test(version || ''); - }); - }, - [versions], - ); - - const fetchOpenshiftVersions = React.useCallback(async (latest_release: boolean) => { - try { - const { data } = await SupportedOpenshiftVersionsAPI.list(latest_release); - - const versions: OpenshiftVersionOptionType[] = getKeys(data).map((key) => { - const versionItem = data[key] as OpenshiftVersion; - const version = versionItem.displayName; - return { - label: `OpenShift ${version}`, - value: key as string, - version, - default: Boolean(versionItem.default), - supportLevel: versionItem.supportLevel, - cpuArchitectures: versionItem.cpuArchitectures as CpuArchitecture[], - }; - }); - - setVersions(sortVersions(versions)); - } catch (e) { - handleApiError(e, (e) => { - setError({ - title: 'Failed to retrieve list of supported OpenShift versions.', - message: getApiErrorMessage(e), - }); - }); - } - }, []); - - const isSupportedOpenShiftVersion = React.useCallback( - (version: OpenShiftVersion) => { - if (versions.length === 0) { - // Till the data are loaded - return true; - } - const selectedVersion = findVersionItemByVersion(version); - return supportedVersionLevels.includes(selectedVersion?.supportLevel || ''); - }, - [findVersionItemByVersion, versions.length], - ); - - const getCpuArchitectures = React.useCallback( - (version: OpenShiftVersion) => { - // TODO (multi-arch) confirm this is correctly retrieving the associated version - const matchingVersion = findVersionItemByVersion(version); - return matchingVersion?.cpuArchitectures ?? []; - }, - [findVersionItemByVersion], - ); - - React.useEffect(() => { - void fetchOpenshiftVersions(latest_release ?? false); - }, [latest_release, fetchOpenshiftVersions]); - - return { - error, - loading: !error && versions.length === 0, - versions, - isSupportedOpenShiftVersion, - getCpuArchitectures, - }; -}