import { useCallback } from "react"; import { readReportJson, ReportReadError, useReportQuery, } from "./use-report-query"; /** All report reads share cancellation, visible errors and safe refresh behavior. */ export function useFeatureReport( endpoint: string, field: string, validate: (data: T) => boolean, ) { const read = useCallback( async (signal: AbortSignal) => { const root = window.swiftCommerceData?.apiUrl || "/wp-json/swift-commerce/v1"; const response = await readReportJson>( `${root}${endpoint}`, signal, ); const data = response[field]; if (!data || !validate(data)) throw new ReportReadError( "The report returned incomplete data. Please try again.", ); return data; }, [endpoint, field, validate], ); return useReportQuery(endpoint, read); }