refactor(tours): share the planned tours filter between desktop and phone

The All, Unplanned and Planned filter of the tours list was copied into the phone selection list. It now lives in filterTours next to the other tour presentation helpers, and both lists call it.
This commit is contained in:
Maurice
2026-10-09 12:07:25 +02:00
parent f84924c56a
commit 6d2490a3ba
5 changed files with 36 additions and 15 deletions
+3 -6
View File
@@ -12,6 +12,7 @@ import { NEUTRAL_TINT, fs } from '../shared/DialogShell'
import { Tooltip } from '../shared/Tooltip'
import EmptyState from '../shared/EmptyState'
import { TourDayMenu } from './tourParts'
import { filterTours, type TourFilter } from './tourPresentation'
const MAX_FILE_BYTES = 10 * 1024 * 1024
@@ -44,7 +45,7 @@ export default function ToursSidebar({ tripId, days, tours, loading = false, sel
const { canEdit, canAssign } = useTourPermissions({ tripId, canEdit: editPermission, canAssign: assignPermission })
const toast = useToast()
const loadTrip = useTripStore(s => s.loadTrip)
const [filter, setFilter] = useState<'all' | 'unplanned' | 'planned'>('all')
const [filter, setFilter] = useState<TourFilter>('all')
const [importing, setImporting] = useState(false)
const fileInputRef = useRef<HTMLInputElement>(null)
const filterRef = useRef<HTMLDivElement>(null)
@@ -95,11 +96,7 @@ export default function ToursSidebar({ tripId, days, tours, loading = false, sel
unplanned: tours.filter(tr => !tr.planned).length,
planned: tours.filter(tr => tr.planned).length,
}
const filtered = tours.filter(tr => {
if (filter === 'unplanned') return !tr.planned
if (filter === 'planned') return tr.planned
return true
})
const filtered = filterTours(tours, filter)
useEffect(() => {
if (!filterOpen) return
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { hikeSourceBadgeLabel, tourSource } from './tourPresentation'
import { filterTours, hikeSourceBadgeLabel, tourSource } from './tourPresentation'
describe('tourPresentation', () => {
it('uses the source abstraction for TREK, GPX import, and wanderer badges', () => {
@@ -12,4 +12,19 @@ describe('tourPresentation', () => {
expect(tourSource({ wanderer_ref: 'wanderer:123' })).toBe('wanderer')
expect(hikeSourceBadgeLabel({ wanderer_ref: 'wanderer:123' }, t)).toBe('tours.badge.wanderer')
})
it('filters tours by All, Unplanned and Planned, keeping their order', () => {
const tours = [
{ id: 1, planned: true },
{ id: 2, planned: false },
{ id: 3, planned: true },
{ id: 4, planned: false },
]
expect(filterTours(tours, 'all')).toEqual(tours)
expect(filterTours(tours, 'all')).not.toBe(tours)
expect(filterTours(tours, 'unplanned').map(tour => tour.id)).toEqual([2, 4])
expect(filterTours(tours, 'planned').map(tour => tour.id)).toEqual([1, 3])
expect(filterTours([], 'planned')).toEqual([])
})
})
@@ -2,6 +2,9 @@ import type { TourListItem } from '@trek/shared'
export type TourSource = 'trek' | 'gpx' | 'wanderer'
/** All / Unplanned / Planned, the same three states as the Places pool. */
export type TourFilter = 'all' | 'unplanned' | 'planned'
type Translate = (key: string, params?: Record<string, unknown>) => string
/**
@@ -20,3 +23,12 @@ export function hikeSourceBadgeLabel(
): string {
return t(`tours.badge.${tourSource(tour)}`)
}
/** The tours a list shows under the All / Unplanned / Planned filter, in their order. */
export function filterTours<T extends Pick<TourListItem, 'planned'>>(tours: T[], filter: TourFilter): T[] {
return tours.filter(tr => {
if (filter === 'unplanned') return !tr.planned
if (filter === 'planned') return tr.planned
return true
})
}