feat(planner): open places and day routes in CoMaps

Add CoMaps, the offline OpenStreetMap navigator, alongside the existing
map integrations. A geolocated place gains a CoMaps entry in its
navigation menu, and a day's route toolbar gains a button that hands the
day's bookended stops to CoMaps in that day's travel mode.

CoMaps' route link carries only a start and a destination, so a two-stop
day goes as a route and a longer one as named pins rather than silently
losing the stops in between. Links use the https form so a device without
CoMaps installed lands on comaps.at instead of a dead scheme.

The hotel-bookend logic behind the Google Maps export was duplicated
between desktop and mobile; both now share dayExportStops so the two
links cannot disagree about which hotel legs are real.

Addresses discussion #1904
This commit is contained in:
Konstantinos Thermos
2026-08-14 10:24:14 +03:00
parent 813b9886b3
commit 8065af28ef
59 changed files with 429 additions and 52 deletions
@@ -0,0 +1,67 @@
import { describe, expect, it } from 'vitest'
import { dayCoMapsUrl, dayExportStops, dayGoogleMapsUrl } from './dayRoute'
import type { Accommodation, Assignment, Day } from '../../../../types'
// FE-MOBILE-DAYROUTE-001 to FE-MOBILE-DAYROUTE-005
const days = [
{ id: 10, day_number: 1 },
{ id: 20, day_number: 2 },
{ id: 30, day_number: 3 },
] as unknown as Day[]
const hotel = {
place_lat: 48.85, place_lng: 2.35, place_name: 'Hotel Lutetia',
start_day_id: 10, end_day_id: 30,
} as unknown as Accommodation
const stop = (id: number, name: string, lat: number, lng: number): Assignment =>
({ id, day_id: 20, order_index: id, place: { id, name, lat, lng } }) as unknown as Assignment
const louvre = stop(1, 'Louvre', 48.86, 2.34)
const orsay = stop(2, 'Orsay', 48.87, 2.33)
describe('dayExportStops', () => {
it('FE-MOBILE-DAYROUTE-001: carries the stops in planned order, hotel-bookended and named', () => {
expect(dayExportStops(days[1], days, [louvre, orsay], [hotel], true)).toEqual([
{ lat: 48.85, lng: 2.35, name: 'Hotel Lutetia' },
{ lat: 48.86, lng: 2.34, name: 'Louvre' },
{ lat: 48.87, lng: 2.33, name: 'Orsay' },
{ lat: 48.85, lng: 2.35, name: 'Hotel Lutetia' },
])
})
it('FE-MOBILE-DAYROUTE-002: drops the bookends when accommodation optimization is off', () => {
const stops = dayExportStops(days[1], days, [louvre, orsay], [hotel], false)
expect(stops.map(s => s.name)).toEqual(['Louvre', 'Orsay'])
})
it('FE-MOBILE-DAYROUTE-003: a stop without coordinates cannot be exported', () => {
const nowhere = { id: 3, day_id: 20, order_index: 3, place: { id: 3, name: 'TBD' } } as unknown as Assignment
const stops = dayExportStops(days[1], days, [louvre, nowhere], [], false)
expect(stops.map(s => s.name)).toEqual(['Louvre'])
})
})
describe('day map links', () => {
it('FE-MOBILE-DAYROUTE-004: both exports read the same stop list', () => {
// One source for the bookend rules, so the two links can never disagree
// about which hotel legs are real.
const google = dayGoogleMapsUrl(days[1], days, [louvre, orsay], [hotel], true)!
const coMaps = dayCoMapsUrl(days[1], days, [louvre, orsay], [hotel], true, 'walking')!
for (const ll of ['48.85,2.35', '48.86,2.34', '48.87,2.33']) {
expect(google).toContain(ll)
expect(coMaps).toContain(ll)
}
})
it('FE-MOBILE-DAYROUTE-005: the CoMaps link travels in the mode it was given', () => {
// Four bookended stops is more than a CoMaps route link can hold, so it
// becomes pins — which is also why no mode appears here.
expect(dayCoMapsUrl(days[1], days, [louvre, orsay], [hotel], true, 'walking'))
.toMatch(/^https:\/\/comaps\.at\/map\?v=1&/)
// Two stops fit a route, and then the mode does apply.
expect(dayCoMapsUrl(days[1], days, [louvre, orsay], [], false, 'walking'))
.toContain('type=pedestrian')
})
})
+48 -16
View File
@@ -1,4 +1,7 @@
import { generateGoogleMapsUrl, optimizeRoute } from '../../../../components/Map/RouteCalculator'
import {
generateCoMapsUrl, generateGoogleMapsUrl, optimizeRoute,
type NamedWaypoint, type RouteProfileKey,
} from '../../../../components/Map/RouteCalculator'
import {
getAccommodationAnchors, getDayBookendHotels, shouldDrawEveningLeg, shouldDrawMorningLeg,
} from '../../../../utils/dayOrder'
@@ -54,9 +57,33 @@ export function optimizeDayOrder(
}
/**
* Google-Maps directions URL over the day's located stops, bookended by the
* morning/evening accommodation exactly like the drawn route.
* The day's located stops in planned order, bookended by the morning/evening
* accommodation exactly like the drawn route. Names ride along for the deep
* links that can label a pin with one.
*/
export function dayExportStops(
day: Day,
days: Day[],
dayAssignments: Assignment[],
accommodations: Accommodation[],
bookendFromAccommodation: boolean,
): NamedWaypoint[] {
const located = dayAssignments.filter(a => a.place?.lat != null && a.place?.lng != null)
const stops = located.map(a => ({ lat: a.place!.lat!, lng: a.place!.lng!, name: a.place!.name }))
const bookends = bookendFromAccommodation ? getDayBookendHotels(day, days, accommodations) : null
const firstStop = located[0] ? { isPlace: true, time: located[0].place?.place_time ?? null } : undefined
const last = located[located.length - 1]
const lastStop = last ? { isPlace: true, time: last.place?.place_time ?? null } : undefined
const morning = bookends && shouldDrawMorningLeg(bookends, day, firstStop)
&& bookends.morning?.place_lat != null && bookends.morning?.place_lng != null
? { lat: bookends.morning.place_lat, lng: bookends.morning.place_lng, name: bookends.morning.place_name } : null
const evening = bookends && shouldDrawEveningLeg(bookends, day, lastStop)
&& bookends.evening?.place_lat != null && bookends.evening?.place_lng != null
? { lat: bookends.evening.place_lat, lng: bookends.evening.place_lng, name: bookends.evening.place_name } : null
return [...(morning ? [morning] : []), ...stops, ...(evening ? [evening] : [])]
}
/** Google-Maps directions URL over the day's bookended stops. */
export function dayGoogleMapsUrl(
day: Day,
days: Day[],
@@ -64,17 +91,22 @@ export function dayGoogleMapsUrl(
accommodations: Accommodation[],
bookendFromAccommodation: boolean,
): string | null {
const located = dayAssignments.filter(a => a.place?.lat != null && a.place?.lng != null)
const stops = located.map(a => ({ lat: a.place!.lat!, lng: a.place!.lng! }))
const bookends = bookendFromAccommodation ? getDayBookendHotels(day, days, accommodations) : null
const firstStop = located[0] ? { isPlace: true, time: located[0].place?.place_time ?? null } : undefined
const last = located[located.length - 1]
const lastStop = last ? { isPlace: true, time: last.place?.place_time ?? null } : undefined
const morning = bookends && shouldDrawMorningLeg(bookends, day, firstStop)
&& bookends.morning?.place_lat != null && bookends.morning?.place_lng != null
? { lat: bookends.morning.place_lat, lng: bookends.morning.place_lng } : null
const evening = bookends && shouldDrawEveningLeg(bookends, day, lastStop)
&& bookends.evening?.place_lat != null && bookends.evening?.place_lng != null
? { lat: bookends.evening.place_lat, lng: bookends.evening.place_lng } : null
return generateGoogleMapsUrl([...(morning ? [morning] : []), ...stops, ...(evening ? [evening] : [])]) || null
return generateGoogleMapsUrl(
dayExportStops(day, days, dayAssignments, accommodations, bookendFromAccommodation),
) || null
}
/** The same stops handed to CoMaps for offline navigation, in the day's travel mode (#1904). */
export function dayCoMapsUrl(
day: Day,
days: Day[],
dayAssignments: Assignment[],
accommodations: Accommodation[],
bookendFromAccommodation: boolean,
profile: RouteProfileKey,
): string | null {
return generateCoMapsUrl(
dayExportStops(day, days, dayAssignments, accommodations, bookendFromAccommodation),
profile,
) || null
}
@@ -1,6 +1,6 @@
import { useState, type MouseEvent } from 'react'
import {
ArrowRight, ArrowUpRight, BedDouble, CalendarRange, ChevronRight, LogIn, LogOut,
ArrowRight, ArrowUpRight, BedDouble, CalendarRange, ChevronRight, Compass, LogIn, LogOut,
MapPin, Pencil, PencilLine, Route, Ticket, TrainFront, Undo2,
Car, Footprints, Zap, RotateCcw,
} from 'lucide-react'
@@ -186,6 +186,7 @@ export default function MPlanTimeline({ planner, shell }: MPlanTimelineProps) {
<PlanAction icon={TrainFront} label={t('mobileTrip.addTransportShort')} onClick={tl.addTransport} />
<PlanAction icon={Route} label={t('dayplan.optimize')} onClick={() => void tl.optimize()} />
<PlanAction icon={ArrowUpRight} label={t('mobileTrip.googleMaps')} onClick={tl.exportGoogleMaps} />
<PlanAction icon={Compass} label={t('mobileTrip.coMaps')} onClick={tl.exportCoMaps} />
</div>
)}
</div>
@@ -5,7 +5,7 @@ import { assignmentsApi, reservationsApi, weatherApi } from '../../../../api/cli
import { usePluginStore } from '../../../../store/pluginStore'
import { getDayBookendHotels } from '../../../../utils/dayOrder'
import { getDisplayTimeForDay, getMergedItems, getTransportForDay } from '../../../../utils/dayMerge'
import { dayGoogleMapsUrl, optimizeDayOrder } from '../lib/dayRoute'
import { dayCoMapsUrl, dayGoogleMapsUrl, optimizeDayOrder } from '../lib/dayRoute'
import {
buildPlanRows, breaksChronology, findUpNext, hotelChipsForDay, hotelLegsForDay, itemHasTime,
type HotelLegs, type PlanRow, type TransportEntry,
@@ -307,6 +307,15 @@ export function useMPlanTimeline(planner: TripPlanner) {
if (url) window.open(url, '_blank', 'noopener,noreferrer')
}, [day, dayAssignments, days, tripAccommodations, settings])
const exportCoMaps = useCallback(() => {
if (!day) return
const url = dayCoMapsUrl(
day, days, dayAssignments, tripAccommodations, settings.optimize_from_accommodation !== false,
day.default_transport_mode ?? routeProfile,
)
if (url) window.open(url, '_blank', 'noopener,noreferrer')
}, [day, dayAssignments, days, tripAccommodations, settings, routeProfile])
const renameDay = useCallback((title: string) => {
if (!day) return
void planner.handleUpdateDayTitle(day.id, title.trim())
@@ -352,7 +361,7 @@ export function useMPlanTimeline(planner: TripPlanner) {
openTransitKeys, toggleTransit,
moveRow, removeAssignment, editAssignment, editTransport, openTransitJourney,
addPlace, addBooking, addTransport,
optimize, exportGoogleMaps, renameDay, fullPlaceOf,
optimize, exportGoogleMaps, exportCoMaps, renameDay, fullPlaceOf,
routeModeOptions, setLegMode,
}
}
@@ -1,6 +1,6 @@
import { useEffect, useMemo, useRef, useState } from 'react'
import {
CalendarDays, Car, Footprints, Hotel, Pencil, Plus, RotateCcw,
CalendarDays, Car, Compass, Footprints, Hotel, Pencil, Plus, RotateCcw,
Route as RouteIcon, TramFront, Zap,
} from 'lucide-react'
import type { WeatherResult } from '@trek/shared'
@@ -14,7 +14,7 @@ import { useDayNotes } from '../../../../hooks/useDayNotes'
import { RES_ICONS, getNoteIcon } from '../../../../components/Planner/DayPlanSidebar.constants'
import { getDayBookendHotels, isDayInAccommodationRange } from '../../../../utils/dayOrder'
import { splitReservationDateTime } from '../../../../utils/formatters'
import { dayGoogleMapsUrl, optimizeDayOrder } from '../lib/dayRoute'
import { dayCoMapsUrl, dayGoogleMapsUrl, optimizeDayOrder } from '../lib/dayRoute'
import { splitNoteTime } from '../lib/dayNotes'
import { weatherIconFor } from '../plan/planTimelineModel'
import type { Assignment, DayNote, Reservation } from '../../../../types'
@@ -182,6 +182,16 @@ export default function MDaySheet({ planner, shell }: MTripSheetsProps) {
if (url) window.open(url, '_blank', 'noopener,noreferrer')
}
// The same day in CoMaps, for navigating it offline (#1904).
const openInCoMaps = () => {
if (!day) return
const url = dayCoMapsUrl(
day, planner.days, dayAssignments, planner.tripAccommodations, optimizeFromAccommodation !== false,
day.default_transport_mode ?? planner.routeProfile,
)
if (url) window.open(url, '_blank', 'noopener,noreferrer')
}
const addAccommodation = () => {
if (!day) return
shell.openSheet('accommodation', { dayId: day.id })
@@ -327,6 +337,16 @@ export default function MDaySheet({ planner, shell }: MTripSheetsProps) {
{t('planner.openGoogleMaps')}
</button>
)}
{routable && (
<button
type="button"
onClick={openInCoMaps}
className={`flex items-center gap-[5px] rounded-full px-3 py-[7px] text-[0.75rem] font-semibold text-m-ink ${INNER_CLS}`}
>
<Compass size={13} strokeWidth={2} />
{t('planner.openCoMaps')}
</button>
)}
{canEditDays && dayAssignments.length >= 3 && (
<button
type="button"