mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
feat(collections): the map's padlock, and a picked place on the full map keeps the map
The collections map gets the trip map's padlock (#2010): locked, a click on a place leaves the camera where it is, so places can be clicked through without the map zooming in and out. One setting for both maps. On the full map a picked place no longer brings the list back: its details dock on the map's edge, or open as a sheet on a narrow screen.
This commit is contained in:
@@ -15,6 +15,9 @@ interface CollectionMapProps {
|
||||
dark: boolean
|
||||
/** A control bar floats over the top of the map (the desktop panel's), so the frame starts below it. */
|
||||
underBar?: boolean
|
||||
/** Given, the padlock sits on the map: locked, picking a place leaves the camera where it is (#2010). */
|
||||
locked?: boolean
|
||||
onToggleLock?: () => void
|
||||
}
|
||||
|
||||
/** How far a pin reaches from its point: half a selected pin (44 px) and the rating badge on its corner. */
|
||||
@@ -69,7 +72,7 @@ function framePoints(places: CollectionPlace[]): [number, number][] {
|
||||
* box instead, clear of the edges and of the controls floating over it, and lands
|
||||
* while the map is built rather than flying in.
|
||||
*/
|
||||
export default function CollectionMap({ places, selectedPlaceId, onOpenPlace, onDeselect, dark, underBar = false }: CollectionMapProps): React.ReactElement {
|
||||
export default function CollectionMap({ places, selectedPlaceId, onOpenPlace, onDeselect, dark, underBar = false, locked = false, onToggleLock }: CollectionMapProps): React.ReactElement {
|
||||
const pts = useMemo(() => mappablePlaces(places).map(withMarkerCategory), [places])
|
||||
const tileUrl = useTileUrl(dark ? OFM_DARK : OFM_POSITRON)
|
||||
const focusPoints = useMemo(() => framePoints(pts), [pts])
|
||||
@@ -95,6 +98,8 @@ export default function CollectionMap({ places, selectedPlaceId, onOpenPlace, on
|
||||
focusPoints={focusPoints}
|
||||
focusKey={focusKey}
|
||||
fitPadding={framePadding(underBar)}
|
||||
followSelection={!locked}
|
||||
onToggleFollow={onToggleLock}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -28,6 +28,9 @@ interface CollectionMapPanelProps {
|
||||
onLabelFilter?: (ids: number[]) => void
|
||||
canManageLabels?: boolean
|
||||
onManageLabels?: () => void
|
||||
/** The padlock on the desktop map (#2010); the phone has no hover to look around with. */
|
||||
mapLocked?: boolean
|
||||
onToggleMapLocked?: () => void
|
||||
t: TranslationFn
|
||||
}
|
||||
|
||||
@@ -39,7 +42,7 @@ interface CollectionMapPanelProps {
|
||||
export default function CollectionMapPanel({
|
||||
places, selectedPlaceId, onSelect, onDeselect, dark, overlay, view, onToggleView,
|
||||
search, onSearch, labelOptions = [], labelFilter = [], onLabelFilter, canManageLabels = false,
|
||||
onManageLabels, t,
|
||||
onManageLabels, mapLocked = false, onToggleMapLocked, t,
|
||||
}: CollectionMapPanelProps): React.ReactElement {
|
||||
const showLabels = onLabelFilter != null && (labelOptions.length > 0 || canManageLabels)
|
||||
return (
|
||||
@@ -51,6 +54,8 @@ export default function CollectionMapPanel({
|
||||
onDeselect={onDeselect}
|
||||
dark={dark}
|
||||
underBar={overlay}
|
||||
locked={overlay && mapLocked}
|
||||
onToggleLock={overlay ? onToggleMapLocked : undefined}
|
||||
/>
|
||||
{overlay && (
|
||||
<div className="col-map-topbar">
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
|
||||
const MAP_LOCK_KEY = 'trek:map-locked';
|
||||
|
||||
/**
|
||||
* Whether a map keeps its camera when a place or a day is picked (#2010). One
|
||||
* setting per browser for every map that offers the padlock, the trip planner
|
||||
* and the collections alike: whoever locked one expects the other to hold still too.
|
||||
*/
|
||||
export function useMapLock(): [boolean, () => void] {
|
||||
const [locked, setLocked] = useState<boolean>(() => {
|
||||
try {
|
||||
return localStorage.getItem(MAP_LOCK_KEY) === '1';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
const toggle = useCallback(() => {
|
||||
setLocked((prev) => {
|
||||
const next = !prev;
|
||||
try {
|
||||
localStorage.setItem(MAP_LOCK_KEY, next ? '1' : '0');
|
||||
} catch {
|
||||
/* private mode keeps it for the session */
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
return [locked, toggle];
|
||||
}
|
||||
@@ -59,6 +59,10 @@ function makeHook(overrides: Record<string, unknown> = {}) {
|
||||
heroHeight: 0,
|
||||
listColRef: { current: null },
|
||||
listColRect: null,
|
||||
mapColRef: { current: null },
|
||||
mapColRect: null,
|
||||
mapLocked: false,
|
||||
toggleMapLocked: vi.fn(),
|
||||
categories: [],
|
||||
collections: [{ id: 1, name: 'Test', is_owner: true }],
|
||||
ownedLists: [{ id: 1, name: 'Test', is_owner: true }],
|
||||
|
||||
@@ -73,11 +73,16 @@ function CollectionsPageDesktop(): React.ReactElement {
|
||||
if (c.view !== 'map') c.setSelectedPlaceId(null)
|
||||
c.setView(c.view === 'map' ? 'list' : 'map')
|
||||
}
|
||||
// Clicking a marker in the full-map view drops back to the split so the list
|
||||
// + detail come into view alongside the map.
|
||||
const onMapSelect = (id: number) => { showPlace(id); if (c.view === 'map') c.setView('list') }
|
||||
// A marker shows its place and the map stays as it is: on the full map the
|
||||
// detail docks on the map's edge instead of bringing the list back.
|
||||
const onMapSelect = (id: number) => showPlace(id)
|
||||
|
||||
const desktopSplit = c.isWide && c.hasMappable
|
||||
// Where the place detail docks: over the list in the split, on the map's start
|
||||
// edge when the map has the whole width, and as a centred sheet otherwise.
|
||||
const detailDock = !desktopSplit ? null
|
||||
: c.view === 'map' ? (c.mapColRect ? { left: c.mapColRect.left + 12, width: Math.min(420, c.mapColRect.width - 24) } : null) // rtl-lint-disable: a measured viewport position, like the list dock
|
||||
: c.listColRect
|
||||
const mapShown = c.hasMappable && (c.view === 'map' || c.isWide)
|
||||
const mapOverlay = c.isWide && mapShown // the map carries the toggle + search
|
||||
const canAddPlace = typeof c.activeId === 'number' && c.canEdit // a real list you can edit
|
||||
@@ -112,6 +117,8 @@ function CollectionsPageDesktop(): React.ReactElement {
|
||||
onToggleView={toggleView}
|
||||
search={c.search}
|
||||
onSearch={c.setSearch}
|
||||
mapLocked={c.mapLocked}
|
||||
onToggleMapLocked={c.toggleMapLocked}
|
||||
t={t}
|
||||
/>
|
||||
)
|
||||
@@ -184,7 +191,7 @@ function CollectionsPageDesktop(): React.ReactElement {
|
||||
body = (
|
||||
<div className={`col-split${c.view === 'map' ? ' map-full' : ''}`}>
|
||||
<div className="col-split-list" ref={c.listColRef}>{listColumn}</div>
|
||||
<div className="col-split-map">{mapPanel(true)}</div>
|
||||
<div className="col-split-map" ref={c.mapColRef}>{mapPanel(true)}</div>
|
||||
</div>
|
||||
)
|
||||
} else if (c.view === 'map' && c.hasMappable) {
|
||||
@@ -343,14 +350,14 @@ function CollectionsPageDesktop(): React.ReactElement {
|
||||
visible + interactive). On the desktop split it docks over the list
|
||||
column so the map stays clear; otherwise it's a full-width sheet.
|
||||
Clicking another place / the map background re-points the selection. */}
|
||||
{c.selectedPlace && c.view !== 'map' && (
|
||||
{c.selectedPlace && (c.view !== 'map' || c.hasMappable) && (
|
||||
<CollectionPlaceDetail
|
||||
place={c.selectedPlace}
|
||||
canEdit={c.canEdit}
|
||||
canDelete={c.canDelete}
|
||||
categories={c.categories}
|
||||
labels={c.labels}
|
||||
anchorRect={desktopSplit ? c.listColRect : null}
|
||||
anchorRect={detailDock}
|
||||
onClose={c.handleCloseDetail}
|
||||
onSetStatus={c.handleDetailStatus}
|
||||
onSave={patch => c.updatePlace(c.selectedPlace!.id, patch)}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// to the hook. The page follows the Page pattern, so useCollections is mocked and
|
||||
// each child is stubbed with a prop-driven button.
|
||||
import React from 'react'
|
||||
import { render, screen, fireEvent } from '../../tests/helpers/render'
|
||||
import { render, screen, fireEvent, cleanup } from '../../tests/helpers/render'
|
||||
import CollectionsPage from './CollectionsPage'
|
||||
|
||||
vi.mock('../components/Layout/Navbar', () => ({ default: () => <nav data-testid="navbar" /> }))
|
||||
@@ -59,8 +59,10 @@ vi.mock('../components/Collections/CollectionFilterBar', () => ({
|
||||
}))
|
||||
|
||||
vi.mock('../components/Collections/CollectionMapPanel', () => ({
|
||||
default: (p: { overlay: boolean; places: { id: number }[]; onSelect: (id: number) => void; onDeselect: () => void; onToggleView: () => void; onSearch: (v: string) => void; onLabelFilter?: (ids: number[]) => void; onManageLabels?: () => void }) => (
|
||||
default: (p: { overlay: boolean; places: { id: number }[]; onSelect: (id: number) => void; onDeselect: () => void; onToggleView: () => void; onSearch: (v: string) => void; onLabelFilter?: (ids: number[]) => void; onManageLabels?: () => void; mapLocked?: boolean; onToggleMapLocked?: () => void }) => (
|
||||
<div data-testid={p.overlay ? 'map-overlay' : 'map-plain'}>
|
||||
<span data-testid="map-locked">{String(p.mapLocked)}</span>
|
||||
{p.onToggleMapLocked && <button type="button" onClick={p.onToggleMapLocked}>map-lock</button>}
|
||||
<span data-testid="map-count">{p.places.length}</span>
|
||||
<span data-testid="map-has-labels">{String(p.onLabelFilter != null)}</span>
|
||||
{p.onManageLabels && <button type="button" onClick={p.onManageLabels}>map-labels</button>}
|
||||
@@ -192,6 +194,10 @@ function makeHook(overrides: Hook = {}): Hook {
|
||||
heroHeight: 240,
|
||||
listColRef: () => {},
|
||||
listColRect: { left: 10, width: 400 },
|
||||
mapColRef: () => {},
|
||||
mapColRect: { left: 500, width: 900 },
|
||||
mapLocked: false,
|
||||
toggleMapLocked: vi.fn(),
|
||||
categories: [],
|
||||
collections: [list],
|
||||
ownedLists: [list, { id: 4, name: 'Other', is_owner: true }],
|
||||
@@ -556,11 +562,18 @@ describe('CollectionsPage — child wiring', () => {
|
||||
expect(hook.setView).toHaveBeenCalledWith('map')
|
||||
})
|
||||
|
||||
it('FE-PAGE-COLLPAGE-028: from the map view a marker click drops back into the split', () => {
|
||||
it('FE-PAGE-COLLPAGE-028: on the full map a marker click shows the place and the map stays', () => {
|
||||
const hook = renderPage({ view: 'map' })
|
||||
fireEvent.click(screen.getByText('map-select'))
|
||||
expect(hook.setSelectedPlaceId).toHaveBeenCalledWith(10)
|
||||
expect(hook.setView).toHaveBeenCalledWith('list')
|
||||
expect(hook.setView).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-PAGE-COLLPAGE-028b: the map carries the trip map\'s padlock, remembered by the page', () => {
|
||||
const hook = renderPage({ mapLocked: true })
|
||||
expect(screen.getByTestId('map-locked')).toHaveTextContent('true')
|
||||
fireEvent.click(screen.getByText('map-lock'))
|
||||
expect(hook.toggleMapLocked).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('FE-PAGE-COLLPAGE-029: the map view toggle returns to the list without clearing the selection', () => {
|
||||
@@ -637,9 +650,17 @@ describe('CollectionsPage — detail sheet and modals', () => {
|
||||
expect(screen.getByTestId('detail-anchored')).toHaveTextContent('sheet')
|
||||
})
|
||||
|
||||
it('FE-PAGE-COLLPAGE-034: the full-map view hides the detail sheet', () => {
|
||||
it('FE-PAGE-COLLPAGE-034: on a narrow full map the detail opens as a sheet over the map', () => {
|
||||
renderPage({ ...withPlace, view: 'map', isWide: false })
|
||||
expect(screen.queryByTestId('detail')).toBeNull()
|
||||
expect(screen.getByTestId('detail-anchored')).toHaveTextContent('sheet')
|
||||
})
|
||||
|
||||
it('FE-PAGE-COLLPAGE-034b: on a wide full map the detail docks on the map\'s edge, once the map is measured', () => {
|
||||
renderPage({ ...withPlace, view: 'map' })
|
||||
expect(screen.getByTestId('detail-anchored')).toHaveTextContent('docked')
|
||||
cleanup()
|
||||
renderPage({ ...withPlace, view: 'map', mapColRect: null })
|
||||
expect(screen.getByTestId('detail-anchored')).toHaveTextContent('sheet')
|
||||
})
|
||||
|
||||
it('FE-PAGE-COLLPAGE-035: the add-place modal is bound to the active list', () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useNavigate, useParams, useSearchParams } from 'react-router'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { useElementSize } from '../../hooks/useElementSize'
|
||||
import { useElementRect } from '../../hooks/useElementRect'
|
||||
import { useMapLock } from '../../hooks/useMapLock'
|
||||
import { useSettingsStore } from '../../store/settingsStore'
|
||||
import { useAuthStore } from '../../store/authStore'
|
||||
import { useToast } from '../../components/shared/Toast'
|
||||
@@ -52,6 +53,10 @@ export function useCollections() {
|
||||
const hero = useElementSize<HTMLDivElement>()
|
||||
// Measure the split's list column so the place-detail sheet can dock over it.
|
||||
const listCol = useElementRect<HTMLDivElement>()
|
||||
// ...and the map column, so on a full map the sheet docks on the map's own edge.
|
||||
const mapCol = useElementRect<HTMLDivElement>()
|
||||
// The trip map's padlock (#2010): a locked map stays put when a place is picked.
|
||||
const [mapLocked, toggleMapLocked] = useMapLock()
|
||||
|
||||
const store = useCollectionStore()
|
||||
const {
|
||||
@@ -556,7 +561,8 @@ export function useCollections() {
|
||||
return {
|
||||
t, language, dark, navigate,
|
||||
isWide, heroRef: hero.ref, heroHeight: hero.height,
|
||||
listColRef: listCol.ref, listColRect: listCol.rect, categories,
|
||||
listColRef: listCol.ref, listColRect: listCol.rect, mapColRef: mapCol.ref, mapColRect: mapCol.rect, categories,
|
||||
mapLocked, toggleMapLocked,
|
||||
// store data
|
||||
collections, ownedLists, sharedLists, activeCollection, isAllSaved, isOwner,
|
||||
myRole, canEdit, canDelete,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { usePlannerTripView } from './usePlannerTripView'
|
||||
import type { TripStoreState } from '../../store/tripStore'
|
||||
import type { Place } from '../../types'
|
||||
import type { PlannerBase } from './plannerTypes'
|
||||
import { useMapLock } from '../../hooks/useMapLock'
|
||||
|
||||
interface PlannerMapViewOptions extends Pick<PlannerBase, 'tripId' | 'trip'> {
|
||||
places: Place[]
|
||||
@@ -60,20 +61,11 @@ export function usePlannerMapView(options: PlannerMapViewOptions) {
|
||||
// A locked map stays where the traveller put it (#2010): picking a day or a place no
|
||||
// longer zooms or pans it. Remembered per browser, like the other view toggles; the
|
||||
// first frame of a trip still fits, or the map would open on nothing.
|
||||
const [mapLocked, setMapLocked] = useState<boolean>(() => {
|
||||
try { return localStorage.getItem('trek:map-locked') === '1' } catch { return false }
|
||||
})
|
||||
const [mapLocked, toggleMapLocked] = useMapLock()
|
||||
const mapLockedRef = useRef(mapLocked)
|
||||
mapLockedRef.current = mapLocked
|
||||
const isMobileRef = useRef(isMobile)
|
||||
isMobileRef.current = isMobile
|
||||
const toggleMapLocked = useCallback(() => {
|
||||
setMapLocked(prev => {
|
||||
const next = !prev
|
||||
try { localStorage.setItem('trek:map-locked', next ? '1' : '0') } catch { /* private mode keeps it for the session */ }
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
// The recorded route from Dawarich (#2279), per trip and remembered on the
|
||||
// account for the same reason as the overview above: it answers "what
|
||||
// actually happened on this trip", a question about one trip rather than a
|
||||
|
||||
Reference in New Issue
Block a user