-
-
Notifications
You must be signed in to change notification settings - Fork 345
fix: restrict zooming and panning on data inspector #2574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CommanderStorm
merged 5 commits into
maplibre:main
from
manbhav234:fix-zoom-panning-data-inspector-ui
Feb 26, 2026
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
358444a
fix: restrict zooming and panning on data inspector
manbhav234 4d7c0f3
chore(fmt): apply pre-commit formatting fixes
pre-commit-ci[bot] 22f5235
fix linting issues
manbhav234 1cbc4e2
Update martin/martin-ui/src/components/dialogs/tile-inspect-map.tsx
CommanderStorm 69e1d1b
chore(fmt): apply pre-commit formatting fixes
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
martin/martin-ui/src/components/dialogs/tile-inspect-map.tsx
This file contains hidden or 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,155 @@ | ||
| 'use client'; | ||
|
|
||
| import MaplibreInspect from '@maplibre/maplibre-gl-inspect'; | ||
| import type { MapRef } from '@vis.gl/react-maplibre'; | ||
| import { Layer, Map as MapLibreMap, Source } from '@vis.gl/react-maplibre'; | ||
| import { Popup } from 'maplibre-gl'; | ||
| import { type ErrorInfo, useCallback, useEffect, useId, useRef } from 'react'; | ||
| import { Toaster } from '@/components/ui/toaster'; | ||
| import { useAsyncOperation } from '@/hooks/use-async-operation'; | ||
| import { useToast } from '@/hooks/use-toast'; | ||
| import { buildMartinUrl } from '@/lib/api'; | ||
| import type { TileSource } from '@/lib/types'; | ||
| import { ErrorBoundary } from '../error/error-boundary'; | ||
|
|
||
| interface TileInspectDialogMapProps { | ||
| name: string; | ||
| source: TileSource; | ||
| } | ||
|
|
||
| interface TileJson { | ||
| bounds: [number, number, number, number]; | ||
| maxzoom?: number; | ||
| minzoom?: number; | ||
| center?: [number, number, number]; | ||
| } | ||
|
|
||
| const fetchTileJson = async (endpoint: string): Promise<TileJson> => { | ||
| const response = await fetch(buildMartinUrl(`/${endpoint}`)); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch tileJson: ${response.statusText}`); | ||
| } | ||
| return response.json(); | ||
| }; | ||
|
|
||
| export function TileInspectDialogMap({ name, source }: TileInspectDialogMapProps) { | ||
| const { toast } = useToast(); | ||
| const id = useId(); | ||
| const mapRef = useRef<MapRef>(null); | ||
| const inspectControlRef = useRef<MaplibreInspect>(null); | ||
|
|
||
| const tileJsonOperation = useAsyncOperation<TileJson>(() => fetchTileJson(name), { | ||
| onError: (error: Error) => console.error('TileJson Fetch Failed:', error), | ||
| showErrorToast: false, | ||
| }); | ||
|
|
||
| const isImageSource = ['image/gif', 'image/jpeg', 'image/png', 'image/webp'].includes( | ||
| source.content_type, | ||
| ); | ||
|
|
||
| // biome-ignore lint/correctness/useExhaustiveDependencies: if we list tileJson below, this is an infinte loop | ||
| useEffect(() => { | ||
| tileJsonOperation.execute(); | ||
| }, []); | ||
|
|
||
| const configureMap = useCallback(() => { | ||
| if (!tileJsonOperation.data) { | ||
| return; | ||
| } | ||
| if (!mapRef.current) { | ||
| console.error('Map not found despite being initialized, this cannot happen'); | ||
| return; | ||
| } | ||
| const map = mapRef.current.getMap(); | ||
| const tileJson: TileJson = tileJsonOperation.data; | ||
| //Error when first value set to -180. it gets resolved on setting to -179 or lower. Other values work fine. | ||
| map.setMaxBounds([ | ||
| [tileJson.bounds[0] === -180 ? -179 : tileJson.bounds[0], tileJson.bounds[1]], | ||
| [tileJson.bounds[2], tileJson.bounds[3]], | ||
| ]); | ||
| if (tileJson.minzoom) { | ||
| map.setMinZoom(tileJson.minzoom); | ||
| map.setZoom(tileJson.minzoom); | ||
| } | ||
| if (tileJson.maxzoom) { | ||
| map.setMaxZoom(tileJson.maxzoom); | ||
| } | ||
| if (tileJson.center) { | ||
| map.setCenter([tileJson.center[0], tileJson.center[1]]); | ||
| } | ||
| }, [tileJsonOperation.data]); | ||
|
|
||
| const addInspectorToMap = useCallback(() => { | ||
| if (!mapRef.current) { | ||
| console.error('Map not found despite being initialized, this cannot happen'); | ||
| return; | ||
| } | ||
| const map = mapRef.current.getMap(); | ||
|
|
||
| map.addSource(name, { type: 'vector', url: buildMartinUrl(`/${name}`) }); | ||
| // Import and add the inspect control | ||
| if (inspectControlRef.current) { | ||
| map.removeControl(inspectControlRef.current); | ||
| } | ||
|
|
||
| inspectControlRef.current = new MaplibreInspect({ | ||
| popup: new Popup({ | ||
| closeButton: false, | ||
| closeOnClick: false, | ||
| }), | ||
| showInspectButton: false, | ||
| showInspectMap: true, | ||
| showInspectMapPopup: true, | ||
| showInspectMapPopupOnHover: true, | ||
| showMapPopup: true, | ||
| }); | ||
|
|
||
| map.addControl(inspectControlRef.current); | ||
|
|
||
| configureMap(); | ||
| }, [name, configureMap]); | ||
|
|
||
| return ( | ||
| <ErrorBoundary | ||
| onError={(error: Error, errorInfo: ErrorInfo) => { | ||
| console.error('Application error:', error, errorInfo); | ||
| toast({ | ||
| description: 'An unexpected error occurred. The page will reload automatically.', | ||
| title: 'Application Error', | ||
| variant: 'destructive', | ||
| }); | ||
|
|
||
| // Auto-reload after 3 seconds | ||
| setTimeout(() => { | ||
| window.location.reload(); | ||
| }, 3000); | ||
| }} | ||
| > | ||
| {isImageSource ? ( | ||
| <MapLibreMap | ||
| onLoad={configureMap} | ||
| ref={mapRef} | ||
| reuseMaps={false} | ||
| style={{ | ||
| height: '500px', | ||
| width: '100%', | ||
| }} | ||
| > | ||
| <Source id={`${id}tile-source`} type="raster" url={buildMartinUrl(`/${name}`)} /> | ||
| <Layer id={`${id}tile-layer`} source={`${id}tile-source`} type="raster" /> | ||
| </MapLibreMap> | ||
| ) : ( | ||
| <MapLibreMap | ||
| onLoad={addInspectorToMap} | ||
| ref={mapRef} | ||
| reuseMaps={false} | ||
| style={{ | ||
| height: '500px', | ||
| width: '100%', | ||
| }} | ||
| ></MapLibreMap> | ||
| )} | ||
| <Toaster /> | ||
| </ErrorBoundary> | ||
| ); | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.