reorganized custom hooks

main
Inga 🏳‍🌈 1 year ago
parent 47b2aeb48b
commit ed1f4bc47c
  1. 24
      src/hooks/leaflet/useLayers.ts
  2. 0
      src/hooks/leaflet/useMap.ts
  3. 2
      src/hooks/leaflet/useMapClick.ts
  4. 5
      src/hooks/usePrompt.ts
  5. 3
      src/hooks/useSortable.ts
  6. 27
      src/routePlanner/hooks/useMarkersOnMap/index.ts
  7. 4
      src/routePlanner/map.tsx
  8. 2
      src/routePlanner/marker.tsx
  9. 2
      src/routePlanner/markers.tsx

@ -0,0 +1,24 @@
import leaflet from 'leaflet';
import { type Ref, useEffect } from 'preact/hooks';
export const useLayers = (
mapRef: Readonly<Ref<leaflet.Map | undefined>>,
layers: leaflet.Layer[],
) =>
useEffect(() => {
const map = mapRef.current;
if (!map) {
return;
}
for (const layer of layers) {
layer.addTo(map);
}
return () => {
for (const layer of layers) {
layer.removeFrom(map);
layer.remove();
}
};
}, [mapRef, layers]);

@ -1,6 +1,6 @@
import leaflet from 'leaflet';
import { type Ref, useEffect } from 'preact/hooks';
import type { Coordinates } from '../../../shared/types';
import type { Coordinates } from '../../shared/types';
export const useMapClick = (
mapRef: Readonly<Ref<leaflet.Map | undefined>>,

@ -4,11 +4,10 @@ export const usePrompt = (
message: string,
defaultValue: string,
onChange: (newValue: string) => void,
) => {
return useCallback(() => {
) =>
useCallback(() => {
const newValue = prompt(message, defaultValue);
if (newValue?.length) {
onChange(newValue);
}
}, [message, defaultValue, onChange]);
};

@ -1,10 +1,9 @@
import { type Ref, useEffect } from 'preact/hooks';
import Sortable from 'sortablejs';
import type { ReorderListParams } from '../../types';
export const useSortable = (
listContainerRef: Readonly<Ref<HTMLElement>>,
onListReorder: (params: ReorderListParams) => void,
onListReorder: (params: { oldIndex: number; newIndex: number }) => void,
) =>
useEffect(() => {
if (!listContainerRef.current) {

@ -1,28 +1,13 @@
import leaflet from 'leaflet';
import { type Ref, useEffect } from 'preact/hooks';
import { type Ref, useMemo } from 'preact/hooks';
import { useLayers } from '../../../hooks/leaflet/useLayers';
import type { Marker } from '../../types';
import { getLayersForMarkers } from './getLayersForMarkers';
export const useMarkersOnMap = (
mapRef: Readonly<Ref<leaflet.Map | undefined>>,
markers: Marker[],
) =>
useEffect(() => {
const map = mapRef.current;
if (!map) {
return;
}
const layers = getLayersForMarkers(markers);
for (const layer of layers) {
layer.addTo(map);
}
return () => {
for (const layer of layers) {
layer.removeFrom(map);
layer.remove();
}
};
}, [mapRef, markers]);
) => {
const layers = useMemo(() => getLayersForMarkers(markers), [markers]);
useLayers(mapRef, layers);
};

@ -1,6 +1,6 @@
import { useRef } from 'preact/hooks';
import { useMap } from './hooks/useMap';
import { useMapClick } from './hooks/useMapClick';
import { useMap } from '../hooks/leaflet/useMap';
import { useMapClick } from '../hooks/leaflet/useMapClick';
import { useMarkersOnMap } from './hooks/useMarkersOnMap';
import type { MapProps } from './types';

@ -1,4 +1,4 @@
import { usePrompt } from './hooks/usePrompt';
import { usePrompt } from '../hooks/usePrompt';
import type { MarkerProps } from './types';
export const MarkerComponent = ({ marker, isFirst, isLast }: MarkerProps) => {

@ -1,5 +1,5 @@
import { useRef } from 'preact/hooks';
import { useSortable } from './hooks/useSortable';
import { useSortable } from '../hooks/useSortable';
import { MarkerComponent } from './marker';
import type { MarkersProps } from './types';

Loading…
Cancel
Save