1
- import { useCallback , useEffect , useMemo , useState } from "react" ;
2
- import { Box } from "@chakra-ui/react" ;
3
- import Map , { type MapRef , type MapLayerMouseEvent , Source , Layer } from "react-map-gl/maplibre" ;
4
- import { StacItem } from "stac-ts" ;
5
- import getBbox from "@turf/bbox" ;
6
- import { BackgroundTiles } from "../Map" ;
1
+ import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
2
+ import { Box } from '@chakra-ui/react' ;
3
+ import Map , {
4
+ type MapRef ,
5
+ type MapLayerMouseEvent ,
6
+ Source ,
7
+ Layer
8
+ } from 'react-map-gl/maplibre' ;
9
+ import { StacItem } from 'stac-ts' ;
10
+ import getBbox from '@turf/bbox' ;
11
+ import { BackgroundTiles } from '../Map' ;
7
12
8
13
type MapViewProps = {
9
14
results ?: {
10
- type : " FeatureCollection" ;
11
- features : StacItem [ ]
15
+ type : ' FeatureCollection' ;
16
+ features : StacItem [ ] ;
12
17
} ;
13
18
highlightItem ?: string ;
14
19
setHighlightItem : ( id ?: string ) => void ;
15
- id : string ;
16
- hidden : boolean ;
17
- }
20
+ id ? : string ;
21
+ hidden ? : boolean ;
22
+ } ;
18
23
19
24
const resultsOutline = {
20
- " line-color" : " #C53030" ,
21
- " line-width" : 1 ,
25
+ ' line-color' : ' #C53030' ,
26
+ ' line-width' : 1
22
27
} ;
23
28
24
29
const resultsFill = {
25
- " fill-color" : " #C53030" ,
26
- " fill-opacity" : 0.1
30
+ ' fill-color' : ' #C53030' ,
31
+ ' fill-opacity' : 0.1
27
32
} ;
28
33
29
34
const resultsHighlight = {
30
- " fill-color" : " #F6E05E" ,
31
- " fill-opacity" : .7
35
+ ' fill-color' : ' #F6E05E' ,
36
+ ' fill-opacity' : 0 .7
32
37
} ;
33
38
34
- function MapView ( { id, hidden, results, highlightItem, setHighlightItem } : MapViewProps ) {
35
- const [ map , setMap ] = useState < MapRef > ( ) ;
39
+ function MapView ( {
40
+ id,
41
+ hidden,
42
+ results,
43
+ highlightItem,
44
+ setHighlightItem
45
+ } : MapViewProps ) {
46
+ const [ map , setMap ] = useState < MapRef > ( ) ;
36
47
const setMapRef = ( m : MapRef ) => setMap ( m ) ;
37
- const highlightFilter = useMemo ( ( ) => [ "==" , [ "get" , "id" ] , highlightItem || "" ] , [ highlightItem ] ) ;
38
-
48
+ const highlightFilter = useMemo (
49
+ ( ) => [ '==' , [ 'get' , 'id' ] , highlightItem || '' ] ,
50
+ [ highlightItem ]
51
+ ) ;
39
52
40
53
// MapLibre doesn't preserve IDs so we're adding the ID
41
54
// to the properties so we can identify the items for user interactions.
@@ -51,6 +64,7 @@ function MapView({ id, hidden, results, highlightItem, setHighlightItem }: MapVi
51
64
if ( map && ! hidden ) {
52
65
map . resize ( ) ;
53
66
67
+ // @ts -expect-error results is a STACItem which is geojson compatible.
54
68
const bounds = results ?. features . length && getBbox ( results ) ;
55
69
if ( bounds ) {
56
70
const [ x1 , y1 , x2 , y2 ] = bounds ;
@@ -59,40 +73,43 @@ function MapView({ id, hidden, results, highlightItem, setHighlightItem }: MapVi
59
73
}
60
74
} , [ hidden , map , results ] ) ;
61
75
62
-
63
- const handleHover = useCallback ( ( e : MapLayerMouseEvent ) => {
64
- const interactiveItem = e . features && e . features [ 0 ] ;
65
- if ( interactiveItem ) {
66
- setHighlightItem ( interactiveItem . properties ?. id ) ;
67
- }
68
- } , [ setHighlightItem ] ) ;
76
+ const handleHover = useCallback (
77
+ ( e : MapLayerMouseEvent ) => {
78
+ const interactiveItem = e . features && e . features [ 0 ] ;
79
+ if ( interactiveItem ) {
80
+ setHighlightItem ( interactiveItem . properties ?. id ) ;
81
+ }
82
+ } ,
83
+ [ setHighlightItem ]
84
+ ) ;
69
85
70
86
return (
71
87
< Box
72
- h = " calc(100vh - 2.5rem)"
73
- maxH = " 650px"
74
- position = " sticky"
75
- top = "4"
76
- flexBasis = " 500px"
88
+ h = ' calc(100vh - 2.5rem)'
89
+ maxH = ' 650px'
90
+ position = ' sticky'
91
+ top = '4'
92
+ flexBasis = ' 500px'
77
93
id = { id }
78
94
hidden = { hidden }
79
95
>
80
96
< Map
81
97
ref = { setMapRef }
82
98
onMouseMove = { handleHover }
83
99
onMouseLeave = { ( ) => setHighlightItem ( ) }
84
- interactiveLayerIds = { [ " results-fill" ] }
100
+ interactiveLayerIds = { [ ' results-fill' ] }
85
101
>
86
102
< BackgroundTiles />
87
- { results && (
88
- < Source
89
- id = "results"
90
- type = "geojson"
91
- data = { resultsWithIDs }
92
- >
93
- < Layer id = "results-line" type = "line" paint = { resultsOutline } />
94
- < Layer id = "results-fill" type = "fill" paint = { resultsFill } />
95
- < Layer id = "results-hover" type = "fill" paint = { resultsHighlight } filter = { highlightFilter as any } />
103
+ { results && (
104
+ < Source id = 'results' type = 'geojson' data = { resultsWithIDs } >
105
+ < Layer id = 'results-line' type = 'line' paint = { resultsOutline } />
106
+ < Layer id = 'results-fill' type = 'fill' paint = { resultsFill } />
107
+ < Layer
108
+ id = 'results-hover'
109
+ type = 'fill'
110
+ paint = { resultsHighlight }
111
+ filter = { highlightFilter as any }
112
+ />
96
113
</ Source >
97
114
) }
98
115
</ Map >
0 commit comments