Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | /** * useRevenueForecast Hook * * Fetches revenue forecast predictions for planning insights. */ "use client"; import { useState, useEffect, useCallback } from "react"; export interface ForecastPrediction { date: string; predicted: number; lower: number; upper: number; } export interface RevenueForecast { predictions: ForecastPrediction[]; confidence: number; growthRate: number; } interface UseRevenueForecastResult { forecast: RevenueForecast | null; isLoading: boolean; error: Error | null; daysAhead: number; setDaysAhead: (days: number) => void; refetch: () => Promise<void>; } const emptyForecast: RevenueForecast = { predictions: [], confidence: 0, growthRate: 0, }; export function useRevenueForecast(initialDaysAhead: number = 7): UseRevenueForecastResult { const [forecast, setForecast] = useState<RevenueForecast | null>(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState<Error | null>(null); const [daysAhead, setDaysAhead] = useState(initialDaysAhead); const fetchForecast = useCallback(async () => { try { setIsLoading(true); setError(null); const response = await fetch(`/api/admin/analytics/revenue/forecast?days=${daysAhead}`); if (!response.ok) { throw new Error(`Failed to fetch revenue forecast: ${response.status}`); } const result = await response.json(); if (result.success && result.data) { setForecast(result.data); } else { setForecast(emptyForecast); } } catch (err) { setError(err instanceof Error ? err : new Error("Unknown error")); setForecast(emptyForecast); } finally { setIsLoading(false); } }, [daysAhead]); useEffect(() => { fetchForecast(); }, [fetchForecast]); return { forecast, isLoading, error, daysAhead, setDaysAhead, refetch: fetchForecast, }; } export default useRevenueForecast; |