collapse five duplicate overlay blocks into one tested builder
MapWorkspace held five near-identical useMemo blocks deciding which raster image the map draws under the active theme — terrain, flood depth, thematic raster, WALOUS land cover and bathymetry. Each filtered partitions by source name, read bbox_epsg4326 and assembled the same overlay shape, so the parts that genuinely differ per theme were buried in the repetition. One builder makes the rule testable and leaves only the source, the label and the opacity varying. A raster whose bounds are unusable is now skipped rather than drawn from a partial bbox: an overlay in the wrong place is worse than no overlay. The legend asked "are these thematic or WALOUS overlays" by inspecting two of the five lists. That is a property of the source, so it says so directly. Two contract tests needed fixing rather than repointing. One asserted `"api" not in source.lower()`, which the new hook name useMapImageOverlays matches inside "useM-api-mageOverlays" — as would rapid, capital or therapy. The contract is that this component talks to no API client, so it now says that. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { useCallback, useState } from 'react'
|
||||
|
||||
import type { VectorSelectionBBox } from '../types'
|
||||
import { bboxToInputState, normalizeBboxFromCorners, parseBboxInput } from '../components/map/mapWorkspaceUtils'
|
||||
|
||||
/**
|
||||
* Drawing a rectangle on the map, by drag or by two clicks.
|
||||
*
|
||||
* The rule that matters here is not the geometry but the clearing: a new
|
||||
* selection must retire the previous theme insights and temporal comparison
|
||||
* before anything is drawn, so results from the old rectangle can never be read
|
||||
* as belonging to the new one.
|
||||
*/
|
||||
|
||||
interface MapRectangleSelectionOptions {
|
||||
initialBbox: VectorSelectionBBox | null
|
||||
setSelectionBbox: (bbox: VectorSelectionBBox) => void
|
||||
/** Everything that describes the *previous* rectangle and must not survive. */
|
||||
retireStaleResults: () => void
|
||||
}
|
||||
|
||||
export interface MapRectangleSelection {
|
||||
drawing: boolean
|
||||
setDrawing: (value: boolean) => void
|
||||
firstCorner: [number, number] | null
|
||||
bboxInput: ReturnType<typeof bboxToInputState>
|
||||
setBboxInput: (value: ReturnType<typeof bboxToInputState>) => void
|
||||
/** The rectangle currently typed into the coordinate fields, if it parses. */
|
||||
typedBbox: VectorSelectionBBox | null
|
||||
handleCoordinateSelect: (coordinate: [number, number]) => void
|
||||
handleBboxPreview: (bbox: VectorSelectionBBox) => void
|
||||
handleBboxSelect: (bbox: VectorSelectionBBox) => void
|
||||
reset: () => void
|
||||
}
|
||||
|
||||
export function useMapRectangleSelection({
|
||||
initialBbox,
|
||||
setSelectionBbox,
|
||||
retireStaleResults,
|
||||
}: MapRectangleSelectionOptions): MapRectangleSelection {
|
||||
const [drawing, setDrawing] = useState(false)
|
||||
const [firstCorner, setFirstCorner] = useState<[number, number] | null>(null)
|
||||
const [bboxInput, setBboxInput] = useState(bboxToInputState(initialBbox))
|
||||
|
||||
const commit = useCallback(
|
||||
(bbox: VectorSelectionBBox) => {
|
||||
setFirstCorner(null)
|
||||
setDrawing(false)
|
||||
retireStaleResults()
|
||||
setSelectionBbox(bbox)
|
||||
},
|
||||
[retireStaleResults, setSelectionBbox],
|
||||
)
|
||||
|
||||
const handleCoordinateSelect = useCallback(
|
||||
(coordinate: [number, number]) => {
|
||||
// Two clicks make a rectangle: the first only records a corner, and must
|
||||
// not disturb the results that are still on screen.
|
||||
if (!firstCorner) {
|
||||
setFirstCorner(coordinate)
|
||||
return
|
||||
}
|
||||
commit(normalizeBboxFromCorners(firstCorner, coordinate))
|
||||
},
|
||||
[commit, firstCorner],
|
||||
)
|
||||
|
||||
// A drag preview moves the rectangle without ending the gesture, so nothing
|
||||
// is retired until the drag is released.
|
||||
const handleBboxPreview = useCallback(
|
||||
(bbox: VectorSelectionBBox) => setSelectionBbox(bbox),
|
||||
[setSelectionBbox],
|
||||
)
|
||||
|
||||
const handleBboxSelect = useCallback((bbox: VectorSelectionBBox) => commit(bbox), [commit])
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setFirstCorner(null)
|
||||
setDrawing(false)
|
||||
}, [])
|
||||
|
||||
return {
|
||||
drawing,
|
||||
setDrawing,
|
||||
firstCorner,
|
||||
bboxInput,
|
||||
setBboxInput,
|
||||
typedBbox: parseBboxInput(bboxInput),
|
||||
handleCoordinateSelect,
|
||||
handleBboxPreview,
|
||||
handleBboxSelect,
|
||||
reset,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user