Persist map selections as derived datasets
GeoIntel CI / docs-smoke (push) Has been cancelled
GeoIntel CI / contract-smoke (push) Has been cancelled

This commit is contained in:
Codex
2026-06-25 02:25:49 +02:00
parent 1e42302f62
commit b92faafa74
16 changed files with 587 additions and 2 deletions
@@ -0,0 +1,57 @@
import { useState } from 'react'
import type { DatasetCreateResponse, VectorSelectionBBox } from '../types'
import { formatError } from '../lib/formatError'
import { datasetsApi } from '../services/api'
interface UseMapSelectionDatasetOptions {
selectedProjectId: string | null
selectedDataset: DatasetCreateResponse | null
isVectorDatasetType: (datasetType: string) => boolean
loadProjectData: (projectId: string) => Promise<unknown>
loadDatasetDetails: (projectId: string, dataset: DatasetCreateResponse) => Promise<void>
setMapLayerVisible: (visible: boolean) => void
}
export function useMapSelectionDataset({
selectedProjectId,
selectedDataset,
isVectorDatasetType,
loadProjectData,
loadDatasetDetails,
setMapLayerVisible,
}: UseMapSelectionDatasetOptions) {
const [selectionDatasetSaving, setSelectionDatasetSaving] = useState(false)
const [selectionDatasetError, setSelectionDatasetError] = useState<string | null>(null)
const [latestSelectionDataset, setLatestSelectionDataset] = useState<DatasetCreateResponse | null>(null)
const deriveMapSelectionDataset = async (bbox: VectorSelectionBBox) => {
if (!selectedProjectId || !selectedDataset || !isVectorDatasetType(selectedDataset.dataset_type)) {
setSelectionDatasetError('Select a vector dataset before saving the area as a dataset.')
return
}
setSelectionDatasetSaving(true)
setSelectionDatasetError(null)
try {
const derived = await datasetsApi.deriveVectorSelection(selectedProjectId, selectedDataset.id, {
bbox: { ...bbox, crs: 'EPSG:4326' },
limit: 250,
output_name: `${selectedDataset.name.replace(/\.(geo)?json$/i, '')}-selection-dataset`,
})
setLatestSelectionDataset(derived)
await loadProjectData(selectedProjectId)
await loadDatasetDetails(selectedProjectId, derived)
setMapLayerVisible(true)
} catch (error) {
setSelectionDatasetError(formatError(error, 'Failed to save area as dataset'))
} finally {
setSelectionDatasetSaving(false)
}
}
return {
selectionDatasetSaving,
selectionDatasetError,
latestSelectionDataset,
deriveMapSelectionDataset,
}
}