Fix "main route" detection

There was a possibility that routes were incorrectly detected as main routes which caused the back button to not get shown.

E.g. a route that is only visible on desktop was still considered a main route on mobile devices
This commit is contained in:
schroda
2025-03-09 15:51:51 +01:00
parent df719df666
commit 95ac4832bd

View File

@@ -82,11 +82,7 @@ export function DefaultNavBar() {
const getOptionForDirection = useGetOptionForDirection();
const { pathname } = useLocation();
const handleBack = useBackButton();
const isMobileWidth = MediaQuery.useIsMobileWidth();
const isMainRoute = navbarItems.some(({ path }) => path === pathname);
const actualNavBarWidth = isMobileWidth || isCollapsed ? 0 : navBarWidth;
const appBarRef = useRef<HTMLDivElement | null>(null);
useResizeObserver(
@@ -103,6 +99,19 @@ export function DefaultNavBar() {
return () => setAppBarHeight(0);
}, [override.status]);
const isMainRoute = navbarItems.some(({ path, show }) => {
if (isMobileWidth && show === 'desktop') {
return false;
}
if (!isMobileWidth && show === 'mobile') {
return false;
}
return path === pathname;
});
const actualNavBarWidth = isMobileWidth || isCollapsed ? 0 : navBarWidth;
const activeNavBar: NavbarItem['show'] = isMobileWidth ? 'mobile' : 'desktop';
const visibleNavBarItems = useMemo(
() => navbarItems.filter(({ show }) => ['both', activeNavBar].includes(show)),
@@ -110,6 +119,11 @@ export function DefaultNavBar() {
);
const NavBarComponent = useMemo(() => (isMobileWidth ? MobileBottomBar : DesktopSideBar), [isMobileWidth]);
const navBar = useMemo(
() => <NavBarComponent navBarItems={visibleNavBarItems} />,
[NavBarComponent, visibleNavBarItems],
);
useLayoutEffect(() => {
if (!isMobileWidth) {
// do not reset navbar width to prevent grid from jumping due to the changing grid item width
@@ -119,11 +133,6 @@ export function DefaultNavBar() {
setNavBarWidth(0);
}, [isMobileWidth]);
const navBar = useMemo(
() => <NavBarComponent navBarItems={visibleNavBarItems} />,
[NavBarComponent, visibleNavBarItems],
);
// Allow default navbar to be overrided
if (override.status) return override.value;