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' ;
712
813type MapViewProps = {
914 results ?: {
10- type : " FeatureCollection" ;
11- features : StacItem [ ]
15+ type : ' FeatureCollection' ;
16+ features : StacItem [ ] ;
1217 } ;
1318 highlightItem ?: string ;
1419 setHighlightItem : ( id ?: string ) => void ;
15- id : string ;
16- hidden : boolean ;
17- }
20+ id ? : string ;
21+ hidden ? : boolean ;
22+ } ;
1823
1924const resultsOutline = {
20- " line-color" : " #C53030" ,
21- " line-width" : 1 ,
25+ ' line-color' : ' #C53030' ,
26+ ' line-width' : 1
2227} ;
2328
2429const resultsFill = {
25- " fill-color" : " #C53030" ,
26- " fill-opacity" : 0.1
30+ ' fill-color' : ' #C53030' ,
31+ ' fill-opacity' : 0.1
2732} ;
2833
2934const resultsHighlight = {
30- " fill-color" : " #F6E05E" ,
31- " fill-opacity" : .7
35+ ' fill-color' : ' #F6E05E' ,
36+ ' fill-opacity' : 0 .7
3237} ;
3338
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 > ( ) ;
3647 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+ ) ;
3952
4053 // MapLibre doesn't preserve IDs so we're adding the ID
4154 // to the properties so we can identify the items for user interactions.
@@ -51,6 +64,7 @@ function MapView({ id, hidden, results, highlightItem, setHighlightItem }: MapVi
5164 if ( map && ! hidden ) {
5265 map . resize ( ) ;
5366
67+ // @ts -expect-error results is a STACItem which is geojson compatible.
5468 const bounds = results ?. features . length && getBbox ( results ) ;
5569 if ( bounds ) {
5670 const [ x1 , y1 , x2 , y2 ] = bounds ;
@@ -59,40 +73,43 @@ function MapView({ id, hidden, results, highlightItem, setHighlightItem }: MapVi
5973 }
6074 } , [ hidden , map , results ] ) ;
6175
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+ ) ;
6985
7086 return (
7187 < 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'
7793 id = { id }
7894 hidden = { hidden }
7995 >
8096 < Map
8197 ref = { setMapRef }
8298 onMouseMove = { handleHover }
8399 onMouseLeave = { ( ) => setHighlightItem ( ) }
84- interactiveLayerIds = { [ " results-fill" ] }
100+ interactiveLayerIds = { [ ' results-fill' ] }
85101 >
86102 < 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+ />
96113 </ Source >
97114 ) }
98115 </ Map >
0 commit comments