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.

1.1 KB
Raw Blame
import { useCallback, useEffect } from 'react';
import { useTripStore } from '../store/tripStore';

interface Bounds {
  south: number;
  west: number;
  north: number;
  east: number;
}

/**
 * Hands the plan map's viewport to the trip store, where the place search reads
 * it as its first hint (#2620). Returns the callback for the map's
 * `onViewportChange`. While `shown` is false, or once the map is gone, the
 * viewport is cleared: a search opened away from the map has nothing visible
 * to lean on, and falls back to the day and the trip.
 */
export function usePlanMapViewport(tripId: number, shown = true): (bounds: Bounds) => void {
  const setPlanMapViewport = useTripStore((s) => s.setPlanMapViewport);

  useEffect(() => {
    if (!shown) setPlanMapViewport(null);
    return () => setPlanMapViewport(null);
  }, [shown, tripId, setPlanMapViewport]);

  return useCallback(
    (bounds: Bounds) => {
      if (!shown || !Number.isFinite(tripId)) return;
      const { south, west, north, east } = bounds;
      setPlanMapViewport({ tripId, south, west, north, east });
    },
    [shown, tripId, setPlanMapViewport]
  );
}