mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
feat(tours): open a tour's route in Google Maps, Apple Maps or CoMaps (#2812)
The tour detail gets a Maps button beside Navigation that hands the whole route to the apps that follow one, in the tour's own mode (walking or cycling). The stops are spread evenly along the line and capped at ten, the most a Google Maps directions link opens. Ski, paddle and horse tours are not offered, since a map app would route them by road; the GPX export stays the way for those.
This commit is contained in:
@@ -411,4 +411,23 @@ describe('TourDetailDialog', () => {
|
||||
expect(screen.queryByTestId('paddle-check-summary')).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: /Paddling has risks/ })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("#2812: offers the whole route in the map apps that follow one, in the tour's own mode", () => {
|
||||
const open = vi.spyOn(window, 'open').mockImplementation(() => null)
|
||||
render(<TourDetailDialog {...defaultProps} tour={{ ...tour, tour_type: 'bike' }} />)
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Open the route in a map app' }))
|
||||
const menu = screen.getByRole('menu')
|
||||
// Only the apps that take a route: Waze and geo: lead to the start, which Navigation already does.
|
||||
expect(Array.from(menu.querySelectorAll('[role="menuitem"]')).map(item => item.textContent)).toEqual(['Google Maps', 'CoMaps'])
|
||||
|
||||
fireEvent.click(screen.getByRole('menuitem', { name: 'CoMaps' }))
|
||||
// Three points along the line go as pins; the start carries the tour's name.
|
||||
expect(open).toHaveBeenCalledWith(expect.stringContaining('https://comaps.at/map?v=1&ll=48,11&n=Ridge%20walk'), '_blank', 'noopener,noreferrer')
|
||||
})
|
||||
|
||||
it('#2812: a paddle tour is not handed to a map app, which would route it by road', () => {
|
||||
render(<TourDetailDialog {...defaultProps} tour={{ ...tour, tour_type: 'kayak', paddle_check: paddleBench() }} />)
|
||||
expect(screen.queryByRole('button', { name: 'Open the route in a map app' })).not.toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
FileImage,
|
||||
FileText,
|
||||
Loader2,
|
||||
MapPinned,
|
||||
Minus,
|
||||
Navigation,
|
||||
Pencil,
|
||||
@@ -73,6 +74,7 @@ import {
|
||||
tourWebsitePresentation,
|
||||
type TourPlannedTimes,
|
||||
} from './tourPresentation';
|
||||
import { tourRouteTargets } from './tourRouteApps';
|
||||
import { tourShareFactsOf } from './tourShareText';
|
||||
import { useTourDayAssignment } from './useTourDayAssignment';
|
||||
import { useTourPermissions, type TourPermissionProps } from './useTourPermissions';
|
||||
@@ -540,6 +542,8 @@ export default function TourDetailDialog({
|
||||
const [colorOpen, setColorOpen] = useState(false);
|
||||
const [navigationOpen, setNavigationOpen] = useState(false);
|
||||
const navigationAnchorRef = useRef<HTMLSpanElement>(null);
|
||||
const [routeAppsOpen, setRouteAppsOpen] = useState(false);
|
||||
const routeAppsAnchorRef = useRef<HTMLSpanElement>(null);
|
||||
useDesktopEscape(desktopNonModal, onClose, desktopFocusReturnTarget);
|
||||
|
||||
const heightsHidden = heightsUnknown(tour);
|
||||
@@ -555,6 +559,11 @@ export default function TourDetailDialog({
|
||||
const navigationTargets = getNavigationTargets(place);
|
||||
const navigationLabel =
|
||||
navigationTargets.length === 1 ? navigationTargetLabel(navigationTargets[0], t) : t('inspector.navigation');
|
||||
// #2812: the whole route, not only the way to its start, in the apps that follow one.
|
||||
const routeTargets = useMemo(
|
||||
() => (analysis ? tourRouteTargets(tour.tour_type, analysis.routeCoordinates, place.name) : []),
|
||||
[analysis, tour.tour_type, place.name]
|
||||
);
|
||||
const description = tour.description !== undefined ? tour.description : place.description;
|
||||
const website = tourWebsitePresentation(tour.website !== undefined ? tour.website : place.website);
|
||||
const times = tourPlannedTimes(tour);
|
||||
@@ -663,6 +672,31 @@ export default function TourDetailDialog({
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{routeTargets.length > 0 && (
|
||||
<>
|
||||
<Tooltip label={t('tours.detail.openRoute')}>
|
||||
<span ref={routeAppsAnchorRef} className="inline-flex">
|
||||
<DialogButton
|
||||
onClick={() => setRouteAppsOpen((open) => !open)}
|
||||
aria-label={t('tours.detail.openRoute')}
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={routeAppsOpen}
|
||||
data-testid="tour-route-apps"
|
||||
icon={<MapPinned size={14} strokeWidth={2} />}
|
||||
>
|
||||
<span className="max-sm:hidden">{t('tours.detail.mapsShort')}</span>
|
||||
</DialogButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
{routeAppsOpen && (
|
||||
<NavigationMenu
|
||||
targets={routeTargets}
|
||||
anchor={routeAppsAnchorRef.current}
|
||||
onClose={() => setRouteAppsOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{website && (
|
||||
<Tooltip label={website.domain}>
|
||||
<span className="inline-flex">
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
// FE-TOUR-ROUTEAPPS-001 to FE-TOUR-ROUTEAPPS-004
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { TOUR_APP_MAX_STOPS, tourAppProfile, tourRouteStops, tourRouteTargets } from './tourRouteApps';
|
||||
|
||||
/** A straight line east along the equator, one point every 0.01 degrees. */
|
||||
const line = (points: number): [number, number][] => Array.from({ length: points }, (_, i) => [0, i / 100]);
|
||||
|
||||
describe('tourRouteApps (#2812)', () => {
|
||||
it('FE-TOUR-ROUTEAPPS-001: a short line goes whole, the start named by the tour', () => {
|
||||
expect(tourRouteStops(line(3), 'Ridge walk')).toEqual([
|
||||
{ lat: 0, lng: 0, name: 'Ridge walk' },
|
||||
{ lat: 0, lng: 0.01 },
|
||||
{ lat: 0, lng: 0.02 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-TOUR-ROUTEAPPS-002: a long line is thinned to the cap, evenly along it, keeping both ends', () => {
|
||||
const stops = tourRouteStops(line(101), 'Long ride');
|
||||
expect(stops).toHaveLength(TOUR_APP_MAX_STOPS);
|
||||
expect(stops[0]).toEqual({ lat: 0, lng: 0, name: 'Long ride' });
|
||||
expect(stops[stops.length - 1]).toEqual({ lat: 0, lng: 1 });
|
||||
// Nine equal steps over a hundred points: every eleventh point, give or take one.
|
||||
const steps = stops.slice(1).map((stop, i) => Math.round((stop.lng - stops[i].lng) * 100));
|
||||
expect(steps.every((step) => step === 11 || step === 12)).toBe(true);
|
||||
});
|
||||
|
||||
it('FE-TOUR-ROUTEAPPS-003: walks and hikes are walked, bike tours ridden, and the rest offered to no app', () => {
|
||||
expect(tourAppProfile('hike')).toBe('walking');
|
||||
expect(tourAppProfile('walk')).toBe('walking');
|
||||
expect(tourAppProfile('bike')).toBe('cycling');
|
||||
expect(tourAppProfile('kayak')).toBeNull();
|
||||
expect(tourAppProfile('ski')).toBeNull();
|
||||
expect(tourAppProfile('horse')).toBeNull();
|
||||
expect(tourRouteTargets('kayak', line(5), 'Lake')).toEqual([]);
|
||||
});
|
||||
|
||||
it('FE-TOUR-ROUTEAPPS-004: only the apps that take the whole route, with the mode the tour is routed in', () => {
|
||||
const targets = tourRouteTargets('bike', line(2), 'Ride');
|
||||
expect(targets.map((target) => target.id)).toEqual(['google', 'comaps']);
|
||||
expect(targets.find((target) => target.id === 'comaps')?.url).toContain('type=bicycle');
|
||||
expect(targets.find((target) => target.id === 'google')?.url).toBe('https://www.google.com/maps/dir/0,0/0,0.01');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
import type { TourTypeKey } from '@trek/shared';
|
||||
|
||||
import { haversineKm } from '../../utils/geo';
|
||||
import type { NamedWaypoint, RouteProfileKey } from '../Map/RouteCalculator';
|
||||
import { dayRouteTargets, type DayRouteTarget } from '../Planner/dayRouteApps';
|
||||
|
||||
/**
|
||||
* The most stops a tour hands to a map app (#2812). Google Maps opens a
|
||||
* directions link with at most ten stops, a start, a destination and eight in
|
||||
* between, and drops the rest without a word. The other apps get the same
|
||||
* stops, so every app is handed the same route.
|
||||
*/
|
||||
export const TOUR_APP_MAX_STOPS = 10;
|
||||
|
||||
/**
|
||||
* How a map app follows each kind of tour. A ski, paddle or horse tour has no
|
||||
* mode in any of them: handed over, it would be routed by road, so it is not
|
||||
* offered and the GPX file stays the way to take it along.
|
||||
*/
|
||||
const APP_PROFILE: Partial<Record<TourTypeKey, RouteProfileKey>> = {
|
||||
hike: 'walking',
|
||||
walk: 'walking',
|
||||
food: 'walking',
|
||||
run: 'walking',
|
||||
bike: 'cycling',
|
||||
};
|
||||
|
||||
/** The mode a map app follows this tour in, or null where no map app can. */
|
||||
export function tourAppProfile(type: TourTypeKey): RouteProfileKey | null {
|
||||
return APP_PROFILE[type] ?? null;
|
||||
}
|
||||
|
||||
const at = ([lat, lng]: readonly [number, number]) => ({ lat, lng });
|
||||
|
||||
/**
|
||||
* Stops along a tour's line, spread evenly over its length, so the app routes
|
||||
* the way the tour goes rather than the shortest way between its ends. The
|
||||
* line's own first and last points are always among them, which keeps a round
|
||||
* tour round. The start carries the tour's name, the one place an app shows it.
|
||||
*/
|
||||
export function tourRouteStops(
|
||||
line: ReadonlyArray<readonly [number, number]>,
|
||||
name: string,
|
||||
max = TOUR_APP_MAX_STOPS
|
||||
): NamedWaypoint[] {
|
||||
return spreadIndices(line, max).map((i, n) => ({ ...at(line[i]), ...(n === 0 ? { name } : {}) }));
|
||||
}
|
||||
|
||||
/** The indices of at most `max` points of the line, its ends and the points nearest to even steps along it. */
|
||||
function spreadIndices(line: ReadonlyArray<readonly [number, number]>, max: number): number[] {
|
||||
if (line.length <= max) return line.map((_, i) => i);
|
||||
const along = [0];
|
||||
for (let i = 1; i < line.length; i++) along.push(along[i - 1] + haversineKm(at(line[i - 1]), at(line[i])));
|
||||
const total = along[along.length - 1];
|
||||
const last = line.length - 1;
|
||||
const picked = [0];
|
||||
let index = 0;
|
||||
for (let k = 1; k < max - 1; k++) {
|
||||
const target = (total * k) / (max - 1);
|
||||
while (index < last && along[index] < target) index++;
|
||||
const nearest = target - along[index - 1] < along[index] - target ? index - 1 : index;
|
||||
if (nearest > picked[picked.length - 1] && nearest < last) picked.push(nearest);
|
||||
}
|
||||
picked.push(last);
|
||||
return picked;
|
||||
}
|
||||
|
||||
/**
|
||||
* The map apps a tour's route can be opened in, with the tour's mode. Only the
|
||||
* apps that take the whole route: one that only gets the start (Waze, a geo:
|
||||
* link) is what the tour's Navigation button already offers.
|
||||
*/
|
||||
export function tourRouteTargets(
|
||||
type: TourTypeKey,
|
||||
line: ReadonlyArray<readonly [number, number]>,
|
||||
name: string
|
||||
): DayRouteTarget[] {
|
||||
const profile = tourAppProfile(type);
|
||||
if (!profile) return [];
|
||||
return dayRouteTargets(tourRouteStops(line, name), profile).filter((target) => !target.firstStopOnly);
|
||||
}
|
||||
Reference in New Issue
Block a user