o2o-infinith-web/src/hooks/useReport.ts

51 lines
1.4 KiB
TypeScript

/**
* useReport — fetch a completed report from the API.
*
* Prototype version had DEMO_REPORTS hardcoded mapping — REMOVED.
* This MVP hook is API-only; no fallback mocks.
*
* TODO (D5): after transformReport.ts is ported, pipe raw → transformed
* so pages continue to receive the shape they expect.
*/
import { useEffect, useState } from 'react'
import { apiClient } from '@/lib/apiClient'
import type { MarketingReport } from '@/types/report'
interface UseReportResult {
data: MarketingReport | null
isLoading: boolean
error: Error | null
}
export function useReport(runId: string | undefined): UseReportResult {
const [data, setData] = useState<MarketingReport | null>(null)
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<Error | null>(null)
useEffect(() => {
if (!runId) return
let cancelled = false
;(async () => {
try {
setIsLoading(true)
const { data: raw } = await apiClient.get(`/api/reports/${runId}`)
if (!cancelled) {
// TODO: run through transformReport(raw) once util is ported
setData(raw as MarketingReport)
}
} catch (err) {
if (!cancelled) setError(err as Error)
} finally {
if (!cancelled) setIsLoading(false)
}
})()
return () => {
cancelled = true
}
}, [runId])
return { data, isLoading, error }
}