All files / src/hooks useNavigationEndpoints.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 13/13
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                    3x 3x       20x 15x     4x         67x         183x 183x 183x 183x 183x 183x   48x    
import { BuildingId } from "../data/buildings";
import { Coordinate } from "../type";
import { create } from "zustand";
 
export interface LabeledCoordinate extends Coordinate {
  /** Human-readable name resolved by the geocoding service */
  label: string;
  buildingId?: BuildingId;
}
interface NavigationEndpointsState {
  origin: LabeledCoordinate | null;
  destination: LabeledCoordinate | null;
  setOrigin: (endpoint: LabeledCoordinate | null) => void;
  setDestination: (endpoint: LabeledCoordinate | null) => void;
  swap: () => void;
  clear: () => void;
}
 
export const useNavigationEndpointsStore = create<NavigationEndpointsState>(
  (set) => ({
    origin: null,
    destination: null,
 
    setOrigin: (endpoint) => set({ origin: endpoint }),
    setDestination: (endpoint) => set({ destination: endpoint }),
 
    swap: () =>
      set((state) => ({
        origin: state.destination,
        destination: state.origin,
      })),
 
    clear: () => set({ origin: null, destination: null }),
  }),
);
 
export default function useNavigationEndpoints() {
  const origin = useNavigationEndpointsStore((s) => s.origin);
  const destination = useNavigationEndpointsStore((s) => s.destination);
  const setOrigin = useNavigationEndpointsStore((s) => s.setOrigin);
  const setDestination = useNavigationEndpointsStore((s) => s.setDestination);
  const swap = useNavigationEndpointsStore((s) => s.swap);
  const clear = useNavigationEndpointsStore((s) => s.clear);
 
  return { origin, setOrigin, destination, setDestination, swap, clear };
}