Files
TREK/client/src/components/Map/AssistantGhostLayer.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

59 lines
2.2 KiB
TypeScript

import L from 'leaflet';
import { useEffect, useLayoutEffect, useRef } from 'react';
import { useMap } from 'react-leaflet';
import { GHOST_SIZE, mountGhosts, setGhostHot, type GhostHandlers } from './assistantGhostMarker';
import type { AssistantGhost } from './mapViewContract';
interface AssistantGhostLayerProps {
ghosts: readonly AssistantGhost[] | undefined;
hoveredId: string | null | undefined;
onHover?: (id: string | null) => void;
onClick?: (id: string) => void;
}
/** Above the place pins, so a suggestion on a busy day is never buried under the stops it joins. */
const GHOST_Z_OFFSET = 900;
/**
* The assistant's suggestions on the Leaflet map, outside the cluster group:
* a suggestion stands on its own until it is decided. The name shows in the
* map's own tooltip, built from text. A hover only toggles a class on the
* element; the pins are rebuilt when the suggestions change, never on hover.
*/
export function AssistantGhostLayer({ ghosts, hoveredId, onHover, onClick }: AssistantGhostLayerProps) {
const map = useMap();
const handlers = useRef<GhostHandlers>({ onHover, onClick });
useLayoutEffect(() => {
handlers.current = { onHover, onClick };
}, [onHover, onClick]);
const elements = useRef<ReadonlyMap<string, HTMLElement>>(new Map());
useEffect(() => {
if (!ghosts?.length) return;
const mounted = mountGhosts(
ghosts,
(el, ghost) => {
const icon = L.divIcon({ html: el, className: 'trek-ghost-pin', iconSize: [GHOST_SIZE, GHOST_SIZE] });
const label = document.createElement('span');
label.textContent = ghost.name;
const marker = L.marker([ghost.lat, ghost.lng], { icon, keyboard: false, zIndexOffset: GHOST_Z_OFFSET })
.bindTooltip(label, { direction: 'top', offset: [0, -GHOST_SIZE / 2], className: 'map-tooltip' })
.addTo(map);
return { remove: () => marker.remove() };
},
handlers
);
elements.current = mounted.elements;
return () => {
mounted.dispose();
elements.current = new Map();
};
}, [ghosts, map]);
useEffect(() => {
setGhostHot(elements.current, hoveredId);
}, [ghosts, hoveredId]);
return null;
}