Skip to content

Commit

Permalink
updates map cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
agnlez committed Dec 12, 2023
1 parent 4284adf commit 21ca41d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 7 additions & 1 deletion frontend/src/components/map/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useControl } from 'react-map-gl';

import { MapboxOverlay, MapboxOverlayProps } from '@deck.gl/mapbox/typed';

import { CustomMapProps } from '@/components/map/types';

interface DeckMapboxOverlayContext {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
addLayer: (layer: any) => void;
Expand All @@ -30,12 +32,16 @@ function useMapboxOverlay(
return overlay;
}

export const DeckMapboxOverlayProvider = ({ children }: PropsWithChildren) => {
export const DeckMapboxOverlayProvider = ({
children,
cursor = 'auto',
}: PropsWithChildren<{ cursor?: CustomMapProps['cursor'] }>) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const layersRef = useRef<any[]>([]);

const OVERLAY = useMapboxOverlay({
interleaved: true,
getCursor: () => cursor,
});

const addLayer = useCallback(
Expand Down
20 changes: 17 additions & 3 deletions frontend/src/containers/map/content/map/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentProps, useCallback, useMemo, useRef } from 'react';
import { ComponentProps, useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { useMap } from 'react-map-gl';

Expand Down Expand Up @@ -40,6 +40,7 @@ const MainMap: React.FC = () => {
const { locationCode } = useParams();
const locationBbox = useAtomValue(bboxLocation);
const hoveredPolygonId = useRef<Parameters<typeof map.setFeatureState>[0] | null>(null);
const [cursor, setCursor] = useState<'grab' | 'crosshair' | 'pointer'>('grab');

const locationsQuery = useGetLocations(
{
Expand Down Expand Up @@ -106,6 +107,10 @@ const MainMap: React.FC = () => {
const handleMouseMove = useCallback(
(e: Parameters<ComponentProps<typeof Map>['onMouseOver']>[0]) => {
if (e.features.length > 0) {
if (!drawState.active) {
setCursor('pointer');
}

if (hoveredPolygonId.current !== null) {
map.setFeatureState(
{
Expand All @@ -126,9 +131,13 @@ const MainMap: React.FC = () => {
);

hoveredPolygonId.current = e.features[0];
} else {
if (!drawState.active) {
setCursor('grab');
}
}
},
[map, hoveredPolygonId]
[map, hoveredPolygonId, drawState.active]
);

const handleMouseLeave = useCallback(() => {
Expand Down Expand Up @@ -186,6 +195,10 @@ const MainMap: React.FC = () => {
};
}, [locationBbox, isSidebarOpen]);

useEffect(() => {
setCursor(drawState.active ? 'crosshair' : 'grab');
}, [drawState.active]);

return (
<div className="absolute left-0 h-full w-full border-r border-b border-black">
<Map
Expand All @@ -198,14 +211,15 @@ const MainMap: React.FC = () => {
onMouseLeave={handleMouseLeave}
renderWorldCopies={false}
attributionControl={false}
cursor={cursor}
>
<>
<Popup />
<LabelsManager />
<LayersToolbox />
<ZoomControls />
<DrawControls />
<LayerManager />
<LayerManager cursor={cursor} />
<Analysis />
<Attributions />
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Layer } from 'react-map-gl';

import { DeckMapboxOverlayProvider } from '@/components/map/provider';
import { CustomMapProps } from '@/components/map/types';
import LayerManagerItem from '@/containers/map/content/map/layer-manager/item';
import {
useSyncMapLayerSettings,
useSyncMapLayers,
} from '@/containers/map/content/map/sync-settings';

const LayerManager = () => {
const LayerManager = ({ cursor }: { cursor: CustomMapProps['cursor'] }) => {
const [activeLayers] = useSyncMapLayers();
const [layersSettings] = useSyncMapLayerSettings();

return (
<DeckMapboxOverlayProvider>
<DeckMapboxOverlayProvider cursor={cursor}>
<>
{/*
Generate all transparent backgrounds to be able to sort by layers without an error
Expand Down

0 comments on commit 21ca41d

Please sign in to comment.