Files
TREK/client/src/components/Map/AssistantCameraController.tsx
T
Maurice b64da26651 feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect
their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or
OpenRouter key) or use the one the admin set up, including a custom
OpenAI-compatible server. It is an addon and only shows while a connection
is usable.

The chat runs on the existing MCP tools, pinned to the trip. Every change is
held as a change card the user applies or discards, and applied changes can
be undone. On the desktop it is a floating window that can be moved and
resized; on the phone it is fullscreen. Search results show as place cards
that can go onto a day, weather lookups as a strip, proposed places as
ghost markers on the map with a before/after switch, and an answer can be
shared into the trip chat. Booking files can be dropped in and are read
into change cards.
2026-10-22 21:30:12 +00:00

46 lines
1.7 KiB
TypeScript

import L from 'leaflet';
import { useCallback, useEffect } from 'react';
import { useMap } from 'react-leaflet';
import { MAX_ZOOM_RASTER, type ViewportPadding } from '../../utils/mapViewport';
import { ASSISTANT_CAMERA_MS, centreOf, coverPadding, useCameraRequest, useUserMoveClock } from './assistantCamera';
import type { AssistantCamera } from './mapViewContract';
interface AssistantCameraControllerProps {
camera: AssistantCamera | null | undefined;
/** The planner's padding for its panels, in pixels per edge. */
padding: ViewportPadding;
}
/**
* Moves the Leaflet map where the assistant asks: `fit` frames the points,
* `pan` brings their centre into view only when it is outside. Zoomed in no
* further than a day fit, the same scale the GL map stops at.
*/
export function AssistantCameraController({ camera, padding }: AssistantCameraControllerProps) {
const map = useMap();
const getContainer = useCallback(() => map.getContainer(), [map]);
const userMovedAt = useUserMoveClock(getContainer, true);
const request = useCameraRequest(camera, userMovedAt);
useEffect(() => {
request((next) => {
const duration = ASSISTANT_CAMERA_MS / 1000;
if (next.mode === 'pan') {
const centre = centreOf(next.points);
if (!map.getBounds().contains(centre)) map.panTo(centre, { animate: next.animate, duration });
return;
}
const box = coverPadding(padding, next.cover);
map.fitBounds(L.latLngBounds(next.points), {
paddingTopLeft: [box.left, box.top],
paddingBottomRight: [box.right, box.bottom],
maxZoom: MAX_ZOOM_RASTER,
animate: next.animate,
duration,
});
});
}, [map, padding, request]);
return null;
}