generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable mouse interaction with crags/crag-groups on map (#1080)
* refactor: use precomputed polygons on area maps * feat: add active marker and info panel
- Loading branch information
Showing
13 changed files
with
208 additions
and
167 deletions.
There are no files selected for viewing
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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Marker } from 'react-map-gl' | ||
import { Point } from '@turf/helpers' | ||
import { MapPin } from '@phosphor-icons/react/dist/ssr' | ||
|
||
export const AreaActiveMarker: React.FC<{ point: Point }> = ({ point }) => { | ||
const { coordinates } = point | ||
return ( | ||
<Marker longitude={coordinates[0]} latitude={coordinates[1]} style={{ zIndex: 2000 }}> | ||
<MapPin size={36} weight='fill' className='text-accent' /> | ||
</Marker> | ||
) | ||
} |
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,45 @@ | ||
import * as Popover from '@radix-ui/react-popover' | ||
import { MapAreaFeatureProperties } from './AreaMap' | ||
import { getAreaPageFriendlyUrl } from '@/js/utils' | ||
import { Card } from '../core/Card' | ||
import { EntityIcon } from '@/app/editArea/[slug]/general/components/AreaItem' | ||
|
||
/** | ||
* Area info panel | ||
*/ | ||
export const AreaInfoDrawer: React.FC<{ data: MapAreaFeatureProperties | null, onClose?: () => void }> = ({ data, onClose }) => { | ||
const parent = data?.parent == null ? null : JSON.parse(data.parent) | ||
const parentName = parent?.name ?? 'Unknown' | ||
const parentId = parent?.id ?? null | ||
return ( | ||
<Popover.Root open={data != null}> | ||
<Popover.Anchor className='absolute top-3 left-3 z-50' /> | ||
<Popover.Content align='start'> | ||
{data != null && <Content {...data} parentName={parentName} parentId={parentId} />} | ||
</Popover.Content> | ||
</Popover.Root> | ||
) | ||
} | ||
|
||
export const Content: React.FC<MapAreaFeatureProperties & { parentName: string, parentId: string | null }> = ({ id, name, parentName, parentId }) => { | ||
const url = parentId == null | ||
? parentName | ||
: ( | ||
<a | ||
href={getAreaPageFriendlyUrl(parentId, name)} | ||
className='inline-flex items-center gap-1.5' | ||
> | ||
<EntityIcon type='area' size={16} /><span className='text-secondary font-medium hover:underline '>{parentName}</span> | ||
</a> | ||
) | ||
return ( | ||
<Card> | ||
<div className='flex flex-col gap-y-1 text-xs'> | ||
<div>{url}</div> | ||
<div className='ml-2'> | ||
<span className='text-secondary'>∟</span><a href={getAreaPageFriendlyUrl(id, name)} className='text-sm font-medium hover:underline'>{name}</a> | ||
</div> | ||
</div> | ||
</Card> | ||
) | ||
} |
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,112 @@ | ||
'use client' | ||
import { useEffect, useRef, useState } from 'react' | ||
import { Map, ScaleControl, FullscreenControl, NavigationControl, Source, Layer, MapLayerMouseEvent, LineLayer } from 'react-map-gl' | ||
import dynamic from 'next/dynamic' | ||
import { lineString, Point, point } from '@turf/helpers' | ||
import lineToPolygon from '@turf/line-to-polygon' | ||
import { useDebouncedCallback } from 'use-debounce' | ||
|
||
import { AreaMetadataType, AreaType } from '../../js/types' | ||
import { MAP_STYLES } from './BaseMap' | ||
import { AreaInfoDrawer } from './AreaInfoDrawer' | ||
import { AreaActiveMarker } from './AreaActiveMarker' | ||
|
||
type ChildArea = Pick<AreaType, 'uuid' | 'areaName'> & { metadata: Pick<AreaMetadataType, 'lat' | 'lng' | 'leaf' | 'bbox' | 'polygon'> } | ||
interface AreaMapProps { | ||
subAreas: ChildArea[] | ||
area: AreaType | ||
focused: string | null | ||
selected: string | null | ||
} | ||
|
||
export interface MapAreaFeatureProperties { | ||
id: string | ||
name: string | ||
parent: string // due to a backend backend bug, this is a string instead of a parent object | ||
// parent: { | ||
// id: string | ||
// name: string | ||
// } | ||
} | ||
|
||
/** | ||
* Area map | ||
*/ | ||
const AreaMap: React.FC<AreaMapProps> = ({ area, subAreas }) => { | ||
const [hovered, setHovered] = useState<MapAreaFeatureProperties | null>(null) | ||
const [selected, setSelected] = useState<Point | null>(null) | ||
const mapRef = useRef(null) | ||
let fitBoundOpts: any = { padding: { top: 45, left: 45, bottom: 45, right: 45 } } | ||
if (subAreas.length === 0) { | ||
fitBoundOpts = { maxZoom: 14 } | ||
} | ||
|
||
const { metadata } = area | ||
const boundary = metadata?.polygon == null ? null : lineToPolygon(lineString(metadata.polygon), { properties: { name: area.areaName } }) | ||
|
||
const onClick = (event: MapLayerMouseEvent): void => { | ||
const feature = event?.features?.[0] | ||
if (feature == null) { | ||
setSelected(null) | ||
setHovered(null) | ||
} else { | ||
setSelected(feature.geometry as unknown as Point) | ||
setHovered(feature.properties as MapAreaFeatureProperties) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
/** | ||
* Show drop pin if viewing a leaf area | ||
*/ | ||
if (metadata.leaf) { | ||
setSelected(point([metadata.lng, metadata.lat]).geometry as unknown as Point) | ||
} | ||
}, [metadata.leaf]) | ||
return ( | ||
<div className='relative w-full h-full'> | ||
<Map | ||
ref={mapRef} | ||
id='map' | ||
initialViewState={{ | ||
bounds: metadata.bbox, | ||
fitBoundsOptions: fitBoundOpts | ||
}} | ||
onClick={useDebouncedCallback(onClick, 200, { leading: true, maxWait: 200 })} | ||
reuseMaps | ||
mapboxAccessToken={process.env.NEXT_PUBLIC_MAPBOX_API_KEY} | ||
mapStyle={MAP_STYLES.light} | ||
cooperativeGestures | ||
interactiveLayerIds={['crags']} | ||
> | ||
<ScaleControl /> | ||
<FullscreenControl /> | ||
<NavigationControl showCompass={false} /> | ||
{selected != null && | ||
<AreaActiveMarker point={selected} />} | ||
<AreaInfoDrawer data={hovered} /> | ||
{boundary != null && | ||
<Source id='child-areas-polygon' type='geojson' data={boundary}> | ||
<Layer {...areaPolygonStyle} /> | ||
</Source>} | ||
</Map> | ||
</div> | ||
) | ||
} | ||
|
||
export default AreaMap | ||
|
||
export const LazyAreaMap = dynamic<AreaMapProps>(async () => await import('./AreaMap').then( | ||
module => module.default), { | ||
ssr: false | ||
}) | ||
|
||
const areaPolygonStyle: LineLayer = { | ||
id: 'polygon', | ||
type: 'line', | ||
paint: { | ||
'line-opacity': ['step', ['zoom'], 0.85, 10, 0.5], | ||
'line-width': ['step', ['zoom'], 2, 10, 6], | ||
'line-color': 'rgb(219,39,119)' | ||
} | ||
} |
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
Oops, something went wrong.
4bdfffe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
open-tacos – ./
www.openbeta.io
open-tacos-git-develop-openbeta-dev.vercel.app
open-tacos-openbeta-dev.vercel.app
openclimbmap.com
openbeta.io
tacos.openbeta.io