1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/HistoryRouter.tsx

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-04-14 01:19:27 -04:00
import React, { useLayoutEffect } from 'react';
import { HistoryRouterProps, Router } from 'react-router-dom';
import { Update } from 'history';
const normalizePath = (pathname: string) => pathname.replace(/^!/, '');
2022-04-14 01:19:27 -04:00
export function HistoryRouter({ basename, children, history }: HistoryRouterProps) {
const [state, setState] = React.useState({
action: history.action,
location: history.location
});
useLayoutEffect(() => {
const onHistoryChange = (update: Update) => {
if (update.location.pathname.startsWith('!')) {
history.replace(normalizePath(update.location.pathname), update.location.state);
} else {
setState(update);
}
};
history.listen(onHistoryChange);
}, [ history ]);
2022-04-14 01:19:27 -04:00
return (
<Router
basename={basename}
// eslint-disable-next-line react/no-children-prop
children={children}
location={{
...state.location,
pathname: normalizePath(state.location.pathname)
}}
2022-04-14 01:19:27 -04:00
navigationType={state.action}
navigator={history}
/>
);
}