Extract frontend orchestration hooks
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { useState } from 'react'
|
||||
import { analysisApi } from '../services/api'
|
||||
import type { ChangeDetectionSummary, DatasetCreateResponse } from '../types'
|
||||
import { formatError } from '../lib/formatError'
|
||||
|
||||
interface ChangeDetectionWorkflowOptions {
|
||||
selectedProjectId: string | null
|
||||
availableVectorDatasets: DatasetCreateResponse[]
|
||||
loadDatasetJobs: (projectId: string, datasetId: string) => Promise<void>
|
||||
}
|
||||
|
||||
export function useChangeDetectionWorkflow({
|
||||
selectedProjectId,
|
||||
availableVectorDatasets,
|
||||
loadDatasetJobs,
|
||||
}: ChangeDetectionWorkflowOptions) {
|
||||
const [changeSourceDatasetId, setChangeSourceDatasetId] = useState('')
|
||||
const [changeTargetDatasetId, setChangeTargetDatasetId] = useState('')
|
||||
const [changeIouThreshold, setChangeIouThreshold] = useState(0.8)
|
||||
const [changeIncludeUnchanged, setChangeIncludeUnchanged] = useState(true)
|
||||
const [runningChangeDetection, setRunningChangeDetection] = useState(false)
|
||||
const [changeDetectionResult, setChangeDetectionResult] = useState<ChangeDetectionSummary | null>(null)
|
||||
const [changeDetectionError, setChangeDetectionError] = useState<string | null>(null)
|
||||
|
||||
const runChangeDetection = async () => {
|
||||
const sourceDatasetId = changeSourceDatasetId || availableVectorDatasets[0]?.id
|
||||
const targetDatasetId =
|
||||
changeTargetDatasetId || availableVectorDatasets.find((dataset) => dataset.id !== sourceDatasetId)?.id
|
||||
if (!sourceDatasetId || !targetDatasetId) {
|
||||
setChangeDetectionError('Select two vector datasets')
|
||||
return
|
||||
}
|
||||
if (sourceDatasetId === targetDatasetId) {
|
||||
setChangeDetectionError('Source and target datasets must differ')
|
||||
return
|
||||
}
|
||||
if (changeIouThreshold < 0 || changeIouThreshold > 1) {
|
||||
setChangeDetectionError('IoU threshold must be between 0 and 1')
|
||||
return
|
||||
}
|
||||
setChangeDetectionError(null)
|
||||
setChangeDetectionResult(null)
|
||||
setRunningChangeDetection(true)
|
||||
try {
|
||||
const job = await analysisApi.runChangeDetection({
|
||||
source_dataset_id: sourceDatasetId,
|
||||
target_dataset_id: targetDatasetId,
|
||||
iou_threshold: changeIouThreshold,
|
||||
include_unchanged: changeIncludeUnchanged,
|
||||
})
|
||||
if (job.status !== 'success') {
|
||||
throw new Error(job.error_message || 'Change detection job failed')
|
||||
}
|
||||
if (!job.result_json) {
|
||||
throw new Error('Change detection completed without result payload')
|
||||
}
|
||||
setChangeSourceDatasetId(sourceDatasetId)
|
||||
setChangeTargetDatasetId(targetDatasetId)
|
||||
setChangeDetectionResult(job.result_json)
|
||||
if (selectedProjectId) {
|
||||
await loadDatasetJobs(selectedProjectId, sourceDatasetId)
|
||||
}
|
||||
} catch (error) {
|
||||
setChangeDetectionError(formatError(error, 'Change detection failed'))
|
||||
} finally {
|
||||
setRunningChangeDetection(false)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
changeSourceDatasetId,
|
||||
changeTargetDatasetId,
|
||||
changeIouThreshold,
|
||||
changeIncludeUnchanged,
|
||||
runningChangeDetection,
|
||||
changeDetectionResult,
|
||||
changeDetectionError,
|
||||
runChangeDetection,
|
||||
setChangeSourceDatasetId,
|
||||
setChangeTargetDatasetId,
|
||||
setChangeIouThreshold,
|
||||
setChangeIncludeUnchanged,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user