diff --git a/src/app/components/page/MobileNavDrawer.tsx b/src/app/components/page/MobileNavDrawer.tsx index 0fa0e4877..ce296ae4b 100644 --- a/src/app/components/page/MobileNavDrawer.tsx +++ b/src/app/components/page/MobileNavDrawer.tsx @@ -1,5 +1,6 @@ import type { ReactNode } from 'react'; -import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; +import { startTransition, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; +import type { AnimationPlaybackControls } from 'framer-motion'; import { animate, motion, useDragControls, useMotionValue, useReducedMotion } from 'framer-motion'; import { useAtomValue, useSetAtom } from 'jotai'; import { matchPath, useLocation, useNavigate } from 'react-router-dom'; @@ -72,6 +73,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra const [width, setWidth] = useState(0); const x = useMotionValue(0); const dragControls = useDragControls(); + const settleAnimRef = useRef(null); const initialIntent = contentOpen ? 1 : 0; const [panelIntent, setPanelIntent] = useState(initialIntent); @@ -92,11 +94,14 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra const settle = useCallback( (target: number) => { + settleAnimRef.current?.stop(); + settleAnimRef.current = null; + if (reduceMotion) { x.jump(target); return; } - animate(x, target, { + settleAnimRef.current = animate(x, target, { type: 'spring', stiffness: SETTLE_STIFFNESS, damping: SETTLE_DAMPING, @@ -130,12 +135,16 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra if (width > 0 && !reduceMotion) { settle(target); } else { + settleAnimRef.current?.stop(); + settleAnimRef.current = null; x.jump(target); } } }, [contentOpen, width, x, settle, reduceMotion]); useLayoutEffect(() => { + settleAnimRef.current?.stop(); + settleAnimRef.current = null; const target = panelIntentRef.current === 1 ? -width : 0; x.jump(target); }, [width, x]); @@ -171,6 +180,8 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra dragMomentum={false} onDragStart={() => { x.stop(); + settleAnimRef.current?.stop(); + settleAnimRef.current = null; if (panelIntentRef.current === 0 && !roomArmed) setRoomArmed(true); }} onDragEnd={(_event, info) => { @@ -187,7 +198,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra if (section?.getRoomPath && matchedRoomId && isRoomRoute) { setLastRoom((prev) => ({ ...prev, [section.key]: matchedRoomId })); } - if (section) navigate(section.listPath); + if (section) startTransition(() => navigate(section.listPath)); } else { settle(-width); } @@ -205,7 +216,7 @@ export function MobileNavDrawer({ nav, rail, bottomNav, children }: MobileNavDra panelIntentRef.current = 1; setPanelIntent(1); settle(-width); - navigate(roomPath); + startTransition(() => navigate(roomPath)); } else { settle(0); } diff --git a/src/app/features/room-nav/RoomNavItem.tsx b/src/app/features/room-nav/RoomNavItem.tsx index 923354775..8fd1e1f5a 100644 --- a/src/app/features/room-nav/RoomNavItem.tsx +++ b/src/app/features/room-nav/RoomNavItem.tsx @@ -413,12 +413,8 @@ export function RoomNavItem({ navigateRoom(room.roomId); } } else { - if (isMobile) { - navigate(linkPath); - } else { - // Keep heavy room mounts off the urgent path on desktop. - startTransition(() => navigate(linkPath)); - } + // Render the room off the urgent path so the tap doesn't freeze the UI on mount. + startTransition(() => navigate(linkPath)); } }; @@ -441,7 +437,7 @@ export function RoomNavItem({ if (dx > 10 || dy > 10) return; // was a drag, not a tap if (room.isCallRoom()) return; // call rooms use onClick evt.preventDefault(); - navigate(linkPath); + startTransition(() => navigate(linkPath)); }; const handleChatButtonClick = (evt: MouseEvent) => { diff --git a/src/app/hooks/router/useSelectedRoom.ts b/src/app/hooks/router/useSelectedRoom.ts index b99dafa78..88d9c2000 100644 --- a/src/app/hooks/router/useSelectedRoom.ts +++ b/src/app/hooks/router/useSelectedRoom.ts @@ -1,9 +1,10 @@ -import { useAtomValue } from 'jotai'; +import { useMemo } from 'react'; +import { atom, useAtomValue } from 'jotai'; import { matchPath, useLocation, useParams } from 'react-router-dom'; import { getCanonicalAliasRoomId, isRoomAlias } from '$utils/matrix'; import { useMatrixClient } from '$hooks/useMatrixClient'; import { resolveSection } from '$pages/pathUtils'; -import { lastVisitedRoomAtom } from '$state/room/lastRoom'; +import { lastVisitedRoomSectionAtom } from '$state/room/lastRoom'; export const useSelectedRoom = (): string | undefined => { const mx = useMatrixClient(); @@ -18,14 +19,24 @@ export const useSelectedRoom = (): string | undefined => { return roomId; }; +const emptyLastRoomAtom = atom(undefined); + export const useSelectedOrLastRoom = (): string | undefined => { const selectedRoomId = useSelectedRoom(); const location = useLocation(); - const lastVisitedRoom = useAtomValue(lastVisitedRoomAtom); - - if (selectedRoomId) return selectedRoomId; const section = resolveSection(location.pathname); const listMatch = section && matchPath({ path: section.listPath, end: true }, location.pathname); - return listMatch ? lastVisitedRoom[section.key] : undefined; + + const sectionKey = section?.key; + const sectionAtom = useMemo( + () => (sectionKey ? lastVisitedRoomSectionAtom(sectionKey) : emptyLastRoomAtom), + [sectionKey] + ); + const lastRoomId = useAtomValue(sectionAtom); + + if (selectedRoomId) return selectedRoomId; + + return listMatch ? lastRoomId : undefined; }; + diff --git a/src/app/state/room/lastRoom.ts b/src/app/state/room/lastRoom.ts index 8690b8008..b5ac42ef8 100644 --- a/src/app/state/room/lastRoom.ts +++ b/src/app/state/room/lastRoom.ts @@ -1,3 +1,4 @@ +import { atom } from 'jotai'; import { atomWithLocalStorage, getLocalStorageItem, @@ -13,3 +14,8 @@ const lastVisitedRoomBaseAtom = atomWithLocalStorage>( ); export const lastVisitedRoomAtom = lastVisitedRoomBaseAtom; + +/** Returns a read-only derived atom for a single section's last-visited room. */ +export const lastVisitedRoomSectionAtom = (sectionKey: string) => + atom((get) => get(lastVisitedRoomAtom)[sectionKey]); +