diff --git a/src/app/features/settings/SettingsRoute.test.tsx b/src/app/features/settings/SettingsRoute.test.tsx index 55c04d37..2dba6df0 100644 --- a/src/app/features/settings/SettingsRoute.test.tsx +++ b/src/app/features/settings/SettingsRoute.test.tsx @@ -1,11 +1,22 @@ import { act, render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { MemoryRouter, Route, Routes, useLocation, useNavigationType } from 'react-router-dom'; +import type { ReactNode } from 'react'; +import { + MemoryRouter, + Route, + Routes, + useLocation, + useNavigate, + useNavigationType, +} from 'react-router-dom'; import { describe, expect, it, vi } from 'vitest'; import { SettingTile } from '$components/setting-tile'; +import { ClientRouteOutlet } from '$pages/client/ClientRouteOutlet'; import { ScreenSize, ScreenSizeProvider } from '$hooks/useScreenSize'; import { getHomePath, getSettingsPath } from '$pages/pathUtils'; +import { SETTINGS_PATH } from '$pages/paths'; import { SettingsRoute } from './SettingsRoute'; +import { SettingsShallowRouteRenderer } from './SettingsShallowRouteRenderer'; import { SettingsSectionPage } from './SettingsSectionPage'; import { focusedSettingTile } from './styles.css'; import { useSettingsFocus } from './useSettingsFocus'; @@ -49,6 +60,10 @@ vi.mock('$state/hooks/settings', () => ({ useSetting: mockUseSetting, })); +vi.mock('$components/Modal500', () => ({ + Modal500: ({ children }: { children: ReactNode }) =>
{children}
, +})); + vi.mock('./general', () => ({ General: createSectionMock('General section'), })); @@ -115,6 +130,48 @@ function LocationProbe() { ); } +function HomePage() { + const navigate = useNavigate(); + const location = useLocation(); + + return ( +
+

Home route

+ +
+ ); +} + +function renderClientShell( + screenSize: ScreenSize, + options?: { initialEntries?: string[]; initialIndex?: number } +) { + const initialEntries = options?.initialEntries ?? [getHomePath()]; + return render( + + + + + }> + } /> + } /> + + + + + + ); +} + function renderSettingsRoute( path: string, screenSize: ScreenSize, @@ -126,7 +183,7 @@ function renderSettingsRoute( - } /> + } /> @@ -258,6 +315,39 @@ describe('SettingsRoute', () => { }); }); +describe('Settings shallow route shell', () => { + it('keeps the desktop background route mounted when settings opens shallow', async () => { + const user = userEvent.setup(); + + renderClientShell(ScreenSize.Desktop); + + await user.click(screen.getByRole('button', { name: 'Open settings' })); + + expect(screen.getByRole('heading', { name: 'Home route' })).toBeInTheDocument(); + expect(screen.getByRole('heading', { name: 'Notifications section' })).toBeInTheDocument(); + }); + + it('renders mobile settings as a full page without retaining the background outlet', async () => { + const user = userEvent.setup(); + + renderClientShell(ScreenSize.Mobile); + + await user.click(screen.getByRole('button', { name: 'Open settings' })); + + expect(screen.queryByRole('heading', { name: 'Home route' })).not.toBeInTheDocument(); + expect(screen.getByRole('heading', { name: 'Notifications section' })).toBeInTheDocument(); + }); + + it('renders desktop direct entry settings as a full page without retaining the background outlet', () => { + renderClientShell(ScreenSize.Desktop, { + initialEntries: [getSettingsPath('devices')], + }); + + expect(screen.queryByRole('heading', { name: 'Home route' })).not.toBeInTheDocument(); + expect(screen.getByRole('heading', { name: 'Devices section' })).toBeInTheDocument(); + }); +}); + describe('useSettingsFocus', () => { it('highlights a focus target from the query string', async () => { vi.useFakeTimers(); diff --git a/src/app/features/settings/SettingsShallowRouteRenderer.tsx b/src/app/features/settings/SettingsShallowRouteRenderer.tsx new file mode 100644 index 00000000..92256fa8 --- /dev/null +++ b/src/app/features/settings/SettingsShallowRouteRenderer.tsx @@ -0,0 +1,22 @@ +import { Route, Routes, useNavigate, useLocation } from 'react-router-dom'; +import { useScreenSizeContext } from '$hooks/useScreenSize'; +import { Modal500 } from '$components/Modal500'; +import { isShallowSettingsRoute } from '$pages/client/ClientRouteOutlet'; +import { SETTINGS_PATH } from '$pages/paths'; +import { SettingsRoute } from './SettingsRoute'; + +export function SettingsShallowRouteRenderer() { + const navigate = useNavigate(); + const location = useLocation(); + const screenSize = useScreenSizeContext(); + + if (!isShallowSettingsRoute(location.pathname, location.state, screenSize)) return null; + + return ( + navigate(-1)}> + + } /> + + + ); +} diff --git a/src/app/pages/Router.tsx b/src/app/pages/Router.tsx index d81890da..28f8c7ef 100644 --- a/src/app/pages/Router.tsx +++ b/src/app/pages/Router.tsx @@ -10,6 +10,8 @@ import * as Sentry from '@sentry/react'; import { ClientConfig } from '$hooks/useClientConfig'; import { ErrorPage } from '$components/DefaultErrorPage'; +import { SettingsRoute } from '$features/settings'; +import { SettingsShallowRouteRenderer } from '$features/settings/SettingsShallowRouteRenderer'; import { Room } from '$features/room'; import { Lobby } from '$features/lobby'; import { PageRoot } from '$components/page'; @@ -49,6 +51,7 @@ import { SERVER_PATH_SEGMENT, CREATE_PATH, TO_ROOM_EVENT_PATH, + SETTINGS_PATH, } from './paths'; import { getAppPathFromHref, @@ -59,7 +62,7 @@ import { getOriginBaseUrl, getSpaceLobbyPath, } from './pathUtils'; -import { ClientBindAtoms, ClientLayout, ClientRoot } from './client'; +import { ClientBindAtoms, ClientLayout, ClientRoot, ClientRouteOutlet } from './client'; import { HandleNotificationClick, ClientNonUIFeatures } from './client/ClientNonUIFeatures'; import { Home, HomeRouteRoomProvider, HomeSearch } from './client/home'; import { Direct, DirectCreate, DirectRouteRoomProvider } from './client/direct'; @@ -182,7 +185,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) } > - + @@ -191,6 +194,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) + @@ -340,6 +344,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) } /> } /> + } /> { + if (screenSize === ScreenSize.Mobile) return false; + if (!matchPath(SETTINGS_PATH, pathname)) return false; + + const backgroundLocation = (state as BackgroundLocationState | null)?.backgroundLocation; + return !!backgroundLocation; +}; + +export function ClientRouteOutlet() { + const outlet = useOutlet(); + const location = useLocation(); + const screenSize = useScreenSizeContext(); + const cachedOutletRef = useRef(outlet); + const shallowSettings = isShallowSettingsRoute(location.pathname, location.state, screenSize); + + if (!shallowSettings) { + cachedOutletRef.current = outlet; + return outlet; + } + + return cachedOutletRef.current; +} diff --git a/src/app/pages/client/index.ts b/src/app/pages/client/index.ts index 5668e81d..7c654fb3 100644 --- a/src/app/pages/client/index.ts +++ b/src/app/pages/client/index.ts @@ -1,3 +1,4 @@ export * from './ClientRoot'; export * from './ClientBindAtoms'; export * from './ClientLayout'; +export * from './ClientRouteOutlet';