import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import type { AssistantGhost, AssistantMapProps } from '../../components/Map/mapViewContract'; import { activityFocus, blockDays, changeEffects, dayPoints, ghostCandidates, liveGhosts, overlaySnapshot, windowCover, type GhostCandidate, type LatLng, } from '../../store/assistantMapModel'; import { chatOf, useAssistantStore } from '../../store/assistantStore'; import { useSettingsStore } from '../../store/settingsStore'; import { useTripStore } from '../../store/tripStore'; import type { AssignmentsMap, Category, Place } from '../../types'; import { prefersReducedMotion } from '../../utils/reducedMotion'; import { useAssistantRouteLines } from './useAssistantRouteLines'; /** Moves the map was not asked for come at most this often while the assistant works. */ const AUTO_CAMERA_GAP_MS = 2000; /** How long a place that just arrived from an applied suggestion pops. */ const POP_MS = 1000; const NO_IDS: readonly number[] = []; interface CompareViewOptions
{ tripId: number; /** The planner's view of the plan (hotel and hidden service stops left out). */ assignments: AssignmentsMap; /** The plan as the store holds it. */ storedAssignments: AssignmentsMap; places: P[]; } /** * "Before" on the map: while the user compares, the days the last applied * block touched show their plan from just before it ran, the places it made * are left off, and no pin can be dragged onto a day, since the plan shown is * not the one a drop would change. Otherwise everything passes through as is. */ export function useAssistantCompareView
({ tripId, assignments, storedAssignments, places, }: CompareViewOptions
): { assignments: AssignmentsMap; places: P[]; markersLocked: boolean } {
const compare = useAssistantStore((s) => chatOf(s, tripId).compare);
return useMemo(() => {
if (compare?.mode !== 'before') return { assignments, places, markersLocked: false };
const added = new Set(compare.addedPlaceIds);
return {
assignments: overlaySnapshot(assignments, storedAssignments, compare.snapshot),
places: added.size > 0 ? places.filter((p) => !added.has(p.id)) : places,
markersLocked: true,
};
}, [assignments, compare, places, storedAssignments]);
}
interface MapLinkOptions {
tripId: number;
isMobile: boolean;
/** The desktop map lock: a locked map never moves on its own. */
mapLocked: boolean;
/** The places on the plan map now, so a suggestion about one of them marks its pin instead of a ghost. */
mapPlaces: readonly { id: number }[];
assignments: AssignmentsMap;
markersLocked: boolean;
selectDay: (dayId: number) => void;
}
/**
* Everything the assistant shows on the trip map, as props for MapViewAuto:
* its waiting suggestions as ghosts tied to their rows in the chat, the
* routes its cards show (useAssistantRouteLines), the camera moves it asks
* for, and the pop of a place that arrives from an applied suggestion. A
* spot a card points at is a pin of its own, or the pulse of a trip place's
* own pin. Desktop and phone both get it through useTripPlanner.
*/
export function useAssistantMapLink(options: MapLinkOptions): AssistantMapProps {
const { tripId, isMobile, mapLocked, mapPlaces, assignments, markersLocked, selectDay } = options;
const places = useTripStore((s) => s.places);
const categories = useTripStore((s) => s.categories);
const candidates = useGhostCandidates(tripId);
const knownIds = useMemo(() => new Set(places.map((p) => p.id)), [places]);
const preview = useAssistantStore((s) => (s.preview?.tripId === tripId ? s.preview : null));
const linkId = useAssistantStore((s) => (s.mapLink?.tripId === tripId ? s.mapLink.hoveredId : null));
const setMapHover = useAssistantStore((s) => s.setMapHover);
const autoAllowed = useAutoCameraAllowed(isMobile, mapLocked);
const route = useAssistantRouteLines({ tripId, isMobile, autoAllowed });
const { hiddenGhosts, cardGhosts } = route;
// A change whose card draws its own line stands on the map as that line, not as a pin at its start.
const live = useMemo(
() => liveGhosts(candidates, knownIds).filter((c) => !hiddenGhosts.has(c.toolCallId)),
[candidates, hiddenGhosts, knownIds]
);
const onMapIds = useMemo(() => new Set(mapPlaces.map((p) => p.id)), [mapPlaces]);
const { ghosts, existing } = useMemo(
() => ghostPins(live, { places, categories, onMap: onMapIds }),
[categories, live, onMapIds, places]
);
const previewPlace = preview?.placeId !== undefined && onMapIds.has(preview.placeId) ? preview.placeId : null;
const previewId = preview && previewPlace === null ? `preview:${preview.ref}` : null;
const shownGhosts = useMemo(() => {
const all = cardGhosts.length > 0 ? [...ghosts, ...cardGhosts] : ghosts;
if (!preview || !previewId) return all;
return [...all, { id: previewId, lat: preview.lat, lng: preview.lng, name: preview.name }];
}, [cardGhosts, ghosts, preview, previewId]);
const onGhostHover = useCallback((id: string | null) => setMapHover(tripId, id, 'map'), [setMapHover, tripId]);
const onGhostClick = useCallback((id: string) => setMapHover(tripId, id, 'map'), [setMapHover, tripId]);
useAutoCamera({ tripId, live, ghosts, assignments, autoAllowed });
const assistantCamera = useCameraProp(tripId, isMobile);
const poppedPlaceIds = useArrivalPop(tripId, knownIds, selectDay, autoAllowed);
const pulse = previewPlace ?? (linkId ? (existing.get(linkId) ?? null) : null);
return useMemo(
() => ({
assistantGhosts: shownGhosts,
assistantHoveredId: previewId ?? linkId,
onAssistantGhostHover: onGhostHover,
onAssistantGhostClick: onGhostClick,
assistantPulsePlaceId: pulse,
assistantCamera,
poppedPlaceIds,
markersLocked,
assistantRouteLines: route.assistantRouteLines,
onAssistantRouteHover: route.onAssistantRouteHover,
onAssistantRoutePoint: route.onAssistantRoutePoint,
routeProfileFocus: route.routeProfileFocus,
}),
[
assistantCamera,
linkId,
markersLocked,
onGhostClick,
onGhostHover,
poppedPlaceIds,
previewId,
pulse,
route.assistantRouteLines,
route.onAssistantRouteHover,
route.onAssistantRoutePoint,
route.routeProfileFocus,
shownGhosts,
]
);
}
/**
* The thread's ghost candidates, re-read only when they change. The selector
* hands back a string so a streamed text delta, which rewrites the messages
* on every frame, does not render the whole planner each time.
*/
function useGhostCandidates(tripId: number): GhostCandidate[] {
const signature = useAssistantStore((s) => JSON.stringify(ghostCandidates(chatOf(s, tripId).messages)));
return useMemo(() => JSON.parse(signature) as GhostCandidate[], [signature]);
}
interface GhostPinSources {
places: readonly Place[];
categories: readonly Category[];
/** The places with a pin of their own on the map now. */
onMap: ReadonlySet