'use client'; import React, { createContext, use, useEffect, useState } from 'react'; import { usePathname } from 'next/navigation'; interface NavHistoryContext { previousPath: string | null; canGoBack: boolean; } const NavHistoryContext = createContext({ previousPath: null, canGoBack: false, }); interface Props { children: React.ReactNode; } export function NavHistoryProvider(props: Props) { const pathname = usePathname(); const [previousPath, setPreviousPath] = useState(null); const [currentPath, setCurrentPath] = useState(pathname); useEffect(() => { if (pathname !== currentPath) { setPreviousPath(currentPath); setCurrentPath(pathname); } }, [pathname, currentPath]); return ( {props.children} ); } export function useNavHistory() { const context = use(NavHistoryContext); if (!context) { throw new Error('useNavHistory must be used within a NavHistoryProvider'); } return context; }