Skip to content

Commit

Permalink
fix: map hover and selected
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGOs92 committed Sep 18, 2024
1 parent e614bdd commit 206669c
Showing 1 changed file with 56 additions and 38 deletions.
94 changes: 56 additions & 38 deletions src/components/DeploiementBAL/DeploiementMap.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useCallback, useEffect, useState } from 'react'
import styled from 'styled-components'
import { MapMouseEvent, Popup, useMap } from 'react-map-gl/maplibre'
import { toolsColors } from '@/theme/theme'
Expand Down Expand Up @@ -172,76 +172,95 @@ interface DeploiementMapProps {

export default function DeploiementMap({ center, zoom, filteredCodesCommmune, selectedPaintLayer }: DeploiementMapProps) {
const { current: map } = useMap()
const router = useRouter()
const [highlighted, setHighlighted] = useState('')
const hoveredRef = React.useRef<string | null>(null)
const selectedRef = React.useRef<string | null>(null)
const [popup, setPopup] = useState<{ latitude: number, longitude: number, properties: any } | null>(null)

useEffect(() => {
if (!map) {
return
}

const onMouseMove = (event: MapMouseEvent) => {
if (!map) {
return
}

const canvas = map.getCanvas()
canvas.style.cursor = 'pointer'

const [feature] = (event as any).features
map.setCenter(center)
map.setZoom(zoom)
}, [map, center, zoom])

if (highlighted) {
map.setFeatureState({ source: 'data', id: highlighted }, { hover: false })
}
const closePopup = useCallback(() => {
setPopup(null)
if (selectedRef.current) {
map?.setFeatureState({ source: 'data', id: selectedRef.current }, { hover: false })
selectedRef.current = null
}
if (hoveredRef.current) {
map?.setFeatureState({ source: 'data', id: hoveredRef.current }, { hover: false })
hoveredRef.current = null
}
}, [map])

setHighlighted(feature.id)
map.setFeatureState({ source: 'data', id: highlighted }, { hover: true })
useEffect(() => {
if (!map) {
return
}

const onClick = (event: MapMouseEvent) => {
const [feature] = (event as any).features
if (filteredCodesCommmune.length === 0 || filteredCodesCommmune.includes(feature.properties.code)) {
closePopup()
setPopup({
latitude: event.lngLat.lat,
longitude: event.lngLat.lng,
properties: feature.properties,
})
}
else {
setPopup(null)
selectedRef.current = feature.id
map.setFeatureState({ source: 'data', id: feature.id }, { hover: true })
}
}

const onClick = (event: MapMouseEvent) => {
const [feature] = (event as any).features
router.push(`/commune/${feature.id}`)
map.on('click', 'bal-polygon-fill', onClick)

return () => {
map.off('click', 'bal-polygon-fill', onClick)
}
}, [map, filteredCodesCommmune, closePopup])

const onMouseLeave = () => {
useEffect(() => {
if (!map) {
return
}

const onMouseMove = (event: MapMouseEvent) => {
if (!map) {
return
}
const canvas = map.getCanvas()
canvas.style.cursor = ''

if (highlighted) {
map.setFeatureState({ source: 'data', id: highlighted }, { hover: false })
const [feature] = (event as any).features

if (hoveredRef.current && hoveredRef.current !== selectedRef.current) {
map.setFeatureState({ source: 'data', id: hoveredRef.current }, { hover: false })
}

setPopup(null)
hoveredRef.current = feature.id
map.setFeatureState({ source: 'data', id: feature.id }, { hover: true })
}

map.setCenter(center)
map.setZoom(zoom)
const onMouseLeave = () => {
if (!map) {
return
}

if (hoveredRef.current) {
map.setFeatureState({ source: 'data', id: hoveredRef.current }, { hover: false })
}
}

map.on('mousemove', 'bal-polygon-fill', onMouseMove)
map.on('mouseleave', 'bal-polygon-fill', onMouseLeave)
map.on('click', 'bal-polygon-fill', onClick)

return () => {
map.off('mousemove', 'bal-polygon-fill', onMouseMove)
map.off('mouseleave', 'bal-polygon-fill', onMouseLeave)
map.off('click', 'bal-polygon-fill', onClick)
}
}, [center, zoom, map, filteredCodesCommmune, router])
}, [map])

return (
<StyledWrapper>
Expand All @@ -263,14 +282,13 @@ export default function DeploiementMap({ center, zoom, filteredCodesCommmune, se
}
</div>
{popup && (
<Popup latitude={popup.latitude} longitude={popup.longitude} closeButton={false}>
<Popup latitude={popup.latitude} longitude={popup.longitude} onClose={closePopup}>
<div>
<p>
<b>{popup.properties.nom} ({popup.properties.code})</b>
</p>
<b>{popup.properties.nom} ({popup.properties.code})</b>
<div>Nombre d’adresses : {popup.properties.nbNumeros}</div>
<div>{`${popup.properties.certificationPercentage ? `Taux de certification : ${popup.properties.certificationPercentage}%` : 'Aucune adresse n’est certifiée par la commune'}`}</div>
{popup.properties.hasBAL ? <div>Déposé via : {popup.properties.nomClient}</div> : <div>Ne dispose pas d&apos;une BAL</div>}
<a href={`/commune/${popup.properties.code}`}>Voir la page commune</a>
</div>
</Popup>
)}
Expand Down

0 comments on commit 206669c

Please sign in to comment.