All files / web/src/hooks use-smart-back.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 1/1
100% Lines 18/18

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    1x 1x         1x 10x 10x   10x 10x   8x 2x 8x   4x 6x   2x 2x 8x 10x 10x 10x  
'use client';
 
import { useNavigate, useRouter } from '@tanstack/react-router';
import { useCallback } from 'react';
 
/**
 * Hook for smart back navigation that respects browser history
 */
export function useSmartBack() {
  const router = useRouter();
  const navigate = useNavigate();
 
  return useCallback(
    (fallbackUrl?: string) => {
      // Check if we can go back in browser history
      if (typeof window !== 'undefined' && window.history.length > 1) {
        router.history.back();
      } else if (fallbackUrl) {
        // Fallback to provided URL
        navigate({ to: fallbackUrl });
      } else {
        // Ultimate fallback to agents
        navigate({ to: '/agents' });
      }
    },
    [router, navigate],
  );
}