feat: make Mol explorer map-first
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { formatError } from '../lib/formatError'
|
||||
import { datasetsApi } from '../services/api/datasets'
|
||||
import type { DatasetCreateResponse, VectorSelectionBBox, VectorSelectionResponse } from '../types'
|
||||
|
||||
export interface MapThemeQuery<TThemeId extends string> {
|
||||
themeId: TThemeId
|
||||
dataset: DatasetCreateResponse
|
||||
}
|
||||
|
||||
export interface MapThemeInsight<TThemeId extends string> extends MapThemeQuery<TThemeId> {
|
||||
result: VectorSelectionResponse
|
||||
}
|
||||
|
||||
export function useMapThemeSelectionInsights<TThemeId extends string>(
|
||||
selectedProjectId: string | null,
|
||||
) {
|
||||
const [themeInsights, setThemeInsights] = useState<Array<MapThemeInsight<TThemeId>>>([])
|
||||
const [themeInsightsLoading, setThemeInsightsLoading] = useState(false)
|
||||
const [themeInsightsError, setThemeInsightsError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
setThemeInsights([])
|
||||
setThemeInsightsError(null)
|
||||
}, [selectedProjectId])
|
||||
|
||||
const clearThemeInsights = () => {
|
||||
setThemeInsights([])
|
||||
setThemeInsightsError(null)
|
||||
}
|
||||
|
||||
const loadThemeInsights = async (
|
||||
bbox: VectorSelectionBBox,
|
||||
queries: Array<MapThemeQuery<TThemeId>>,
|
||||
): Promise<Array<MapThemeInsight<TThemeId>>> => {
|
||||
if (!selectedProjectId) {
|
||||
setThemeInsights([])
|
||||
setThemeInsightsError('Open eerst een project om de selectie te analyseren.')
|
||||
return []
|
||||
}
|
||||
|
||||
setThemeInsightsLoading(true)
|
||||
setThemeInsightsError(null)
|
||||
try {
|
||||
const settled = await Promise.allSettled(
|
||||
queries.map(async ({ themeId, dataset }) => ({
|
||||
themeId,
|
||||
dataset,
|
||||
result: await datasetsApi.selectVectorFeatures(selectedProjectId, dataset.id, {
|
||||
bbox,
|
||||
limit: 1000,
|
||||
}),
|
||||
})),
|
||||
)
|
||||
const successful = settled.flatMap((item) => (item.status === 'fulfilled' ? [item.value] : []))
|
||||
const failureCount = settled.length - successful.length
|
||||
setThemeInsights(successful)
|
||||
if (failureCount > 0) {
|
||||
setThemeInsightsError(
|
||||
`${failureCount} beschikbare databron${failureCount === 1 ? '' : 'nen'} kon niet worden bevraagd.`,
|
||||
)
|
||||
}
|
||||
return successful
|
||||
} catch (error) {
|
||||
setThemeInsights([])
|
||||
setThemeInsightsError(formatError(error, 'De gebiedsanalyse is mislukt.'))
|
||||
return []
|
||||
} finally {
|
||||
setThemeInsightsLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
themeInsights,
|
||||
themeInsightsLoading,
|
||||
themeInsightsError,
|
||||
loadThemeInsights,
|
||||
clearThemeInsights,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user