Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

3.7 KB
Raw Blame
import L from 'leaflet';
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { Marker, Tooltip, useMap } from 'react-leaflet';
import { frameLng } from '../../utils/lngFrame';
import { onRealWorld } from '../../utils/worldCopy';
import { useLngFrame } from './lngFrameContext';
import { MAP_LABEL_BADGE_CLASS } from './mapHoverLabel';
import { plannerPinBadge, plannerPinShape, type PlannerPinPoint } from './plannerPinShape';
import { wirePlannerWaypointDrag } from './plannerWaypointDrag';

/** The pin's inline style for a size and selection; the renderer owns the colour. */
export type PinCss = (size: number, selected: boolean) => string;

function plannerWaypointIcon(
  pinCss: PinCss,
  point: PlannerPinPoint,
  index: number,
  selected: boolean,
  draggable: boolean
): L.DivIcon {
  const { size, text } = plannerPinShape(point, index, selected);
  return L.divIcon({
    className: 'tour-planner-waypoint-marker',
    iconSize: [size, size],
    iconAnchor: [size / 2, size / 2],
    html: `<span style="${pinCss(size, selected)};cursor:${draggable ? 'grab' : 'pointer'};touch-action:${draggable ? 'none' : 'auto'};user-select:none">${text}</span>${plannerPinBadge(point)}`,
  });
}

export default function PlannerWaypointMarker({
  point,
  index,
  selected,
  onClick,
  onMove,
  pinCss,
}: {
  point: { id: string; lat: number; lng: number; name?: string; detail?: string } & PlannerPinPoint;
  index: number;
  selected: boolean;
  onClick?: (id: string) => void;
  onMove?: (id: string, lat: number, lng: number) => boolean;
  pinCss: PinCss;
}) {
  const map = useMap();
  const [marker, setMarker] = useState<L.Marker | null>(null);
  const handlersRef = useRef({ onClick, onMove });
  useLayoutEffect(() => {
    handlersRef.current = { onClick, onMove };
  }, [onClick, onMove]);
  const frame = useLngFrame();
  const position = useMemo(
    () => [point.lat, frameLng(frame, point.lng)] as [number, number],
    [point.lat, point.lng, frame]
  );
  const { label, shaping, stageEnd } = point;
  const icon = useMemo(
    () => plannerWaypointIcon(pinCss, { label, shaping, stageEnd }, index, selected, !!onMove),
    [index, label, onMove, pinCss, selected, shaping, stageEnd]
  );
  const eventHandlers = useMemo(
    () => ({
      click: (event: { originalEvent: MouseEvent }) => {
        event.originalEvent.stopPropagation();
        handlersRef.current.onClick?.(point.id);
      },
    }),
    [point.id]
  );

  useEffect(() => {
    if (!marker || !onMove) return;
    const element = marker.getElement();
    if (!element) return;
    return wirePlannerWaypointDrag({
      element,
      initial: { lat: position[0], lng: position[1] },
      coordinateAt: (clientX, clientY) => {
        const rect = map.getContainer().getBoundingClientRect();
        const at = map.containerPointToLatLng([clientX - rect.left, clientY - rect.top]);
        return { lat: at.lat, lng: at.lng };
      },
      setPosition: ({ lat, lng }) => {
        marker.setLatLng([lat, lng]);
      },
      // Saved on the real world, whichever copy of it the pin was dragged on (#2976).
      onCommit: (at) => {
        const { lat, lng } = onRealWorld(at);
        return handlersRef.current.onMove?.(point.id, lat, lng) ?? false;
      },
    });
  }, [map, marker, onMove, point.id, position]);

  return (
    <Marker
      ref={setMarker}
      position={position}
      icon={icon}
      zIndexOffset={selected ? 1200 : 1100}
      eventHandlers={eventHandlers}
    >
      <Tooltip direction="top" offset={[0, -12]} opacity={1} className="map-tooltip">
        <span className="font-medium">{point.name ?? index + 1}</span>
        {point.detail && <span className={MAP_LABEL_BADGE_CLASS}>{point.detail}</span>}
      </Tooltip>
    </Marker>
  );
}