-
-
Notifications
You must be signed in to change notification settings - Fork 306
fix: restrict zooming and panning on the data inspector (#2325) #2326
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
Draft
harshasiddartha
wants to merge
2
commits into
maplibre:main
Choose a base branch
from
harshasiddartha:fix/restrict-zoom-pan-inspector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| 'use client'; | ||
|
|
||
| import { useCallback, useId, useRef } from 'react'; | ||
| import { useCallback, useEffect, useId, useRef, useState } from 'react'; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
|
|
@@ -17,6 +17,23 @@ import { Database } from 'lucide-react'; | |
| import { Popup } from 'maplibre-gl'; | ||
| import { buildMartinUrl } from '@/lib/api'; | ||
|
|
||
| interface TileJSON { | ||
| tilejson?: string; | ||
| name?: string; | ||
| description?: string; | ||
| version?: string; | ||
| attribution?: string; | ||
| scheme?: string; | ||
| tiles: string[]; | ||
| grids?: string[]; | ||
| data?: string[]; | ||
| minzoom?: number; | ||
| maxzoom?: number; | ||
| bounds?: [number, number, number, number]; // [west, south, east, north] | ||
| center?: [number, number, number]; // [longitude, latitude, zoom] | ||
| vector_layers?: unknown[]; | ||
| } | ||
|
|
||
| interface TileInspectDialogProps { | ||
| name: string; | ||
| source: TileSource; | ||
|
|
@@ -27,6 +44,86 @@ export function TileInspectDialog({ name, source, onCloseAction }: TileInspectDi | |
| const id = useId(); | ||
| const mapRef = useRef<MapRef>(null); | ||
| const inspectControlRef = useRef<MaplibreInspect>(null); | ||
| const [tileJSON, setTileJSON] = useState<TileJSON | null>(null); | ||
|
|
||
| const configureMapBounds = useCallback(() => { | ||
| if (!mapRef.current || !tileJSON) { | ||
| return; | ||
| } | ||
|
|
||
| const map = mapRef.current.getMap(); | ||
|
|
||
| // Set minzoom and maxzoom restrictions | ||
| if (tileJSON.minzoom !== undefined) { | ||
| map.setMinZoom(tileJSON.minzoom); | ||
| } | ||
| if (tileJSON.maxzoom !== undefined) { | ||
| map.setMaxZoom(tileJSON.maxzoom); | ||
| } | ||
|
|
||
| // Set bounds restrictions if available | ||
| if (tileJSON.bounds) { | ||
| const [west, south, east, north] = tileJSON.bounds; | ||
| map.setMaxBounds([ | ||
| [west, south], | ||
| [east, north], | ||
| ]); | ||
| } | ||
|
|
||
| // Fit to bounds or center if available | ||
| if (tileJSON.bounds) { | ||
| const [west, south, east, north] = tileJSON.bounds; | ||
| map.fitBounds( | ||
| [ | ||
| [west, south], | ||
| [east, north], | ||
| ], | ||
| { | ||
| maxZoom: tileJSON.maxzoom, | ||
| padding: 50, | ||
| }, | ||
| ); | ||
| } else if (tileJSON.center) { | ||
| const [lng, lat, zoom] = tileJSON.center; | ||
| map.setCenter([lng, lat]); | ||
| if (zoom !== undefined) { | ||
| map.setZoom(zoom); | ||
| } | ||
| } | ||
| }, [tileJSON]); | ||
|
|
||
| // Fetch TileJSON when dialog opens | ||
| useEffect(() => { | ||
| let cancelled = false; | ||
|
|
||
| fetch(buildMartinUrl(`/${name}`)) | ||
| .then((response) => { | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch TileJSON: ${response.statusText}`); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .then((data: TileJSON) => { | ||
| if (!cancelled) { | ||
| setTileJSON(data); | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| console.error('Error fetching TileJSON:', error); | ||
| // Continue without TileJSON restrictions if fetch fails | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [name]); | ||
|
|
||
| // Reconfigure bounds when TileJSON loads after map is already initialized | ||
| useEffect(() => { | ||
| if (tileJSON && mapRef.current) { | ||
| configureMapBounds(); | ||
| } | ||
| }, [tileJSON, configureMapBounds]); | ||
|
|
||
| const addInspectorToMap = useCallback(() => { | ||
| if (!mapRef.current) { | ||
|
|
@@ -54,12 +151,15 @@ export function TileInspectDialog({ name, source, onCloseAction }: TileInspectDi | |
| }); | ||
|
|
||
| map.addControl(inspectControlRef.current); | ||
| }, [name]); | ||
|
|
||
| // Configure bounds after adding inspector | ||
| configureMapBounds(); | ||
| }, [name, configureMapBounds]); | ||
| const isImageSource = ['image/gif', 'image/jpeg', 'image/png', 'image/webp'].includes( | ||
| source.content_type, | ||
| ); | ||
| return ( | ||
| <Dialog onOpenChange={(v) => !v && onCloseAction()} open={true}> | ||
| <Dialog onOpenChange={(v: boolean) => !v && onCloseAction()} open={true}> | ||
| <DialogContent className="max-w-6xl w-full p-6 max-h-[90vh] overflow-auto"> | ||
| <DialogHeader className="mb-6"> | ||
| <DialogTitle className="text-2xl flex items-center justify-between"> | ||
|
|
@@ -76,6 +176,31 @@ export function TileInspectDialog({ name, source, onCloseAction }: TileInspectDi | |
| <section className="border rounded-lg overflow-hidden"> | ||
| {isImageSource ? ( | ||
| <MapLibreMap | ||
| initialViewState={ | ||
| tileJSON?.center | ||
| ? { | ||
| latitude: tileJSON.center[1], | ||
| longitude: tileJSON.center[0], | ||
| zoom: tileJSON.center[2] ?? 0, | ||
| } | ||
| : undefined | ||
| } | ||
| maxBounds={ | ||
| tileJSON?.bounds | ||
| ? [ | ||
| [tileJSON.bounds[0], tileJSON.bounds[1]], | ||
| [tileJSON.bounds[2], tileJSON.bounds[3]], | ||
| ] | ||
| : undefined | ||
| } | ||
| maxZoom={tileJSON?.maxzoom} | ||
| minZoom={tileJSON?.minzoom} | ||
| onLoad={() => { | ||
| // Configure bounds for raster sources after map loads | ||
| if (tileJSON) { | ||
| configureMapBounds(); | ||
| } | ||
| }} | ||
|
Comment on lines
+198
to
+203
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You always seem to configure this twice:
Could you explain this? 🤔 |
||
| ref={mapRef} | ||
| reuseMaps={false} | ||
| style={{ | ||
|
|
@@ -88,6 +213,25 @@ export function TileInspectDialog({ name, source, onCloseAction }: TileInspectDi | |
| </MapLibreMap> | ||
| ) : ( | ||
| <MapLibreMap | ||
| initialViewState={ | ||
| tileJSON?.center | ||
| ? { | ||
| latitude: tileJSON.center[1], | ||
| longitude: tileJSON.center[0], | ||
| zoom: tileJSON.center[2] ?? 0, | ||
| } | ||
| : undefined | ||
| } | ||
| maxBounds={ | ||
| tileJSON?.bounds | ||
| ? [ | ||
| [tileJSON.bounds[0], tileJSON.bounds[1]], | ||
| [tileJSON.bounds[2], tileJSON.bounds[3]], | ||
| ] | ||
| : undefined | ||
| } | ||
| maxZoom={tileJSON?.maxzoom} | ||
| minZoom={tileJSON?.minzoom} | ||
| onLoad={addInspectorToMap} | ||
| ref={mapRef} | ||
| reuseMaps={false} | ||
|
|
||
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.
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.
not sure if this is very react-y.
I think the current recomendation is to use
<Suspense+asyncDataand just wrap the components where we need the data.At least this is explained like this here:
https://stackoverflow.com/questions/53332321/react-hook-warnings-for-async-function-in-useeffect-useeffect-function-must-ret
What do you think? 🤔