Skip to content

Commit

Permalink
refactor: update history management and fix type errors across compon…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
hervedombya committed Nov 27, 2024
1 parent 8915ae1 commit 9d91041
Show file tree
Hide file tree
Showing 41 changed files with 503 additions and 459 deletions.
128 changes: 83 additions & 45 deletions shell-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions shell-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"react-error-boundary": "^4.0.2",
"react-intl": "^5.15.3",
"react-query": "^3.34.0",
"react-router": "5.2.0",
"react-router-dom": "5.2.0",
"react-router": "^6.28.0",
"react-router-dom": "^6.28.0",
"styled-components": "^5.3.11",
"typescript": "^5.6.3"
}
Expand Down
14 changes: 7 additions & 7 deletions shell-ui/src/FederatedApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createBrowserHistory } from 'history';
import React, { useEffect, useMemo } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { QueryClient, QueryClientProvider } from 'react-query';
import { Route, Router, Switch } from 'react-router-dom';
import { BrowserRouter, Route, Router, Routes } from 'react-router-dom';

import { loadShare } from '@module-federation/enhanced/runtime';
import { useQuery } from 'react-query';
Expand Down Expand Up @@ -190,7 +190,7 @@ function InternalRouter() {
[],
);
return (
<Router history={federatedAppHistory} key={app.name}>
<BrowserRouter key={app.name}>
<FederatedRoute
url={
app.url +
Expand All @@ -204,19 +204,19 @@ function InternalRouter() {
app={app}
groups={groups}
/>
</Router>
</BrowserRouter>
);
},
})),
[JSON.stringify(discoveredViews)],
);
return (
<>
<Switch>
<Routes>
{routes.map((route) => (
<Route key={route.path} {...route} />
))}
</Switch>
</Routes>
</>
);
}
Expand All @@ -243,7 +243,7 @@ function InternalApp() {
});

return (
<Router history={history}>
<BrowserRouter>
<ShellHistoryProvider>
<FirstTimeLoginProvider>
<NotificationCenterProvider>
Expand All @@ -259,7 +259,7 @@ function InternalApp() {
</NotificationCenterProvider>
</FirstTimeLoginProvider>
</ShellHistoryProvider>
</Router>
</BrowserRouter>
);
}

Expand Down
1 change: 0 additions & 1 deletion shell-ui/src/alerts/Alerts.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ describe('alerts', () => {
);

//E
// @ts-expect-error - FIXME when you are working on it
const { result, waitForNextUpdate } = renderHook(() => useAlerts(), {
wrapper,
});
Expand Down
2 changes: 0 additions & 2 deletions shell-ui/src/alerts/alertHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export function useAlerts(filters?: FilterLabels) {
throw new Error(
'The useAlerts hook can only be used within AlertProvider.',
);
// @ts-expect-error - FIXME when you are working on it
} else if (query.status === 'success') {
// @ts-expect-error - FIXME when you are working on it
const newQuery = { ...query, alerts: filterAlerts(query.data, filters) };
delete newQuery.data;
return newQuery;
Expand Down
4 changes: 2 additions & 2 deletions shell-ui/src/initFederation/ConfigurationProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export function useDiscoveredViews(): ViewDefinition[] {
return discoveredViews;
}
export const useLinkOpener = () => {
const history = useShellHistory();
const navigate = useShellHistory();
return {
openLink: (
to:
Expand All @@ -325,7 +325,7 @@ export const useLinkOpener = () => {
window.open(to.url, '_blank');
}
} else if (to.isFederated) {
history.push(to.app.appHistoryBasePath + to.view.path);
navigate(to.app.appHistoryBasePath + to.view.path);
} else {
// @ts-expect-error - FIXME when you are working on it
window.location.href = to.url;
Expand Down
6 changes: 3 additions & 3 deletions shell-ui/src/initFederation/ShellHistoryProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContext, useContext } from 'react';
import { useHistory } from 'react-router';
import { useNavigate } from 'react-router';

const ShellHistoryContext = createContext<ReturnType<
typeof useHistory<unknown>
typeof useNavigate
> | null>(null);

export const useShellHistory = () => {
Expand All @@ -17,7 +17,7 @@ export const useShellHistory = () => {
return contextValue;
};
export const ShellHistoryProvider = ({ children }) => {
const history = useHistory();
const history = useNavigate();
return (
<ShellHistoryContext.Provider value={history}>
{children}
Expand Down
11 changes: 4 additions & 7 deletions shell-ui/src/navbar/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useMemo } from 'react';
import { RouteProps, matchPath, useHistory } from 'react-router';
import { RouteProps, matchPath, useNavigate } from 'react-router';
import { useLocation } from 'react-router-dom';
import styled, { createGlobalStyle } from 'styled-components';

Expand Down Expand Up @@ -155,7 +155,7 @@ export const useNavbarLinksToActions = (
const location = useLocation();
const doesRouteMatch = useCallback(
(path: RouteProps) => {
return matchPath(location.pathname, path);
return matchPath(location.pathname, path.path);
},
[location],
);
Expand Down Expand Up @@ -188,9 +188,6 @@ export const useNavbarLinksToActions = (
'i',
).toString()
: link.view.app.appHistoryBasePath + link.view.view.path,
exact: link.view.view.exact,
strict: link.view.view.strict,
sensitive: link.view.view.sensitive,
})
: normalizePath((link.view as NonFederatedView).url) ===
window.location.origin + window.location.pathname,
Expand Down Expand Up @@ -261,7 +258,7 @@ export const Navbar = ({
const { openLink } = useLinkOpener();
const { logOut } = useLogOut();
const { getLinks } = useNavbar();
const history = useHistory();
const navigate = useNavigate();
const navbarLinks = useMemo(() => getLinks(), [getLinks]);
const navbarMainActions = useNavbarLinksToActions(navbarLinks.main);
const navbarSecondaryActions = useNavbarLinksToActions(navbarLinks.secondary);
Expand Down Expand Up @@ -303,7 +300,7 @@ export const Navbar = ({
const url = link.view.isFederated
? link.view.app.appHistoryBasePath + link.view.view.path
: (link.view as NonFederatedView).url;
history.replace(url);
navigate(url, { replace: true });
}
}, [navbarMainActions]);

Expand Down
10 changes: 4 additions & 6 deletions shell-ui/src/navbar/NotificationCenter.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import NotificationCenter from './NotificationCenter';
import userEvent from '@testing-library/user-event';
import { QueryClient, QueryClientProvider } from 'react-query';
import { prepareRenderMultipleHooks } from './__TESTS__/testMultipleHooks';
import { MemoryRouter, Route, Switch } from 'react-router-dom';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { ShellHistoryProvider } from '../initFederation/ShellHistoryProvider';
import { useNotificationCenter } from '../useNotificationCenter';

Expand All @@ -33,14 +33,12 @@ describe('NotificationCenter', () => {
<NotificationCenterProvider>
<NotificationCenter />
<div>{children}</div>
<Switch>
<Route path="/" exact>
Home page
</Route>
<Routes>
<Route path="/">Home page</Route>
<Route path="/alerts">Alert page</Route>
<Route path="/license">License page</Route>
<Route path="/new-version">New version page</Route>
</Switch>
</Routes>
</NotificationCenterProvider>
</ShellHistoryProvider>
</MemoryRouter>
Expand Down
4 changes: 2 additions & 2 deletions shell-ui/src/navbar/NotificationCenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ const NotificationCenter = () => {
if (!selectedItem) {
return;
}
history.push(selectedItem.redirectUrl);
navigate(selectedItem.redirectUrl);
readAllNotifications();
},
});
const isAtLeastOneNotificationUnread = notifications.some(
(notification) => !notification.readOn,
);
const history = useShellHistory();
const navigate = useShellHistory();
return (
<div
style={{ position: 'relative' }}
Expand Down
1 change: 0 additions & 1 deletion shell-ui/src/navbar/navbarHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ export const useNavbar = (): Navbar => {
throw new Error("Can't use useNavbar outside of NavbarConfigProvider");
}

// @ts-expect-error - FIXME when you are working on it
return navbar;
};
Loading

0 comments on commit 9d91041

Please sign in to comment.