This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / webapp / providers / navHistory.tsx
1.1 kB 49 lines
1'use client'; 2 3import React, { createContext, use, useEffect, useState } from 'react'; 4import { usePathname } from 'next/navigation'; 5 6interface NavHistoryContext { 7 previousPath: string | null; 8 canGoBack: boolean; 9} 10 11const NavHistoryContext = createContext<NavHistoryContext>({ 12 previousPath: null, 13 canGoBack: false, 14}); 15 16interface Props { 17 children: React.ReactNode; 18} 19 20export function NavHistoryProvider(props: Props) { 21 const pathname = usePathname(); 22 const [previousPath, setPreviousPath] = useState<string | null>(null); 23 const [currentPath, setCurrentPath] = useState<string>(pathname); 24 25 useEffect(() => { 26 if (pathname !== currentPath) { 27 setPreviousPath(currentPath); 28 setCurrentPath(pathname); 29 } 30 }, [pathname, currentPath]); 31 32 return ( 33 <NavHistoryContext 34 value={{ previousPath, canGoBack: previousPath !== null }} 35 > 36 {props.children} 37 </NavHistoryContext> 38 ); 39} 40 41export function useNavHistory() { 42 const context = use(NavHistoryContext); 43 44 if (!context) { 45 throw new Error('useNavHistory must be used within a NavHistoryProvider'); 46 } 47 48 return context; 49}