mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
feat(mobile): one Maps button for every map app, actions as one glass group
The day plan offered Google Maps and CoMaps as two tiles. One Maps button now opens the day in whichever app the traveller picks: Google Maps, Apple Maps with the stops between as waypoints, CoMaps, Waze and on Android any other map app, the last two to the first stop, which they say. The preferred map app from the settings opens straight away. The day's actions sit together on one glass surface as round icons over their labels, and the up-next card wears the same surface as the timeline card under it.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { dayRouteTargets, generateAppleMapsUrl, generateGeoUrl, generateWazeUrl } from './dayRouteApps';
|
||||
|
||||
const A = { lat: 35.71, lng: 139.79, name: 'Senso-ji' };
|
||||
const B = { lat: 35.72, lng: 139.77, name: 'Ueno Park' };
|
||||
const C = { lat: 35.66, lng: 139.7, name: 'Shibuya' };
|
||||
|
||||
function userAgent(ua: string) {
|
||||
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue(ua);
|
||||
}
|
||||
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
describe('dayRouteApps', () => {
|
||||
it('opens one stop in Apple Maps as a named pin', () => {
|
||||
expect(generateAppleMapsUrl([A])).toBe('https://maps.apple.com/?ll=35.71,139.79&q=Senso-ji');
|
||||
});
|
||||
|
||||
it('hands Apple Maps the whole day, the stops between as waypoints, in the travel mode', () => {
|
||||
expect(generateAppleMapsUrl([A, B, C], 'walking')).toBe(
|
||||
'https://maps.apple.com/directions?source=35.71,139.79&destination=35.66,139.7&waypoint=35.72,139.77&mode=walking'
|
||||
);
|
||||
});
|
||||
|
||||
it('sends Waze and geo: to the first stop, since they take one place', () => {
|
||||
expect(generateWazeUrl([A, B])).toBe('https://waze.com/ul?ll=35.71,139.79&navigate=yes');
|
||||
expect(generateGeoUrl([A, B])).toBe('geo:35.71,139.79?q=35.71,139.79(Senso-ji)');
|
||||
expect(generateWazeUrl([])).toBeNull();
|
||||
});
|
||||
|
||||
it('offers Apple Maps on an iPhone and marks the one-place apps for a multi-stop day', () => {
|
||||
userAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)');
|
||||
const targets = dayRouteTargets([A, B], 'driving');
|
||||
expect(targets.map((t) => t.id)).toEqual(['google', 'apple', 'comaps', 'waze']);
|
||||
expect(targets.find((t) => t.id === 'waze')?.firstStopOnly).toBe(true);
|
||||
expect(targets.find((t) => t.id === 'google')?.firstStopOnly).toBeUndefined();
|
||||
});
|
||||
|
||||
it('offers the generic map-app link on Android instead of Apple Maps', () => {
|
||||
userAgent('Mozilla/5.0 (Linux; Android 15)');
|
||||
expect(dayRouteTargets([A], 'driving').map((t) => t.id)).toEqual(['google', 'comaps', 'waze', 'geo']);
|
||||
// A single stop is the whole day, so nothing is cut short.
|
||||
expect(dayRouteTargets([A], 'driving').some((t) => t.firstStopOnly)).toBe(false);
|
||||
});
|
||||
|
||||
it('offers nothing for a day without a located stop', () => {
|
||||
expect(dayRouteTargets([], 'driving')).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
import {
|
||||
generateCoMapsUrl,
|
||||
generateGoogleMapsUrl,
|
||||
type NamedWaypoint,
|
||||
type RouteProfileKey,
|
||||
} from '../Map/RouteCalculator';
|
||||
import { showsAppleMaps, showsGeoUri, type NavigationAppId } from './placeNavigation';
|
||||
|
||||
/**
|
||||
* The map apps a day's route can be handed to, behind the plan's one Maps
|
||||
* button. Same stops as every other export (dayExportStops), so the app shows
|
||||
* the day the plan shows.
|
||||
*
|
||||
* Not every app takes a whole route. Google Maps, Apple Maps and CoMaps do (each
|
||||
* in its own way, see the builders); Waze and the generic geo: link navigate to
|
||||
* one place only, so they lead to the first stop and say so rather than quietly
|
||||
* dropping the rest of the day.
|
||||
*/
|
||||
export interface DayRouteTarget {
|
||||
id: NavigationAppId;
|
||||
/** Product name, never translated; the generic entry carries labelKey instead. */
|
||||
label: string;
|
||||
labelKey?: string;
|
||||
url: string;
|
||||
/** True when the app only gets the first stop, not the whole route. */
|
||||
firstStopOnly?: boolean;
|
||||
}
|
||||
|
||||
/** Apple Maps' directions modes. Cycling exists there; anything else drives. */
|
||||
function appleMode(profile: RouteProfileKey): string {
|
||||
if (profile === 'walking') return 'walking';
|
||||
if (profile === 'cycling') return 'cycling';
|
||||
return 'driving';
|
||||
}
|
||||
|
||||
const point = (p: NamedWaypoint) => `${p.lat},${p.lng}`;
|
||||
const label = (p: NamedWaypoint) => encodeURIComponent(p.name?.trim() || point(p));
|
||||
|
||||
/**
|
||||
* Apple Maps for a day: one stop opens as a pin with its name, more go as
|
||||
* directions with the stops between as waypoints (Apple's unified Maps URLs,
|
||||
* which iOS, macOS and the web version all read).
|
||||
*/
|
||||
export function generateAppleMapsUrl(places: NamedWaypoint[], profile: RouteProfileKey = 'driving'): string | null {
|
||||
const valid = places.filter((p) => p.lat != null && p.lng != null);
|
||||
if (valid.length === 0) return null;
|
||||
if (valid.length === 1) return `https://maps.apple.com/?ll=${point(valid[0])}&q=${label(valid[0])}`;
|
||||
const [first, ...rest] = valid;
|
||||
const last = rest[rest.length - 1];
|
||||
const between = rest
|
||||
.slice(0, -1)
|
||||
.map((p) => `&waypoint=${point(p)}`)
|
||||
.join('');
|
||||
return `https://maps.apple.com/directions?source=${point(first)}&destination=${point(last)}${between}&mode=${appleMode(profile)}`;
|
||||
}
|
||||
|
||||
/** Waze navigates to one place; the day's first stop is where the drive starts. */
|
||||
export function generateWazeUrl(places: NamedWaypoint[]): string | null {
|
||||
const first = places.find((p) => p.lat != null && p.lng != null);
|
||||
return first ? `https://waze.com/ul?ll=${point(first)}&navigate=yes` : null;
|
||||
}
|
||||
|
||||
/** The generic Android link, handed to whichever map app is installed; one place too. */
|
||||
export function generateGeoUrl(places: NamedWaypoint[]): string | null {
|
||||
const first = places.find((p) => p.lat != null && p.lng != null);
|
||||
return first ? `geo:${point(first)}?q=${point(first)}(${label(first)})` : null;
|
||||
}
|
||||
|
||||
/** Every app the day can be opened in on this device, in the order they are offered. */
|
||||
export function dayRouteTargets(stops: NamedWaypoint[], profile: RouteProfileKey): DayRouteTarget[] {
|
||||
const out: DayRouteTarget[] = [];
|
||||
const push = (target: Omit<DayRouteTarget, 'url'>, url: string | null) => {
|
||||
if (url) out.push({ ...target, url });
|
||||
};
|
||||
push({ id: 'google', label: 'Google Maps' }, generateGoogleMapsUrl(stops));
|
||||
if (showsAppleMaps()) push({ id: 'apple', label: 'Apple Maps' }, generateAppleMapsUrl(stops, profile));
|
||||
push({ id: 'comaps', label: 'CoMaps' }, generateCoMapsUrl(stops, profile));
|
||||
const located = stops.filter((p) => p.lat != null && p.lng != null).length;
|
||||
push({ id: 'waze', label: 'Waze', firstStopOnly: located > 1 }, generateWazeUrl(stops));
|
||||
if (showsGeoUri()) {
|
||||
push(
|
||||
{ id: 'geo', label: 'geo:', labelKey: 'inspector.otherMapApp', firstStopOnly: located > 1 },
|
||||
generateGeoUrl(stops)
|
||||
);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useState, type MouseEvent } from 'react'
|
||||
import { ArrowRight, BedDouble, CalendarDays, CalendarRange, ChevronRight, Compass, LogIn, LogOut, MapPin, Pencil, PencilLine, Route, Ticket, TrainFront, Undo2 } from 'lucide-react'
|
||||
import { ArrowRight, BedDouble, CalendarDays, CalendarRange, ChevronRight, LogIn, LogOut, Map as MapIcon, MapPin, Pencil, PencilLine, Route, Ticket, TrainFront, Undo2 } from 'lucide-react'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import { useContextMenu, ContextMenu } from '../../../../components/shared/ContextMenu'
|
||||
import MarkdownText from '../../../../components/shared/MarkdownText'
|
||||
@@ -22,8 +22,9 @@ import type { MPlanTimelineProps } from '../MTripShell'
|
||||
import type { MergedItem } from '../../../../utils/dayMerge'
|
||||
import type { RouteSegment } from '../../../../types'
|
||||
import type { Assignment } from '../../../../types'
|
||||
import type { ComponentType, ReactNode } from 'react'
|
||||
import GoogleMapsIcon from '../../../../components/shared/GoogleMapsIcon'
|
||||
import type { ReactNode } from 'react'
|
||||
import { useSettingsStore } from '../../../../store/settingsStore'
|
||||
import MRouteAppsSheet, { openRouteTarget } from './MRouteAppsSheet'
|
||||
import MDayPager from '../../../components/MDayPager'
|
||||
|
||||
/**
|
||||
@@ -181,11 +182,23 @@ function PlanDayPage({ planner, shell, dayId: pageDayId, active, editing, legMen
|
||||
|
||||
const chrome = { editing, t, language: tl.language, timeFormat: tl.timeFormat }
|
||||
|
||||
// One Maps button for every app the day can be opened in: the preferred app
|
||||
// from settings (#2423) opens straight away, otherwise the sheet asks.
|
||||
const [mapsOpen, setMapsOpen] = useState(false)
|
||||
const preferredNavApp = useSettingsStore(s => s.settings.preferred_nav_app)
|
||||
const openMaps = () => {
|
||||
const preferred = preferredNavApp ? tl.routeTargets.find(target => target.id === preferredNavApp) : undefined
|
||||
if (preferred) openRouteTarget(preferred)
|
||||
else setMapsOpen(true)
|
||||
}
|
||||
|
||||
return (
|
||||
// One column under the day chips: the UP-NEXT card (go mode) or the edit
|
||||
// header, then the timeline card filling the rest, 12px apart whatever height
|
||||
// the card above it has. A day without an up-next starts the timeline right
|
||||
// under the chips.
|
||||
<>
|
||||
<MRouteAppsSheet open={mapsOpen} onClose={() => setMapsOpen(false)} targets={tl.routeTargets} t={t} />
|
||||
{/* One column under the day chips: the UP-NEXT card (go mode) or the edit
|
||||
header, then the timeline card filling the rest, 12px apart whatever height
|
||||
the card above it has. A day without an up-next starts the timeline right
|
||||
under the chips. */}
|
||||
<div className="absolute inset-x-4 flex flex-col gap-3 top-[calc(var(--m-safe-top,12px)+102px)] bottom-[calc(env(safe-area-inset-bottom,0px)+90px)]">
|
||||
{!editing && <UpNextCard tl={tl} t={t} onOpen={openPlace} />}
|
||||
{editing && <EditHeader tl={tl} planner={planner} shell={shell} />}
|
||||
@@ -308,7 +321,9 @@ function PlanDayPage({ planner, shell, dayId: pageDayId, active, editing, legMen
|
||||
)}
|
||||
|
||||
{editing && (
|
||||
<div className="mt-3 grid grid-cols-3 gap-2">
|
||||
// The day's actions as one group on one glass surface, the way iOS groups
|
||||
// its controls: each a round icon over its label.
|
||||
<div className="mt-3 grid grid-cols-3 gap-x-1 gap-y-2 rounded-[22px] border border-[color:var(--m-gbr)] bg-[color:var(--m-glass)] p-2 shadow-[0_8px_24px_-16px_rgba(0,0,0,.35)] backdrop-blur-[24px] backdrop-saturate-[1.7]">
|
||||
<PlanAction icon={MapPin} label={t('mobileTrip.addPlaceShort')} onClick={tl.addPlace} />
|
||||
<PlanAction
|
||||
icon={PencilLine}
|
||||
@@ -318,16 +333,14 @@ function PlanDayPage({ planner, shell, dayId: pageDayId, active, editing, legMen
|
||||
<PlanAction icon={Ticket} label={t('mobileTrip.addBookingShort')} onClick={tl.addBooking} />
|
||||
<PlanAction icon={TrainFront} label={t('mobileTrip.addTransportShort')} onClick={tl.addTransport} />
|
||||
<PlanAction icon={Route} label={t('dayplan.optimize')} onClick={() => void tl.optimize()} />
|
||||
{tl.canExportRoute && (
|
||||
<>
|
||||
<PlanAction icon={GoogleMapsIcon} label={t('mobileTrip.googleMaps')} onClick={tl.exportGoogleMaps} />
|
||||
<PlanAction icon={Compass} label={t('mobileTrip.coMaps')} onClick={tl.exportCoMaps} />
|
||||
</>
|
||||
{tl.canExportRoute && tl.routeTargets.length > 0 && (
|
||||
<PlanAction icon={MapIcon} label={t('mobileTrip.maps')} onClick={openMaps} />
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -347,7 +360,9 @@ function UpNextCard({ tl, t, onOpen }: {
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onOpen(upNext.assignment)}
|
||||
className="flex-none cursor-pointer rounded-[22px] border border-[color:var(--m-inbr)] bg-[color:var(--m-inner)] px-4 py-3.5 text-start shadow-[0_18px_44px_-18px_rgba(0,0,0,.3)] backdrop-blur-[28px] backdrop-saturate-[1.8]"
|
||||
// The same surface as the timeline card under it: the two are one plan, and
|
||||
// the place at the top plus the round arrow already say which stop is next.
|
||||
className="flex-none cursor-pointer rounded-[22px] border border-[color:var(--m-cbr)] bg-[color:var(--m-card)] px-4 py-3.5 text-start backdrop-blur-[24px] backdrop-saturate-[1.6] transition-transform active:scale-[.99]"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="whitespace-nowrap font-geist text-[0.65625rem] font-bold uppercase tracking-[.08em] text-m-muted">
|
||||
@@ -536,10 +551,10 @@ function HotelChipIcon({ variant }: { variant: 'checkout' | 'checkin' | 'stay' }
|
||||
return <BedDouble size={12} strokeWidth={2.2} className="text-m-muted" />
|
||||
}
|
||||
|
||||
/** Edit-mode action tile — Place · Note · Booking · Transport · Optimize · Google Maps, three per row. */
|
||||
/** One action of the edit-mode group: Place, Note, Booking, Transport, Optimize, Maps,
|
||||
* three per row, a round icon over its label. */
|
||||
function PlanAction({ icon: Icon, label, onClick }: {
|
||||
// Google Maps hands in its own brand mark, which is not a lucide icon (#2005).
|
||||
icon: LucideIcon | ComponentType<{ size?: number; className?: string }>
|
||||
icon: LucideIcon
|
||||
label: string
|
||||
onClick: () => void
|
||||
}) {
|
||||
@@ -547,10 +562,12 @@ function PlanAction({ icon: Icon, label, onClick }: {
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClick}
|
||||
className="flex min-w-0 flex-col items-center justify-center gap-1.5 rounded-[14px] border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] px-1 py-2.5 transition-transform active:scale-[.97]"
|
||||
className="group flex min-w-0 flex-col items-center justify-center gap-1.5 rounded-[16px] px-1 py-2 transition-transform active:scale-[.94]"
|
||||
>
|
||||
<Icon size={17} strokeWidth={2} className="text-m-muted" />
|
||||
<span className="max-w-full text-center font-geist text-[0.625rem] font-semibold leading-tight text-m-ink">{label}</span>
|
||||
<span className="flex h-11 w-11 items-center justify-center rounded-full bg-[color:var(--m-ic)] text-m-ink transition-colors group-active:bg-[color:var(--m-rowbr)]">
|
||||
<Icon size={19} strokeWidth={2} />
|
||||
</span>
|
||||
<span className="max-w-full text-center font-geist text-[0.6875rem] font-semibold leading-tight text-m-ink">{label}</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { ChevronRight, MapPinned } from 'lucide-react';
|
||||
import type { DayRouteTarget } from '../../../../components/Planner/dayRouteApps';
|
||||
import type { TranslationFn } from '../../../../types';
|
||||
import MSheet from '../../../components/MSheet';
|
||||
|
||||
/** Opens a target: a web link in a new tab (the app takes it over), geo: in place. */
|
||||
export function openRouteTarget(target: DayRouteTarget): void {
|
||||
if (target.url.startsWith('geo:')) window.location.assign(target.url);
|
||||
else window.open(target.url, '_blank', 'noopener,noreferrer');
|
||||
}
|
||||
|
||||
/**
|
||||
* The plan's one Maps button, asked: which app should get the day. A docked
|
||||
* sheet of the apps this device can open, each a full-width row; the ones that
|
||||
* take a single place say they only get the first stop.
|
||||
*/
|
||||
export default function MRouteAppsSheet({
|
||||
open,
|
||||
onClose,
|
||||
targets,
|
||||
t,
|
||||
}: {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
targets: DayRouteTarget[];
|
||||
t: TranslationFn;
|
||||
}) {
|
||||
return (
|
||||
<MSheet open={open} onClose={onClose} variant="bottom" ariaLabel={t('mobileTrip.openRouteIn')}>
|
||||
<div className="px-4 pb-4 pt-6">
|
||||
<div className="mb-3 flex items-center gap-2 px-1 text-[0.8125rem] font-bold text-m-ink">
|
||||
<MapPinned size={16} strokeWidth={2.2} className="text-m-muted" aria-hidden="true" />
|
||||
{t('mobileTrip.openRouteIn')}
|
||||
</div>
|
||||
<div className="flex flex-col gap-2">
|
||||
{targets.map((target) => (
|
||||
<button
|
||||
key={target.id}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
openRouteTarget(target);
|
||||
onClose();
|
||||
}}
|
||||
className="flex items-center gap-3 rounded-[18px] border border-[color:var(--m-gbr)] bg-[color:var(--m-glass)] px-4 py-3 text-start transition-transform active:scale-[.98]"
|
||||
>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-[0.9375rem] font-bold text-m-ink">
|
||||
{target.labelKey ? t(target.labelKey) : target.label}
|
||||
</span>
|
||||
{target.firstStopOnly && (
|
||||
<span className="mt-0.5 block font-geist text-[0.6875rem] text-m-muted">
|
||||
{t('mobileTrip.firstStopOnly')}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
<ChevronRight size={17} strokeWidth={2} className="flex-none text-m-faint" aria-hidden="true" />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</MSheet>
|
||||
);
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { dayWeatherAnchor } from '../../../../utils/dayWeather'
|
||||
import { metadataWithLegPositions, planMergedOrder, withTransportPositions } from '../../../../utils/mergedOrder'
|
||||
import { getDisplayTimeForDay, getMergedItems, getTransportForDay, hasCarrierEndpointOnDay, isCarrierTransport, stopDayIds, yieldTimedBookings } from '../../../../utils/dayMerge'
|
||||
import { dayCoMapsUrl, dayExportStops, dayGoogleMapsUrl, optimizeDayOrder, type DayCarrier } from '../../../../components/Planner/dayRoute'
|
||||
import { dayRouteTargets } from '../../../../components/Planner/dayRouteApps'
|
||||
import type { PlannedTransitLeg, TransitLeg } from '../../../../components/Planner/transitLeg'
|
||||
import { useTransitLegPlanner } from '../../../../components/Planner/useTransitLegPlanner'
|
||||
import { dayItemOverrun } from '../../../../components/Tours/tourDayTime'
|
||||
@@ -316,6 +317,17 @@ export function useMPlanTimeline(planner: TripPlanner, forDayId?: number | null)
|
||||
[day, days, dayAssignments, tripAccommodations, settings, dayCarrier],
|
||||
)
|
||||
|
||||
// The apps behind the plan's one Maps button, with the same bookended stops.
|
||||
const routeTargets = useMemo(
|
||||
() => (day
|
||||
? dayRouteTargets(
|
||||
dayExportStops(day, days, dayAssignments, tripAccommodations, settings.optimize_from_accommodation !== false, dayCarrier),
|
||||
day.default_transport_mode ?? routeProfile,
|
||||
)
|
||||
: []),
|
||||
[day, days, dayAssignments, tripAccommodations, settings, dayCarrier, routeProfile],
|
||||
)
|
||||
|
||||
const exportGoogleMaps = useCallback(() => {
|
||||
if (!day) return
|
||||
// Bookend the exported route with the day's accommodation the same way the
|
||||
@@ -401,7 +413,7 @@ export function useMPlanTimeline(planner: TripPlanner, forDayId?: number | null)
|
||||
moveRow, removeAssignment, editAssignment, editTransport, openTransitJourney,
|
||||
moveRowTo,
|
||||
addPlace, addBooking, addTransport,
|
||||
optimize, canExportRoute, exportGoogleMaps, exportCoMaps, renameDay, fullPlaceOf,
|
||||
optimize, canExportRoute, routeTargets, exportGoogleMaps, exportCoMaps, renameDay, fullPlaceOf,
|
||||
routeModeOptions, setLegMode, transitLegFor, planTransitLeg, journeyLate, tourOverrun,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,7 +401,7 @@ function UpNext({ planner, shell, rt, stageDayId, onOpen }: {
|
||||
const targets = place ? getNavigationTargets(place) : []
|
||||
|
||||
return (
|
||||
<section className="mt-2.5 rounded-[22px] border border-[color:var(--m-inbr)] bg-[color:var(--m-inner)] px-4 py-3.5 shadow-[0_18px_44px_-18px_rgba(0,0,0,.3)]">
|
||||
<section className="mt-2.5 rounded-[22px] border border-[color:var(--m-cbr)] bg-[color:var(--m-card)] px-4 py-3.5">
|
||||
<button type="button" onClick={() => onOpen(next.row)} className="w-full text-start">
|
||||
<span className="flex items-center justify-between gap-2">
|
||||
<span className="font-geist text-[0.65625rem] font-bold uppercase tracking-[.08em] text-m-muted">
|
||||
|
||||
Reference in New Issue
Block a user