All files / src/web/components BackButton.tsx

94.66% Statements 71/75
92% Branches 23/25
100% Functions 0/0
94.66% Lines 71/75

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x 1x 1x 1x                                 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x     33x 33x 33x 29x 28x 28x   33x     33x 33x 33x 5x 5x 27x 33x   33x 7x 7x 7x 7x 7x 7x 7x 7x 7x     7x 7x 7x 3x 6x 4x 4x 7x     7x 7x     7x 5x 5x   7x 7x   7x 7x 7x 7x           33x   33x   33x 33x 33x 33x   33x 33x 33x   33x   1x   1x  
import { useLogger } from '@common/utils/logger/useLogger';
import { ArrowLeft } from 'lucide-react';
import { memo, useCallback, useMemo } from 'react';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
 
interface BackButtonProps {
  /** Custom target route, defaults to auto-detection */
  targetRoute?: string;
  /** Fallback route if no navigation context is found */
  fallbackRoute?: string;
  /** Additional CSS classes */
  className?: string;
  /** Custom children content */
  children?: React.ReactNode;
  /** Whether to preserve URL parameters when navigating back */
  preserveFilters?: boolean;
  /** Button text to show */
  text?: string;
}
 
const BackButton = memo(({
  targetRoute,
  fallbackRoute = '/inbox',
  className = '',
  children,
  preserveFilters = true,
  text
}: BackButtonProps) => {
  const navigate = useNavigate();
  const location = useLocation();
  const [searchParams] = useSearchParams();
  const log = useLogger();
 
  // Check if we came from the reading queue using multiple indicators
  const isFromReadingQueue = useMemo(() => {
    return (
      location.state?.fromReadingQueue === true ||
      location.state?.from === '/reading-queue' ||
      (typeof location.state?.from === 'string' && location.state.from.includes('reading-queue')) ||
      (typeof document.referrer === 'string' && document.referrer.includes('reading-queue'))
    );
  }, [location.state]);
 
  // Helper function to get the correct back button text
  const getBackButtonText = useCallback(() => {
    if (text) return text;
    if (isFromReadingQueue) {
      return 'Back to Reading Queue';
    }
    return 'Back to Inbox';
  }, [text, isFromReadingQueue]);
 
  const handleBack = useCallback(() => {
    log.debug('Navigation state for back action', {
      action: 'navigate_back',
      metadata: {
        locationState: location.state,
        documentReferrer: document.referrer,
        targetRoute,
        preserveFilters,
      },
    });
 
    // Determine target route
    let finalTargetRoute = targetRoute;
    if (!finalTargetRoute) {
      if (isFromReadingQueue) {
        finalTargetRoute = '/queue';
      } else {
        finalTargetRoute = fallbackRoute;
      }
    }
 
    // Preserve URL parameters when navigating back
    if (preserveFilters && (finalTargetRoute === '/inbox' || finalTargetRoute === '/queue')) {
      const currentParams = new URLSearchParams();
 
      // Preserve all existing URL parameters
      for (const [key, value] of searchParams.entries()) {
        currentParams.set(key, value);
      }
 
      const paramString = currentParams.toString();
      const finalUrl = paramString ? `${finalTargetRoute}?${paramString}` : finalTargetRoute;
 
      navigate(finalUrl, {
        replace: true,
      });
    } else {
      // Navigate directly without preserving params
      navigate(finalTargetRoute, {
        replace: true,
      });
    }
  }, [navigate, location.state, log, searchParams, targetRoute, fallbackRoute, preserveFilters, isFromReadingQueue]);
 
  const defaultClasses = "px-4 py-2 text-sm font-medium text-neutral-700 dark:text-slate-200 hover:bg-neutral-100 dark:hover:bg-neutral-800/60 rounded-md flex items-center gap-1.5";
 
  return (
    <button
      onClick={handleBack}
      className={`${defaultClasses} ${className}`}
    >
      {children || <ArrowLeft className="h-4 w-4" />}
      {getBackButtonText()}
    </button>
  );
});
 
BackButton.displayName = 'BackButton';
 
export default BackButton;