-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Unités carto] Régler l'affichage de l'Overlay (#2799)
- Loading branch information
Showing
22 changed files
with
316 additions
and
252 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
frontend/src/features/Station/components/HoveredStationOverlay/constants.ts
This file was deleted.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
frontend/src/features/Station/components/SelectedStationOverlay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { useForceUpdate } from '@mtes-mct/monitor-ui' | ||
import { Overlay } from 'ol' | ||
import { useCallback, useEffect, useMemo, useRef } from 'react' | ||
import styled from 'styled-components' | ||
|
||
import { StationCard } from './StationCard' | ||
import { getStationPointFeature } from './StationLayer/utils' | ||
import { useMainAppSelector } from '../../../hooks/useMainAppSelector' | ||
import { useMoveOverlayWhenDragging } from '../../../hooks/useMoveOverlayWhenDragging' | ||
import { getDialogOverlayOffsetFromFeature } from '../../../utils/getDialogOverlayOffsetFromFeature' | ||
import { monitorfishMap } from '../../map/monitorfishMap' | ||
import { FEATURE_MARGINS } from '../constants' | ||
import { useGetStationsQuery } from '../stationApi' | ||
|
||
import type { FeatureWithCodeAndEntityId } from '../../../libs/FeatureWithCodeAndEntityId' | ||
import type { Geometry } from 'ol/geom' | ||
|
||
export function SelectedStationOverlay() { | ||
const wrapperElementRef = useRef<HTMLDivElement | null>(null) | ||
const selectionDialogElementRef = useRef<HTMLDivElement | null>(null) | ||
const overlayRef = useRef<Overlay | undefined>(undefined) | ||
const currentOffsetRef = useRef([0, 0]) | ||
|
||
const isStationLayerDisplayed = useMainAppSelector(state => state.displayedComponent.isStationLayerDisplayed) | ||
const selectedStationId = useMainAppSelector(state => state.station.selectedStationId) | ||
const { data: stations } = useGetStationsQuery() | ||
const { forceUpdate } = useForceUpdate() | ||
|
||
const selectedStation = useMemo( | ||
() => (selectedStationId ? stations?.find(station => station.id === selectedStationId) : undefined), | ||
[stations, selectedStationId] | ||
) | ||
const selectedStationFeature = useMemo( | ||
() => (selectedStation ? getStationPointFeature(selectedStation) : undefined), | ||
[selectedStation] | ||
) | ||
|
||
const removeOverlay = useCallback(() => { | ||
if (!overlayRef.current) { | ||
return | ||
} | ||
|
||
monitorfishMap.removeOverlay(overlayRef.current) | ||
|
||
overlayRef.current = undefined | ||
}, []) | ||
|
||
const updateOverlay = useCallback( | ||
(feature: FeatureWithCodeAndEntityId<Geometry>, wrapperElement: HTMLDivElement) => { | ||
const nextOverlay = new Overlay({ | ||
className: 'ol-overlay-container overlay-active', | ||
element: wrapperElement | ||
}) | ||
nextOverlay.setProperties({ entityId: feature.entityId }) | ||
nextOverlay.setPosition(feature.getGeometry()?.getExtent()) | ||
nextOverlay.setOffset(currentOffsetRef.current) | ||
|
||
removeOverlay() | ||
|
||
overlayRef.current = nextOverlay | ||
|
||
monitorfishMap.addOverlay(nextOverlay) | ||
}, | ||
[removeOverlay] | ||
) | ||
|
||
useEffect(() => { | ||
if ( | ||
!wrapperElementRef.current || | ||
!selectionDialogElementRef.current || | ||
!selectedStationFeature || | ||
overlayRef.current?.getProperties().entityId === selectedStationFeature.entityId | ||
) { | ||
if (!selectedStationFeature) { | ||
removeOverlay() | ||
} | ||
|
||
return | ||
} | ||
|
||
updateOverlay(selectedStationFeature, wrapperElementRef.current) | ||
}, [removeOverlay, selectedStationFeature, updateOverlay]) | ||
|
||
useMoveOverlayWhenDragging( | ||
overlayRef.current, | ||
currentOffsetRef, | ||
() => {}, | ||
true, | ||
() => {} | ||
) | ||
|
||
useEffect(() => { | ||
if (selectedStationId !== undefined) { | ||
forceUpdate() | ||
} | ||
|
||
if ( | ||
overlayRef.current && | ||
selectedStationFeature && | ||
selectionDialogElementRef.current?.offsetWidth && | ||
selectionDialogElementRef.current?.offsetWidth > 0 | ||
) { | ||
currentOffsetRef.current = getDialogOverlayOffsetFromFeature( | ||
selectedStationFeature, | ||
selectionDialogElementRef.current, | ||
FEATURE_MARGINS | ||
) | ||
|
||
overlayRef.current.setOffset(currentOffsetRef.current) | ||
} | ||
}, [ | ||
forceUpdate, | ||
isStationLayerDisplayed, | ||
selectedStationFeature, | ||
selectedStationId, | ||
selectionDialogElementRef.current?.offsetWidth | ||
]) | ||
|
||
useEffect(() => () => removeOverlay(), [removeOverlay]) | ||
|
||
return ( | ||
<WrapperToBeKeptForDOMManagement $isVisible={!!selectionDialogElementRef.current} id="selected-station-overlay"> | ||
<div ref={wrapperElementRef}> | ||
{isStationLayerDisplayed && selectedStation && ( | ||
<StationCard ref={selectionDialogElementRef} isSelected station={selectedStation} /> | ||
)} | ||
</div> | ||
</WrapperToBeKeptForDOMManagement> | ||
) | ||
} | ||
|
||
const WrapperToBeKeptForDOMManagement = styled.div<{ | ||
$isVisible: boolean | ||
}>` | ||
left: 0; | ||
position: absolute; | ||
top: 0; | ||
visibility: ${p => (p.$isVisible ? 'visible' : 'hidden')}; | ||
z-index: 5000; | ||
` |
4 changes: 0 additions & 4 deletions
4
frontend/src/features/Station/components/SelectedStationOverlay/constants.ts
This file was deleted.
Oops, something went wrong.
84 changes: 0 additions & 84 deletions
84
frontend/src/features/Station/components/SelectedStationOverlay/index.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.