Merge pull request #10439 from tchak/improuve-react-types

chore(ts): improuve some types
This commit is contained in:
Paul Chavard 2024-05-22 13:49:45 +00:00 committed by GitHub
commit c405b3cf77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 55 additions and 29 deletions

View file

@ -87,7 +87,7 @@ export function CadastreLayer({
}); });
const onHighlight = useCallback( const onHighlight = useCallback(
({ detail }) => { ({ detail }: CustomEvent<{ cid: string; highlight: boolean }>) => {
highlightFeature(detail.cid, detail.highlight); highlightFeature(detail.cid, detail.highlight);
}, },
[highlightFeature] [highlightFeature]

View file

@ -1,7 +1,7 @@
import { useCallback, useRef, useEffect } from 'react'; import { useCallback, useRef, useEffect } from 'react';
import type { LngLatBoundsLike } from 'maplibre-gl'; import type { LngLatBoundsLike, LngLatLike } from 'maplibre-gl';
import DrawControl from '@mapbox/mapbox-gl-draw'; import DrawControl from '@mapbox/mapbox-gl-draw';
import type { FeatureCollection } from 'geojson'; import type { FeatureCollection, Feature, Point } from 'geojson';
import { useMapLibre } from '../../shared/maplibre/MapLibre'; import { useMapLibre } from '../../shared/maplibre/MapLibre';
import { import {
@ -78,15 +78,24 @@ export function DrawLayer({
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [map, enabled]); }, [map, enabled]);
const onSetId = useCallback(({ detail }) => { const onSetId = useCallback(
drawRef.current?.setFeatureProperty(detail.lid, 'id', detail.id); ({ detail }: CustomEvent<{ lid: string; id: string }>) => {
}, []); drawRef.current?.setFeatureProperty(detail.lid, 'id', detail.id);
const onAddFeature = useCallback(({ detail }) => { },
drawRef.current?.add(detail.feature); []
}, []); );
const onDeleteFature = useCallback(({ detail }) => { const onAddFeature = useCallback(
drawRef.current?.delete(detail.id); ({ detail }: CustomEvent<{ feature: Feature }>) => {
}, []); drawRef.current?.add(detail.feature);
},
[]
);
const onDeleteFature = useCallback(
({ detail }: CustomEvent<{ id: string }>) => {
drawRef.current?.delete(detail.id);
},
[]
);
useMapEvent('draw.create', createFeatures); useMapEvent('draw.create', createFeatures);
useMapEvent('draw.update', updateFeatures); useMapEvent('draw.update', updateFeatures);
@ -122,7 +131,7 @@ function useExternalEvents(
const flyTo = useFlyTo(); const flyTo = useFlyTo();
const onFeatureFocus = useCallback( const onFeatureFocus = useCallback(
({ detail }) => { ({ detail }: CustomEvent<{ id: string; bbox: LngLatBoundsLike }>) => {
const { id, bbox } = detail; const { id, bbox } = detail;
if (id) { if (id) {
const feature = findFeature(featureCollection, id); const feature = findFeature(featureCollection, id);
@ -137,16 +146,26 @@ function useExternalEvents(
); );
const onZoomFocus = useCallback( const onZoomFocus = useCallback(
({ detail }) => { ({
detail
}: CustomEvent<{
feature: Feature<Point>;
featureCollection: FeatureCollection;
}>) => {
if (detail.feature && detail.featureCollection == featureCollection) { if (detail.feature && detail.featureCollection == featureCollection) {
flyTo(17, detail.feature.geometry.coordinates); flyTo(17, detail.feature.geometry.coordinates as LngLatLike);
} }
}, },
[flyTo, featureCollection] [flyTo, featureCollection]
); );
const onFeatureCreate = useCallback( const onFeatureCreate = useCallback(
({ detail }) => { ({
detail
}: CustomEvent<{
feature: Feature;
featureCollection: FeatureCollection;
}>) => {
const { feature } = detail; const { feature } = detail;
const { geometry, properties } = feature; const { geometry, properties } = feature;
if ( if (
@ -164,7 +183,9 @@ function useExternalEvents(
); );
const onFeatureUpdate = useCallback( const onFeatureUpdate = useCallback(
({ detail }) => { ({
detail
}: CustomEvent<{ id: string; properties: Feature['properties'] }>) => {
const { id, properties } = detail; const { id, properties } = detail;
const feature = findFeature(featureCollection, id); const feature = findFeature(featureCollection, id);
@ -177,7 +198,7 @@ function useExternalEvents(
); );
const onFeatureDelete = useCallback( const onFeatureDelete = useCallback(
({ detail }) => { ({ detail }: CustomEvent<{ id: string }>) => {
const { id } = detail; const { id } = detail;
const feature = findFeature(featureCollection, id); const feature = findFeature(featureCollection, id);

View file

@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useMemo } from 'react'; import React, { useCallback, useEffect, useMemo } from 'react';
import { Popup, LngLatBoundsLike } from 'maplibre-gl'; import { Popup, LngLatBoundsLike, LngLatLike } from 'maplibre-gl';
import type { Feature, FeatureCollection } from 'geojson'; import type { Feature, FeatureCollection, Point } from 'geojson';
import { useMapLibre } from '../../shared/maplibre/MapLibre'; import { useMapLibre } from '../../shared/maplibre/MapLibre';
import { import {
@ -102,7 +102,7 @@ function useExternalEvents(featureCollection: FeatureCollection) {
const fitBounds = useFitBounds(); const fitBounds = useFitBounds();
const flyTo = useFlyTo(); const flyTo = useFlyTo();
const onFeatureFocus = useCallback( const onFeatureFocus = useCallback(
({ detail }) => { ({ detail }: CustomEvent<{ id: string }>) => {
const { id } = detail; const { id } = detail;
const feature = findFeature(featureCollection, id); const feature = findFeature(featureCollection, id);
if (feature) { if (feature) {
@ -112,10 +112,10 @@ function useExternalEvents(featureCollection: FeatureCollection) {
[featureCollection, fitBounds] [featureCollection, fitBounds]
); );
const onZoomFocus = useCallback( const onZoomFocus = useCallback(
({ detail }) => { ({ detail }: CustomEvent<{ feature: Feature<Point> }>) => {
const { feature } = detail; const { feature } = detail;
if (feature) { if (feature) {
flyTo(17, feature.geometry.coordinates); flyTo(17, feature.geometry.coordinates as LngLatLike);
} }
}, },
[flyTo] [flyTo]

View file

@ -9,7 +9,8 @@ import type {
LngLatBoundsLike, LngLatBoundsLike,
LngLat, LngLat,
MapLayerEventType, MapLayerEventType,
Style Style,
LngLatLike
} from 'maplibre-gl'; } from 'maplibre-gl';
import type { Feature, Geometry } from 'geojson'; import type { Feature, Geometry } from 'geojson';
@ -39,17 +40,20 @@ export function useFitBoundsNoFly() {
export function useFlyTo() { export function useFlyTo() {
const map = useMapLibre(); const map = useMapLibre();
return useCallback( return useCallback(
(zoom: number, center: [number, number]) => { (zoom: number, center: LngLatLike) => {
map.flyTo({ zoom, center }); map.flyTo({ zoom, center });
}, },
[map] [map]
); );
} }
export function useEvent(eventName: string, callback: EventListener) { export function useEvent<T>(
eventName: string,
callback: (event: CustomEvent<T>) => void
) {
return useEffect(() => { return useEffect(() => {
addEventListener(eventName, callback); addEventListener(eventName, callback as EventListener);
return () => removeEventListener(eventName, callback); return () => removeEventListener(eventName, callback as EventListener);
}, [eventName, callback]); }, [eventName, callback]);
} }

View file

@ -121,7 +121,8 @@
"graphql:doc:build": "RAILS_ENV=production bin/rake graphql:schema:idl && spectaql spectaql_config.yml", "graphql:doc:build": "RAILS_ENV=production bin/rake graphql:schema:idl && spectaql spectaql_config.yml",
"postinstall": "patch-package", "postinstall": "patch-package",
"test": "vitest", "test": "vitest",
"coverage": "vitest run --coverage" "coverage": "vitest run --coverage",
"up": "bunx npm-check-updates --root --format group -i"
}, },
"resolutions": { "resolutions": {
"string-width": "4.2.2", "string-width": "4.2.2",