All files / src/components FloatingHelpButton.tsx

10.52% Statements 2/19
0% Branches 0/8
0% Functions 0/2
11.11% Lines 2/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 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                          1x                                                                           1x                                                                                              
/**
 * FloatingHelpButton Component
 *
 * A floating help button fixed in the top-right corner of the screen.
 * Automatically determines the help path based on the current route.
 */
 
import React from 'react';
import { Link, useLocation } from 'react-router-dom';
import { HelpCircle } from 'lucide-react';
import { useTranslation } from 'react-i18next';
 
// Map routes to their help paths
const routeToHelpPath: Record<string, string> = {
  '/': '/help/dashboard',
  '/dashboard': '/help/dashboard',
  '/scheduler': '/help/scheduler',
  '/tasks': '/help/tasks',
  '/customers': '/help/customers',
  '/services': '/help/services',
  '/resources': '/help/resources',
  '/staff': '/help/staff',
  '/time-blocks': '/help/time-blocks',
  '/my-availability': '/help/time-blocks',
  '/messages': '/help/messages',
  '/tickets': '/help/ticketing',
  '/payments': '/help/payments',
  '/contracts': '/help/contracts',
  '/contracts/templates': '/help/contracts',
  '/plugins': '/help/plugins',
  '/plugins/marketplace': '/help/plugins',
  '/plugins/my-plugins': '/help/plugins',
  '/plugins/create': '/help/plugins/create',
  '/settings': '/help/settings/general',
  '/settings/general': '/help/settings/general',
  '/settings/resource-types': '/help/settings/resource-types',
  '/settings/booking': '/help/settings/booking',
  '/settings/appearance': '/help/settings/appearance',
  '/settings/email': '/help/settings/email',
  '/settings/domains': '/help/settings/domains',
  '/settings/api': '/help/settings/api',
  '/settings/auth': '/help/settings/auth',
  '/settings/billing': '/help/settings/billing',
  '/settings/quota': '/help/settings/quota',
  // Platform routes
  '/platform/dashboard': '/help/dashboard',
  '/platform/businesses': '/help/dashboard',
  '/platform/users': '/help/staff',
  '/platform/tickets': '/help/ticketing',
};
 
const FloatingHelpButton: React.FC = () => {
  const { t } = useTranslation();
  const location = useLocation();
 
  // Get the help path for the current route
  const getHelpPath = (): string => {
    // Exact match first
    if (routeToHelpPath[location.pathname]) {
      return routeToHelpPath[location.pathname];
    }
 
    // Try matching with a prefix (for dynamic routes like /customers/:id)
    const pathSegments = location.pathname.split('/').filter(Boolean);
    if (pathSegments.length > 0) {
      // Try progressively shorter paths
      for (let i = pathSegments.length; i > 0; i--) {
        const testPath = '/' + pathSegments.slice(0, i).join('/');
        if (routeToHelpPath[testPath]) {
          return routeToHelpPath[testPath];
        }
      }
    }
 
    // Default to the main help guide
    return '/help';
  };
 
  const helpPath = getHelpPath();
 
  // Don't show on help pages themselves
  if (location.pathname.startsWith('/help')) {
    return null;
  }
 
  return (
    <Link
      to={helpPath}
      className="fixed top-20 right-4 z-50 inline-flex items-center justify-center w-10 h-10 bg-white dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:text-brand-600 dark:hover:text-brand-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-full shadow-lg border border-gray-200 dark:border-gray-700 transition-all duration-200 hover:scale-110"
      title={t('common.help', 'Help')}
      aria-label={t('common.help', 'Help')}
    >
      <HelpCircle size={20} />
    </Link>
  );
};
 
export default FloatingHelpButton;