|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { createContext, useContext, useState, useEffect } from "react"; |
| 4 | + |
| 5 | +export interface StatData { |
| 6 | + title: string; |
| 7 | + value: string; |
| 8 | + iconName: string; |
| 9 | + trend?: string; |
| 10 | +} |
| 11 | + |
| 12 | +export interface ActivityData { |
| 13 | + id: string; |
| 14 | + type: "donation" | "pool_created" | "reward"; |
| 15 | + user: string; |
| 16 | + amount?: string; |
| 17 | + poolName?: string; |
| 18 | + timestamp: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface DashboardContextType { |
| 22 | + stats: StatData[]; |
| 23 | + activities: ActivityData[]; |
| 24 | + isLoading: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +const DashboardContext = createContext<DashboardContextType | undefined>(undefined); |
| 28 | + |
| 29 | +export function DashboardProvider({ children }: { children: React.ReactNode }) { |
| 30 | + const [stats, setStats] = useState<StatData[]>([]); |
| 31 | + const [activities, setActivities] = useState<ActivityData[]>([]); |
| 32 | + const [isLoading, setIsLoading] = useState(true); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + // Simulate network delay for fetching dashboard data |
| 36 | + const timer = setTimeout(() => { |
| 37 | + setStats([ |
| 38 | + { title: "Total Donated", value: "$45,231.89", iconName: "Banknotes", trend: "+12.5%" }, |
| 39 | + { title: "Active Pools", value: "24", iconName: "Droplets", trend: "+2" }, |
| 40 | + { title: "Impact Score", value: "89.2", iconName: "Activity", trend: "+4.1" }, |
| 41 | + ]); |
| 42 | + setActivities([ |
| 43 | + { id: "1", type: "donation", user: "Alice", amount: "$500", poolName: "Ocean Cleanup", timestamp: "10 mins ago" }, |
| 44 | + { id: "2", type: "pool_created", user: "Bob", poolName: "Reforestation Initiative", timestamp: "2 hours ago" }, |
| 45 | + { id: "3", type: "reward", user: "Charlie", amount: "50 NEVO", timestamp: "5 hours ago" }, |
| 46 | + { id: "4", type: "donation", user: "Diana", amount: "$150", poolName: "Local Shelter", timestamp: "1 day ago" }, |
| 47 | + ]); |
| 48 | + setIsLoading(false); |
| 49 | + }, 1500); |
| 50 | + |
| 51 | + return () => clearTimeout(timer); |
| 52 | + }, []); |
| 53 | + |
| 54 | + return ( |
| 55 | + <DashboardContext.Provider value={{ stats, activities, isLoading }}> |
| 56 | + {children} |
| 57 | + </DashboardContext.Provider> |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export function useDashboard() { |
| 62 | + const context = useContext(DashboardContext); |
| 63 | + if (context === undefined) { |
| 64 | + throw new Error("useDashboard must be used within a DashboardProvider"); |
| 65 | + } |
| 66 | + return context; |
| 67 | +} |
0 commit comments