Skip to content

Commit

Permalink
MGMT-17147: Update openshift-assisted/ui-lib to react-router-dom v6 (#…
Browse files Browse the repository at this point in the history
…2637)

* Setup the router compatibility package

* Use useNavigate instead of useHistory

* Migrate /ocm and /common to compat Routes

* Migrate /cim to compat routes

* Remove old react-router-dom package

* Make UIRouter children optional
  • Loading branch information
jgyselov authored Aug 2, 2024
1 parent c844422 commit 8546719
Show file tree
Hide file tree
Showing 30 changed files with 153 additions and 161 deletions.
2 changes: 1 addition & 1 deletion apps/assisted-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"react-monaco-editor": "^0.55.0",
"react-redux": "^8.0.5",
"react-router-dom": "^5.3.3",
"react-router-dom-v5-compat": "^6.21.2",
"react-tagsinput": "^3.20",
"redux": "^4",
"uuid": "^8.1",
Expand All @@ -31,7 +32,6 @@
"devDependencies": {
"@tsconfig/vite-react": "^1.0.1",
"@types/react": "17.0.x",
"@types/react-router-dom": "^5.3.3",
"@vitejs/plugin-react-swc": "^3.0.1",
"vite": "^4.5.3",
"vite-plugin-environment": "^1.1.3"
Expand Down
17 changes: 10 additions & 7 deletions apps/assisted-ui/src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import type React from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import { BrowserRouter } from 'react-router-dom';
import { CompatRouter, Route } from 'react-router-dom-v5-compat';
import { Page } from '@patternfly/react-core';
import * as OCM from '@openshift-assisted/ui-lib/ocm';
import { Header } from './Header';
import '../i18n';

const { HostsClusterDetailTabMock, Routes, Features, Config } = OCM;
const { HostsClusterDetailTabMock, UILibRoutes, Features, Config } = OCM;
window.__app__ = { OCM };

export const App: React.FC = () => (
<BrowserRouter basename={Config.routeBasePath}>
<Page header={<Header />} isManagedSidebar defaultManagedSidebarIsOpen={false}>
<Routes allEnabledFeatures={Features.STANDALONE_DEPLOYMENT_ENABLED_FEATURES}>
<Route path={'/day2-flow-mock'} component={HostsClusterDetailTabMock} />
</Routes>
</Page>
<CompatRouter>
<Page header={<Header />} isManagedSidebar defaultManagedSidebarIsOpen={false}>
<UILibRoutes allEnabledFeatures={Features.STANDALONE_DEPLOYMENT_ENABLED_FEATURES}>
<Route path={'/day2-flow-mock'} element={<HostsClusterDetailTabMock />} />
</UILibRoutes>
</Page>
</CompatRouter>
</BrowserRouter>
);
2 changes: 1 addition & 1 deletion libs/ui-lib/lib/cim/components/Agent/NoAgentsAlert.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Alert, AlertVariant, List, ListComponent, ListItem } from '@patternfly/react-core';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';
import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
import { Trans } from 'react-i18next';
import { CpuArchitecture } from '../../../common';
Expand Down
2 changes: 1 addition & 1 deletion libs/ui-lib/lib/cim/components/Agent/tableUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { sortable, expandable, breakWord } from '@patternfly/react-table';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';

import {
HostsTableActions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';
import { Button, Label, Popover, Stack, StackItem } from '@patternfly/react-core';
import { PlusCircleIcon } from '@patternfly/react-icons/dist/js/icons/plus-circle-icon';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AddHostDropdownProps } from './types';
import './AddHostDropdown.css';
import { PopoverIcon } from '../../../common';
import { Trans } from 'react-i18next';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';

type ModalType = 'iso' | 'bmc' | 'yaml' | 'ipxe' | undefined;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import {
Alert,
AlertVariant,
Expand All @@ -26,7 +26,7 @@ export const CimConfigProgressAlert: React.FC<CimConfigProgressAlertProps> = ({
assistedServiceDeploymentUrl,
}) => {
const { t } = useTranslation();
const history = useHistory();
const navigate = useNavigate();
const [hideSuccess, setHideSuccess] = React.useState(true);

React.useEffect(
Expand Down Expand Up @@ -68,7 +68,7 @@ export const CimConfigProgressAlert: React.FC<CimConfigProgressAlertProps> = ({
variant={ButtonVariant.link}
isInline
key="assisted-service-deployment"
onClick={() => history.push(assistedServiceDeploymentUrl)}
onClick={() => navigate(assistedServiceDeploymentUrl)}
>
{t('ai:Troubleshoot in the assisted service deployment')} <ExternalLinkAltIcon />
</Button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { Alert, AlertActionLink, AlertVariant } from '@patternfly/react-core';
import { ExternalLinkAltIcon } from '@patternfly/react-icons/dist/js/icons/external-link-alt-icon';

Expand All @@ -10,7 +10,7 @@ export const CimStorageMissingAlert: React.FC<{
storageOperatorUrl: string;
}> = ({ docStorageUrl, storageOperatorUrl }) => {
const { t } = useTranslation();
const history = useHistory();
const navigate = useNavigate();

const contentWorkaround = (
<>
Expand All @@ -20,10 +20,7 @@ export const CimStorageMissingAlert: React.FC<{
);

const actionLinks: React.ReactNode[] = [
<AlertActionLink
key="install-storage-operator"
onClick={() => history.push(storageOperatorUrl)}
>
<AlertActionLink key="install-storage-operator" onClick={() => navigate(storageOperatorUrl)}>
{t('ai:Install storage operator')}
</AlertActionLink>,
<AlertActionLink key="storage-class" onClick={() => window.open(docStorageUrl, '_blank')}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Button, Flex, FlexItem, Popover } from '@patternfly/react-core';
import { InfoCircleIcon } from '@patternfly/react-icons/dist/js/icons/info-circle-icon';
import { Link } from 'react-router-dom';
import { Link } from 'react-router-dom-v5-compat';
import { sortable } from '@patternfly/react-table';
import { global_palette_blue_300 as blueInfoColor } from '@patternfly/react-tokens/dist/js/global_palette_blue_300';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import {
ButtonVariant,
EmptyState,
Expand All @@ -9,7 +8,6 @@ import {
PageSectionVariants,
EmptyStateHeader,
} from '@patternfly/react-core';
import { HostsClusterDetailTabProps } from './types';
import {
OcmCpuArchitecture,
STANDALONE_DEPLOYMENT_ENABLED_FEATURES,
Expand Down Expand Up @@ -44,9 +42,7 @@ const getCluster = (type: string) => {
}
};

export const HostsClusterDetailTabMock: React.FC<
RouteComponentProps & HostsClusterDetailTabProps
> = () => {
export const HostsClusterDetailTabMock = () => {
const [tabShown, setTabShown] = React.useState<string>('');
const [isMocked, setIsMocked] = React.useState<boolean>(false);

Expand Down
22 changes: 13 additions & 9 deletions libs/ui-lib/lib/ocm/components/Routes.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import React from 'react';
import { Provider } from 'react-redux';
import { Switch, Route, Redirect } from 'react-router-dom';
import { Routes, Route, Navigate, Outlet } from 'react-router-dom-v5-compat';
import { Clusters, ClusterPage, NewClusterPage } from './clusters';
import type { FeatureListType } from '../../common/features/featureGate';
import { routeBasePath } from '../config';
import { AssistedUILibVersion } from './ui';
import { storeDay1 } from '../store';
import { useFeatureDetection } from '../hooks/use-feature-detection';

export const Routes: React.FC<{ allEnabledFeatures: FeatureListType }> = ({
export const UILibRoutes = ({
allEnabledFeatures,
children,
}: {
allEnabledFeatures: FeatureListType;
children?: React.ReactNode;
}) => {
useFeatureDetection(allEnabledFeatures);

return (
<Provider store={storeDay1}>
<AssistedUILibVersion />
<Switch>
<Route path={`${routeBasePath}/clusters/~new`} component={NewClusterPage} />
<Route path={`${routeBasePath}/clusters/:clusterId`} component={ClusterPage} />
<Route path={`${routeBasePath}/clusters`} component={Clusters} />
<Routes>
<Route path="clusters" element={<Outlet />}>
<Route path="~new" element={<NewClusterPage />} />
<Route path=":clusterId" element={<ClusterPage />} />
<Route index element={<Clusters />} />
</Route>
{children}
<Redirect to={`${routeBasePath}/clusters`} />
</Switch>
<Route path="*" element={<Navigate to="clusters" />} />
</Routes>
</Provider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { Provider } from 'react-redux';
import { Card, CardBody, CardHeader, Title } from '@patternfly/react-core';
import { storeDay1 } from '../../store';
import { OCM_CLUSTER_LIST_LINK } from '../../config';
import {
AlertsContextProvider,
AssistedInstallerOCMPermissionTypesListType,
Expand All @@ -29,8 +28,8 @@ import { NewFeatureSupportLevelProvider } from '../featureSupportLevels';
import { usePullSecret } from '../../hooks';
import { Cluster } from '@openshift-assisted/types/assisted-installer-service';
import { useFeatureDetection } from '../../hooks/use-feature-detection';
import { BrowserRouter } from 'react-router-dom';
import { OpenshiftVersionsContextProvider } from '../clusterWizard/OpenshiftVersionsContext';
import { BrowserRouter } from 'react-router-dom-v5-compat';

type AssistedInstallerDetailCardProps = {
aiClusterId: string;
Expand Down Expand Up @@ -65,7 +64,7 @@ const ClusterLoadFailed = ({ clusterId, error }: { clusterId: Cluster['id']; err
<ErrorState
title="Failed to fetch the cluster"
fetchData={fetchCluster}
actions={[<BackButton key={'cancel'} to={OCM_CLUSTER_LIST_LINK} />]}
actions={[<BackButton key={'cancel'} to={'..'} />]}
content={error}
/>
</BrowserRouter>
Expand All @@ -85,7 +84,7 @@ const LoadingDefaultConfigFailedCard = () => (
<BrowserRouter>
<ErrorState
title="Failed to retrieve the default configuration"
actions={[<BackButton key={'cancel'} to={OCM_CLUSTER_LIST_LINK} />]}
actions={[<BackButton key={'cancel'} to={'..'} />]}
/>
</BrowserRouter>
</CardBody>
Expand Down
10 changes: 5 additions & 5 deletions libs/ui-lib/lib/ocm/components/clusterDetail/ClusterDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import {
Stack,
StackItem,
Expand Down Expand Up @@ -40,7 +40,7 @@ const ClusterDetail: React.FC<ClusterDetailProps> = ({ cluster }) => {
const isSNOExpansionAllowed =
featureSupportLevelContext.isFeatureSupported('SINGLE_NODE_EXPANSION');
const isSingleClusterFeatureEnabled = useFeature('ASSISTED_INSTALLER_SINGLE_CLUSTER_FEATURE');
const history = useHistory();
const navigate = useNavigate();
const canAddHosts =
(!isSNO(cluster) || isSNOExpansionAllowed) && cluster.status === 'installed' && !isInOcm;

Expand All @@ -49,13 +49,13 @@ const ClusterDetail: React.FC<ClusterDetailProps> = ({ cluster }) => {
try {
const { data } = await ClustersAPI.allowAddHosts(cluster.id);
updateCluster(data);
history.push(`${routeBasePath}/clusters/${cluster.id}`);
navigate(`${routeBasePath}/clusters/${cluster.id}`);
} catch (e) {
handleApiError(e);
}
};
void doItAsync();
}, [cluster.id, history]);
}, [cluster.id, navigate]);

return (
<Stack hasGutter>
Expand Down Expand Up @@ -112,7 +112,7 @@ const ClusterDetail: React.FC<ClusterDetailProps> = ({ cluster }) => {
{!isSingleClusterFeatureEnabled && (
<ToolbarButton
variant={ButtonVariant.link}
onClick={() => history.push(`${routeBasePath}/clusters`)}
onClick={() => navigate('..')}
id={getClusterDetailId('button-back-to-all-clusters')}
>
Back to all clusters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useDispatch } from 'react-redux';
import { useAlerts, LoadingState, ClusterWizardStep, ErrorState } from '../../../common';
import { usePullSecret } from '../../hooks';
Expand All @@ -11,7 +11,6 @@ import { useManagedDomains, useUsedClusterNames } from '../../hooks';
import { useOpenshiftVersionsContext } from './OpenshiftVersionsContext';
import ClusterDetailsForm from './ClusterDetailsForm';
import ClusterWizardNavigation from './ClusterWizardNavigation';
import { routeBasePath } from '../../config';
import {
ClusterDetailsUpdateParams,
ClustersService,
Expand All @@ -29,7 +28,7 @@ const ClusterDetails = ({ cluster, infraEnv }: ClusterDetailsProps) => {
const clusterWizardContext = useClusterWizardContext();
const managedDomains = useManagedDomains();
const { addAlert, clearAlerts } = useAlerts();
const history = useHistory();
const navigate = useNavigate();
const dispatch = useDispatch();
const { usedClusterNames } = useUsedClusterNames(cluster?.id || '');
const pullSecret = usePullSecret();
Expand Down Expand Up @@ -74,7 +73,7 @@ const ClusterDetails = ({ cluster, infraEnv }: ClusterDetailsProps) => {
clearAlerts();
try {
const cluster = await ClustersService.create(params);
history.push(`${routeBasePath}/clusters/${cluster.id}`, ClusterWizardFlowStateNew);
navigate(`../${cluster.id}`, { state: ClusterWizardFlowStateNew });
await UISettingService.update(cluster.id, { addCustomManifests });
} catch (e) {
handleApiError(e, () =>
Expand All @@ -85,7 +84,7 @@ const ClusterDetails = ({ cluster, infraEnv }: ClusterDetailsProps) => {
}
}
},
[clearAlerts, history, addAlert, dispatch],
[clearAlerts, navigate, addAlert, dispatch],
);

const navigation = <ClusterWizardNavigation cluster={cluster} />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { useLocation } from 'react-router-dom-v5-compat';
import { Grid, GridItem } from '@patternfly/react-core';
import isUndefined from 'lodash-es/isUndefined.js';
import { Formik, FormikHelpers } from 'formik';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { PropsWithChildren } from 'react';
import { useLocation } from 'react-router-dom';
import { useLocation } from 'react-router-dom-v5-compat';
import { ClusterWizardContextType, ClusterWizardContext } from './ClusterWizardContext';
import {
ClusterWizardFlowStateType,
Expand Down Expand Up @@ -91,7 +91,8 @@ const ClusterWizardContextProvider = ({
const [wizardStepIds, setWizardStepIds] = React.useState<ClusterWizardStepsType[]>();
const [wizardPerPage, setWizardPerPage] = React.useState(10);
const [customManifestsStep, setCustomManifestsStep] = React.useState<boolean>(false);
const { state: locationState } = useLocation<ClusterWizardFlowStateType>();
const location = useLocation();
const locationState = location.state as ClusterWizardFlowStateType | undefined;
const {
uiSettings,
updateUISettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { Alert, AlertGroup, AlertVariant } from '@patternfly/react-core';
import {
WizardFooter,
Expand All @@ -9,7 +9,6 @@ import {
clusterFieldLabels,
selectClusterValidationsInfo,
} from '../../../common';
import { routeBasePath } from '../../config';
import { wizardStepsValidationsMap } from './wizardTransition';
import { useClusterWizardContext } from './ClusterWizardContext';
import ClusterWizardStepValidationsAlert from '../../../common/components/clusterWizard/ClusterWizardStepValidationsAlert';
Expand Down Expand Up @@ -80,12 +79,9 @@ const ClusterWizardFooter = ({
...rest
}: ClusterWizardFooterProps) => {
const { alerts } = useAlerts();
const history = useHistory();
const navigate = useNavigate();

const handleCancel = React.useCallback(
() => history.push(`${routeBasePath}/clusters/`),
[history],
);
const handleCancel = React.useCallback(() => navigate('..'), [navigate]);

const alertsSection = alerts.length ? <Alerts /> : undefined;

Expand Down
Loading

0 comments on commit 8546719

Please sign in to comment.