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 87 88 | /** * useRevenueTrend Hook * * Fetches revenue trend data for charting. */ "use client"; import { useState, useEffect, useCallback } from "react"; export interface RevenueTrendPoint { date: string; revenue: number; orders: number; averageOrderValue: number; } export type TrendPeriod = "7d" | "14d" | "30d" | "90d"; export type TrendGranularity = "day" | "week" | "month"; interface UseRevenueTrendResult { data: RevenueTrendPoint[]; isLoading: boolean; error: Error | null; period: TrendPeriod; setPeriod: (period: TrendPeriod) => void; granularity: TrendGranularity; setGranularity: (granularity: TrendGranularity) => void; refetch: () => Promise<void>; } export function useRevenueTrend( initialPeriod: TrendPeriod = "30d", initialGranularity: TrendGranularity = "day" ): UseRevenueTrendResult { const [data, setData] = useState<RevenueTrendPoint[]>([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState<Error | null>(null); const [period, setPeriod] = useState<TrendPeriod>(initialPeriod); const [granularity, setGranularity] = useState<TrendGranularity>(initialGranularity); const fetchTrend = useCallback(async () => { try { setIsLoading(true); setError(null); const days = period === "7d" ? 7 : period === "14d" ? 14 : period === "30d" ? 30 : 90; const response = await fetch( `/api/admin/analytics/revenue/trend?days=${days}&granularity=${granularity}` ); if (!response.ok) { throw new Error(`Failed to fetch revenue trend: ${response.status}`); } const result = await response.json(); if (result.success && result.data) { setData(result.data); } else { setData([]); } } catch (err) { setError(err instanceof Error ? err : new Error("Unknown error")); setData([]); } finally { setIsLoading(false); } }, [period, granularity]); useEffect(() => { fetchTrend(); }, [fetchTrend]); return { data, isLoading, error, period, setPeriod, granularity, setGranularity, refetch: fetchTrend, }; } export default useRevenueTrend; |