|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { createContext, useContext, useState, useEffect, ReactNode } from "react" |
| 4 | +import { useSearchParams, useRouter } from "next/navigation" |
| 5 | + |
| 6 | +interface DashboardTabContextType { |
| 7 | + activeTab: string |
| 8 | + setActiveTab: (value: string) => void |
| 9 | +} |
| 10 | + |
| 11 | +const DashboardTabContext = createContext<DashboardTabContextType | undefined>(undefined) |
| 12 | + |
| 13 | +export function DashboardTabProvider({ children }: { children: ReactNode }) { |
| 14 | + const searchParams = useSearchParams() |
| 15 | + const router = useRouter() |
| 16 | + const [activeTab, setActiveTabState] = useState("genesis") |
| 17 | + |
| 18 | + // Read from URL on mount |
| 19 | + useEffect(() => { |
| 20 | + const tab = searchParams.get("tab") |
| 21 | + if (tab && ["genesis", "points"].includes(tab)) { |
| 22 | + setActiveTabState(tab) |
| 23 | + } |
| 24 | + }, [searchParams]) |
| 25 | + |
| 26 | + const setActiveTab = (value: string) => { |
| 27 | + setActiveTabState(value) |
| 28 | + router.push(`/dashboard?tab=${value}`) |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <DashboardTabContext.Provider value={{ activeTab, setActiveTab }}> |
| 33 | + {children} |
| 34 | + </DashboardTabContext.Provider> |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +export function useDashboardTab() { |
| 39 | + const context = useContext(DashboardTabContext) |
| 40 | + // Return default values if not in provider (for non-dashboard pages) |
| 41 | + if (context === undefined) { |
| 42 | + return { activeTab: "genesis", setActiveTab: () => {} } |
| 43 | + } |
| 44 | + return context |
| 45 | +} |
0 commit comments