fix(planner): keep the places filter applied and visible across tab switches

The category and all/unplanned/tracks filters lived twice: a local copy in
the sidebar driving the checkboxes and the list, and a page-level copy
driving the map markers, synced only when a checkbox was clicked. Switching
planner tabs unmounts the sidebar, so the local copy reset to "All
Categories" while the markers stayed filtered — and the only way out was
toggling any category on and off again (#1541).

The filter now lives once in the trip store, next to selectedDayId: it
survives the Plan tab unmounting (and the mobile places sheet closing),
keeps both sidebar instances in agreement, and resets when another trip
loads.
This commit is contained in:
Maurice
2026-07-13 16:16:50 +02:00
parent bae98d1144
commit 2a722a5be0
6 changed files with 75 additions and 29 deletions
@@ -336,6 +336,39 @@ describe('Category filter dropdown', () => {
await user.click(parkOpts[parkOpts.length - 1]);
expect(screen.getByText(/2 categories/i)).toBeInTheDocument();
});
it('FE-PLANNER-SIDEBAR-047: category filter survives unmount/remount (#1541)', async () => {
const user = userEvent.setup();
const cat = buildCategory({ name: 'Hotel', color: '#3b82f6' });
const withCat = buildPlace({ name: 'Grand Palace', category_id: cat.id, address: 'Vienna' });
const noCat = buildPlace({ name: 'Street Market', category_id: null, address: 'Lisbon' });
const { unmount } = render(<PlacesSidebar {...defaultProps} places={[withCat, noCat]} categories={[cat]} />);
await user.click(screen.getByText(/All Categories/i));
await user.click(screen.getByText('Hotel'));
expect(screen.queryByText('Street Market')).not.toBeInTheDocument();
// Switching planner tabs unmounts the sidebar; the filter must come back
// both applied and visible instead of silently sticking on the map only.
unmount();
render(<PlacesSidebar {...defaultProps} places={[withCat, noCat]} categories={[cat]} />);
expect(screen.queryByText(/All Categories/i)).not.toBeInTheDocument();
expect(screen.getByText('Hotel')).toBeInTheDocument();
expect(screen.getByText('Grand Palace')).toBeInTheDocument();
expect(screen.queryByText('Street Market')).not.toBeInTheDocument();
});
it('FE-PLANNER-SIDEBAR-048: filter tab survives unmount/remount (#1541)', async () => {
const user = userEvent.setup();
const planned = buildPlace({ name: 'Planned Place' });
const unplanned = buildPlace({ name: 'Unplanned Place' });
const assignments = { '1': [buildAssignment({ place: planned, day_id: 1 })] };
const { unmount } = render(<PlacesSidebar {...defaultProps} places={[planned, unplanned]} assignments={assignments} />);
await user.click(screen.getByRole('button', { name: /Unplanned/i }));
expect(screen.queryByText('Planned Place')).not.toBeInTheDocument();
unmount();
render(<PlacesSidebar {...defaultProps} places={[planned, unplanned]} assignments={assignments} />);
expect(screen.queryByText('Planned Place')).not.toBeInTheDocument();
expect(screen.getByText('Unplanned Place')).toBeInTheDocument();
});
});
// ── Place list interaction ─────────────────────────────────────────────────────
@@ -23,8 +23,8 @@ export function PlacesHeader(S: SidebarState) {
const {
canEditPlaces, onAddPlace, t, setFileImportOpen, setListImportOpen, hasMultipleListImportProviders,
places, categories, categoryFilters, search, setSearch, plannedIds, hasTracks,
filter, setFilter, onPlacesFilterChange, setSelectedIds, selectMode, setSelectMode,
catDropOpen, setCatDropOpen, toggleCategoryFilter, setCategoryFiltersLocal, onCategoryFilterChange,
filter, setFilter, setSelectedIds, selectMode, setSelectMode,
catDropOpen, setCatDropOpen, toggleCategoryFilter, setCategoryFilters,
} = S
return (
<div className="border-b border-edge-faint" style={{ padding: '14px 16px 10px', flexShrink: 0 }}>
@@ -99,7 +99,7 @@ export function PlacesHeader(S: SidebarState) {
return (
<button
key={f.id}
onClick={() => { setFilter(f.id); onPlacesFilterChange?.(f.id); setSelectedIds(new Set()) }}
onClick={() => { setFilter(f.id); setSelectedIds(new Set()) }}
className={active ? 'bg-accent text-accent-text' : 'bg-surface-card text-content'}
style={{
appearance: 'none', border: 'none', cursor: 'pointer', fontFamily: 'inherit',
@@ -251,7 +251,7 @@ export function PlacesHeader(S: SidebarState) {
)
})()}
{categoryFilters.size > 0 && (
<button onClick={() => { setCategoryFiltersLocal(new Set()); onCategoryFilterChange?.(new Set()) }} className="bg-transparent text-content-faint" style={{
<button onClick={() => setCategoryFilters(new Set())} className="bg-transparent text-content-faint" style={{
display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 4,
width: '100%', padding: '6px 10px', borderRadius: 6, border: 'none', cursor: 'pointer',
fontFamily: 'inherit', fontSize: 'calc(11px * var(--fs-scale-caption, 1))',
@@ -33,8 +33,6 @@ export interface PlacesSidebarProps {
isMobile: boolean
/** Primary pointer is coarse — HTML5 drag would swallow the scroll gesture (#1432). */
isTouch?: boolean
onCategoryFilterChange?: (categoryIds: Set<string>) => void
onPlacesFilterChange?: (filter: string) => void
pushUndo?: (label: string, undoFn: () => Promise<void> | void) => void
initialScrollTop?: number
onScrollTopChange?: (top: number) => void
@@ -48,7 +46,7 @@ export interface PlacesSidebarProps {
export function usePlacesSidebar(props: PlacesSidebarProps) {
const {
tripId, places, assignments, selectedDayId,
onCategoryFilterChange, onPlacesFilterChange, pushUndo, initialScrollTop, onScrollTopChange,
pushUndo, initialScrollTop, onScrollTopChange,
} = props
const { t } = useTranslation()
const toast = useToast()
@@ -149,8 +147,13 @@ export function usePlacesSidebar(props: PlacesSidebarProps) {
}
const [search, setSearch] = useState('')
const [filter, setFilter] = useState('all')
const [categoryFilters, setCategoryFiltersLocal] = useState<Set<string>>(new Set())
// Filter state lives in the trip store so it survives the Plan tab
// unmounting (tab switch, mobile sheet close) and stays in lockstep with the
// map markers, which filter on the same values (#1541).
const filter = useTripStore((s) => s.placesFilter)
const setFilter = useTripStore((s) => s.setPlacesFilter)
const categoryFilters = useTripStore((s) => s.placesCategoryFilter)
const setCategoryFilters = useTripStore((s) => s.setPlacesCategoryFilter)
const [selectMode, setSelectMode] = useState(false)
const [selectedIds, setSelectedIds] = useState<Set<number>>(new Set())
const [pendingDeleteIds, setPendingDeleteIds] = useState<number[] | null>(null)
@@ -177,12 +180,9 @@ export function usePlacesSidebar(props: PlacesSidebarProps) {
}), [])
const toggleCategoryFilter = (catId: string) => {
setCategoryFiltersLocal(prev => {
const next = new Set(prev)
if (next.has(catId)) next.delete(catId); else next.add(catId)
onCategoryFilterChange?.(next)
return next
})
const next = new Set(categoryFilters)
if (next.has(catId)) next.delete(catId); else next.add(catId)
setCategoryFilters(next)
}
const [dayPickerPlace, setDayPickerPlace] = useState(null)
const [catDropOpen, setCatDropOpen] = useState(false)
@@ -266,7 +266,7 @@ export function usePlacesSidebar(props: PlacesSidebarProps) {
listImportLoading, listImportProvider, setListImportProvider,
listImportEnrich, setListImportEnrich, canEnrichImport,
availableListImportProviders, hasMultipleListImportProviders, handleListImport,
search, setSearch, filter, setFilter, categoryFilters, setCategoryFiltersLocal,
search, setSearch, filter, setFilter, categoryFilters, setCategoryFilters,
selectMode, setSelectMode, selectedIds, setSelectedIds, pendingDeleteIds, setPendingDeleteIds,
categoryPickerOpen, setCategoryPickerOpen,
saveToListOpen, setSaveToListOpen, collectionsEnabled, tripId,