mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
4ca5728072
"Add tour" in the planner now offers "Import walk from Google Maps". The dialog takes the link of a route shared in Google Maps, either a maps.app.goo.gl short link or the long google.com/maps/dir link, or the share text of the Google Maps app with the one link in it. It shows the stops in their order with their names, marks a stop the link gives no position as such, says that the route comes from Google Maps with a link to open it there, and that TREK plans the way between the stops itself and does not take the line drawn in Google Maps over. Nothing is written until "Load as walk": that opens a new, unsaved city walk in the planner with the positioned stops and their names, routed on foot by TREK, and warns first when it replaces unsaved work. A stop without a position is left out and never looked up or guessed; with fewer than two positions nothing can be loaded. While the walk is not saved, a calm notice in the planner says where it came from, also after a reload of the draft. Cancelling or a failure leaves every tour as it was. Only a route the link marks unambiguously as on foot is read. A car, bike or transit route, a link that does not say, or one that says two things is refused with a reason the dialog gives in the reader's language. Each stop takes its position from its own block of the link's data, so a line dragged through extra points or a stop given as coordinates no longer leaves the stops without positions or shifts them. The road trip and places imports read links as before. A short link is followed by a resolver of its own: every hop is checked against the Google Maps short link shapes and the strict SSRF guard before it is looked up or asked, so ALLOW_INTERNAL_NETWORK and ALLOW_LINK_LOCAL_IPS do not open it. Each request goes to the vetted addresses only, follows nothing by itself, reads no body and carries no credentials; the chain has one deadline including name lookups, at most three hops and a cap on the Location header, and the long link it ends on must be Google Maps and is never fetched. The preview is POST /api/trips/:tripId/tours/import/google-maps/preview for members who may edit places, with a 502 when Google could not be reached, and the MCP tool preview_google_maps_walk reads the same.
252 lines
8.4 KiB
TypeScript
252 lines
8.4 KiB
TypeScript
import type { WalkImportPreviewResponse, WalkImportStation } from '@trek/shared';
|
|
import { AlertTriangle, ExternalLink, Footprints, Link2, Loader2, MapPinOff, Route } from 'lucide-react';
|
|
import { useId, type FormEvent } from 'react';
|
|
import { useTranslation } from '../../../i18n';
|
|
import { EditorField, INPUT } from '../../shared/dialogParts';
|
|
import {
|
|
DialogButton,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogShell,
|
|
DialogTile,
|
|
FooterSpacer,
|
|
fs,
|
|
NEUTRAL_TINT,
|
|
} from '../../shared/DialogShell';
|
|
import type { TourPlannerController } from '../planner/useTourPlanner';
|
|
import { TourNotice } from '../tourParts';
|
|
import type { WalkImport } from './useWalkImport';
|
|
|
|
/** One stop of the link, in its place in the order; a stop without a position says so instead of a position. */
|
|
function WalkImportStationRow({ station, number }: { station: WalkImportStation; number: number }) {
|
|
const { t } = useTranslation();
|
|
const position =
|
|
station.lat !== null && station.lng !== null ? `${station.lat.toFixed(5)}, ${station.lng.toFixed(5)}` : null;
|
|
const placed = position !== null;
|
|
return (
|
|
<li
|
|
data-testid="walk-import-station"
|
|
data-unresolved={placed ? undefined : 'true'}
|
|
className={`flex items-start gap-2.5 rounded-[10px] px-2.5 py-2 ${placed ? 'bg-surface-card' : 'border border-dashed border-edge'}`}
|
|
>
|
|
<span
|
|
className="mt-px grid h-5 w-5 flex-none place-items-center rounded-full bg-surface-secondary font-geist font-semibold text-content-muted"
|
|
style={fs(10.5)}
|
|
>
|
|
{number}
|
|
</span>
|
|
<span className="flex min-w-0 flex-1 flex-col gap-0.5">
|
|
<span
|
|
className={`break-words font-medium leading-snug ${placed ? 'text-content' : 'text-content-muted'}`}
|
|
style={fs(12.5, 'body')}
|
|
>
|
|
{station.name ?? t('tours.walk.stationNumbered', { number })}
|
|
</span>
|
|
{placed ? (
|
|
<span className="font-geist text-content-faint" style={fs(11)}>
|
|
{position}
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1 text-warning" style={fs(11, 'body')}>
|
|
<MapPinOff size={11} strokeWidth={2.2} className="flex-none" />
|
|
{t('tours.walkImport.noPosition')}
|
|
</span>
|
|
)}
|
|
</span>
|
|
</li>
|
|
);
|
|
}
|
|
|
|
/** The step that asks for the link. */
|
|
function WalkImportLinkStep({ walkImport }: { walkImport: WalkImport }) {
|
|
const { t } = useTranslation();
|
|
const inputId = useId();
|
|
return (
|
|
<EditorField
|
|
label={t('tours.walkImport.urlLabel')}
|
|
htmlFor={inputId}
|
|
hint={t('tours.walkImport.urlHint')}
|
|
error={walkImport.failure}
|
|
>
|
|
<input
|
|
id={inputId}
|
|
type="text"
|
|
inputMode="url"
|
|
autoComplete="off"
|
|
spellCheck={false}
|
|
className={INPUT}
|
|
value={walkImport.url}
|
|
placeholder="https://maps.app.goo.gl/…"
|
|
disabled={walkImport.phase === 'reading'}
|
|
onChange={(event) => walkImport.setUrl(event.target.value)}
|
|
data-testid="walk-import-url"
|
|
/>
|
|
</EditorField>
|
|
);
|
|
}
|
|
|
|
/** The step that shows what the link holds, before anything is loaded. */
|
|
function WalkImportPreviewStep({
|
|
preview,
|
|
unplaced,
|
|
canLoad,
|
|
hasUnsavedChanges,
|
|
}: {
|
|
preview: WalkImportPreviewResponse;
|
|
unplaced: number;
|
|
canLoad: boolean;
|
|
hasUnsavedChanges: boolean;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<div className="flex flex-col gap-4" data-testid="walk-import-preview">
|
|
<p className="m-0 flex flex-wrap items-center gap-x-2 gap-y-1 text-content-muted" style={fs(12, 'body')}>
|
|
<Footprints size={13} strokeWidth={2} className="flex-none" />
|
|
<span>{t('tours.walkImport.source')}</span>
|
|
<a
|
|
href={preview.resolved_url}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className="inline-flex items-center gap-1 font-medium text-accent hover:underline"
|
|
data-testid="walk-import-source"
|
|
>
|
|
{t('tours.walkImport.openSource')}
|
|
<ExternalLink size={12} strokeWidth={2} className="flex-none" />
|
|
</a>
|
|
</p>
|
|
<TourNotice icon={<Route size={13} strokeWidth={2} />}>{t('tours.walkImport.recalculated')}</TourNotice>
|
|
<DialogSection label={t('tours.walkImport.stops')}>
|
|
<ol className="m-0 flex list-none flex-col gap-1 p-0">
|
|
{preview.stations.map((station, index) => (
|
|
<WalkImportStationRow key={index} station={station} number={index + 1} />
|
|
))}
|
|
</ol>
|
|
</DialogSection>
|
|
{unplaced > 0 && (
|
|
<TourNotice tone="warning" icon={<MapPinOff size={13} strokeWidth={2} />}>
|
|
{t('tours.walkImport.unresolved', { count: unplaced })}
|
|
</TourNotice>
|
|
)}
|
|
{!canLoad && <TourNotice tone="warning">{t('tours.walkImport.tooFewPositions')}</TourNotice>}
|
|
{hasUnsavedChanges && (
|
|
<TourNotice tone="warning" icon={<AlertTriangle size={13} strokeWidth={2} />}>
|
|
{t('tours.walkImport.replacesDraft')}
|
|
</TourNotice>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** The buttons of each step: read the link, or go back and load the walk. */
|
|
function WalkImportFooter({ walkImport }: { walkImport: WalkImport }) {
|
|
const { t } = useTranslation();
|
|
const reading = walkImport.phase === 'reading';
|
|
return (
|
|
<DialogFooter>
|
|
<FooterSpacer />
|
|
<DialogButton onClick={walkImport.close}>{t('common.cancel')}</DialogButton>
|
|
{walkImport.preview ? (
|
|
<>
|
|
<DialogButton onClick={walkImport.back}>{t('tours.walkImport.back')}</DialogButton>
|
|
<DialogButton
|
|
variant="primary"
|
|
icon={<Footprints size={14} strokeWidth={2} />}
|
|
disabled={!walkImport.canLoad}
|
|
onClick={walkImport.load}
|
|
data-testid="walk-import-load"
|
|
>
|
|
{t('tours.walkImport.load')}
|
|
</DialogButton>
|
|
</>
|
|
) : (
|
|
<DialogButton
|
|
variant="primary"
|
|
icon={
|
|
reading ? (
|
|
<Loader2 size={14} strokeWidth={2} className="animate-spin" />
|
|
) : (
|
|
<Link2 size={14} strokeWidth={2} />
|
|
)
|
|
}
|
|
disabled={!walkImport.canRead}
|
|
onClick={() => void walkImport.read()}
|
|
data-testid="walk-import-read"
|
|
>
|
|
{t('tours.walkImport.read')}
|
|
</DialogButton>
|
|
)}
|
|
</DialogFooter>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* CW-07: a city walk from a Google Maps walking route, opened from "Add tour".
|
|
* First the link, then its stops in their order with the ones the link gives
|
|
* no position marked, where the route came from, and that TREK plans the way
|
|
* between them itself. Only the load button hands the walk to the planner.
|
|
*/
|
|
export function WalkImportDialog({
|
|
walkImport,
|
|
hasUnsavedChanges,
|
|
}: {
|
|
walkImport: WalkImport;
|
|
hasUnsavedChanges: boolean;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const titleId = useId();
|
|
if (!walkImport.open) return null;
|
|
const submit = (event: FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault();
|
|
if (!walkImport.preview && walkImport.canRead) void walkImport.read();
|
|
};
|
|
const head = (
|
|
<DialogHeader
|
|
tile={
|
|
<DialogTile>
|
|
<Footprints size={20} strokeWidth={2} className="text-content" />
|
|
</DialogTile>
|
|
}
|
|
tint={NEUTRAL_TINT}
|
|
labelId={titleId}
|
|
onClose={walkImport.close}
|
|
title={t('tours.walkImport.title')}
|
|
sub={t('tours.walkImport.intro')}
|
|
subWraps
|
|
/>
|
|
);
|
|
return (
|
|
<DialogShell
|
|
onClose={walkImport.close}
|
|
labelledBy={titleId}
|
|
width="narrow"
|
|
align="top"
|
|
onSubmit={submit}
|
|
header={head}
|
|
footer={<WalkImportFooter walkImport={walkImport} />}
|
|
>
|
|
{walkImport.preview ? (
|
|
<WalkImportPreviewStep
|
|
preview={walkImport.preview}
|
|
unplaced={walkImport.unplaced}
|
|
canLoad={walkImport.canLoad}
|
|
hasUnsavedChanges={hasUnsavedChanges}
|
|
/>
|
|
) : (
|
|
<WalkImportLinkStep walkImport={walkImport} />
|
|
)}
|
|
</DialogShell>
|
|
);
|
|
}
|
|
|
|
/** CW-07: said in the planner while a walk read from Google Maps is not saved yet. */
|
|
export function WalkImportDraftNotice({ planner }: { planner: Pick<TourPlannerController, 'draftSource' | 'mode'> }) {
|
|
const { t } = useTranslation();
|
|
if (planner.draftSource !== 'google_maps' || planner.mode.type !== 'new-draft') return null;
|
|
return (
|
|
<TourNotice role="status" icon={<Footprints size={13} strokeWidth={2} />}>
|
|
{t('tours.walkImport.draftNotice')}
|
|
</TourNotice>
|
|
);
|
|
}
|