Skip to content

Commit

Permalink
break out into getInitialCenter fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane98c committed Mar 28, 2024
1 parent 4900288 commit 797129f
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/region/region-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,32 @@ function getInitialRadius(map, units, minRadius, maxRadius) {

function isValidCoordinate(longitude, latitude) {
return (
longitude >= -180 && longitude <= 180 && latitude >= -90 && latitude <= 90
longitude !== undefined &&
latitude !== undefined &&
longitude >= -180 &&
longitude <= 180 &&
latitude >= -90 &&
latitude <= 90
)
}

function getInitialCenter(map, center) {
if (
Array.isArray(center) &&
center.length === 2 &&
isValidCoordinate(center[0], center[1])
) {
return new mapboxgl.LngLat(center[0], center[1])
} else {
if (center) {
console.warn(
`Invalid initialCenter provided: ${center}. Should be [lng, lat]. Using map center instead.`
)
}
return map.getCenter()
}
}

// TODO:
// - accept mode (only accept mode="circle" to start)
function RegionPicker({
Expand All @@ -40,19 +62,7 @@ function RegionPicker({
const { map } = useMapbox()
const id = useRef(uuidv4())

let safeInitialCenter
if (isValidCoordinate(initialCenterProp[0], initialCenterProp[1])) {
safeInitialCenter = new mapboxgl.LngLat(
initialCenterProp[0],
initialCenterProp[1]
)
} else {
safeInitialCenter = map.getCenter()
console.warn(
`Invalid initial center coordinates: ${initialCenterProp}, Falling back to map's current center.`
)
}
const initialCenter = useRef(safeInitialCenter)
const initialCenter = useRef(getInitialCenter(map, initialCenterProp))

const initialRadius = useRef(
initialRadiusProp || getInitialRadius(map, units, minRadius, maxRadius)
Expand Down

0 comments on commit 797129f

Please sign in to comment.