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

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

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

@ -1,28 +1,13 @@
import leaflet from 'leaflet'; 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 type { Marker } from '../../types';
import { getLayersForMarkers } from './getLayersForMarkers'; import { getLayersForMarkers } from './getLayersForMarkers';
export const useMarkersOnMap = ( export const useMarkersOnMap = (
mapRef: Readonly<Ref<leaflet.Map | undefined>>, mapRef: Readonly<Ref<leaflet.Map | undefined>>,
markers: Marker[], markers: Marker[],
) => ) => {
useEffect(() => { const layers = useMemo(() => getLayersForMarkers(markers), [markers]);
const map = mapRef.current; useLayers(mapRef, layers);
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]);

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

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

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

Loading…
Cancel
Save