All files / src/components/admin/KeyboardShortcutsHelp index.tsx

0% Statements 0/198
100% Branches 0/0
0% Functions 0/1
0% Lines 0/198

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                                                                                                                                                                                                                                                                                                                                                                                             
'use client';

import { useState, useEffect, useCallback } from 'react';
import { Icon } from '@/components/ui/icons';
import { cn } from '@/lib/core';
import {
  KeyboardShortcut,
  getShortcutKeys,
  groupShortcutsByCategory,
} from '@/hooks/useKeyboardShortcuts';

interface KeyboardShortcutsHelpProps {
  /** List of shortcuts to display */
  shortcuts: KeyboardShortcut[];
  /** Whether the modal is open */
  isOpen?: boolean;
  /** Callback when modal should close */
  onClose?: () => void;
  /** Allow opening with ? key */
  enableHotkey?: boolean;
  /** Optional className for custom styling */
  className?: string;
}

/**
 * Keyboard shortcuts help modal.
 *
 * Displays all available keyboard shortcuts grouped by category.
 * Can be triggered by pressing '?' or controlled programmatically.
 *
 * @example
 * ```tsx
 * <KeyboardShortcutsHelp
 *   shortcuts={adminShortcuts}
 *   enableHotkey
 * />
 * ```
 */
export function KeyboardShortcutsHelp({
  shortcuts,
  isOpen: controlledIsOpen,
  onClose,
  enableHotkey = true,
  className,
}: KeyboardShortcutsHelpProps) {
  const [internalIsOpen, setInternalIsOpen] = useState(false);
  const isOpen = controlledIsOpen ?? internalIsOpen;

  const handleClose = useCallback(() => {
    if (onClose) {
      onClose();
    } else {
      setInternalIsOpen(false);
    }
  }, [onClose]);

  const handleOpen = useCallback(() => {
    if (controlledIsOpen === undefined) {
      setInternalIsOpen(true);
    }
  }, [controlledIsOpen]);

  // Listen for ? key to open and Escape to close
  useEffect(() => {
    if (!enableHotkey) return;

    const handleKeyDown = (e: KeyboardEvent) => {
      // Don't trigger when typing in inputs
      const target = e.target as HTMLElement;
      if (
        target instanceof HTMLInputElement ||
        target instanceof HTMLTextAreaElement ||
        target.isContentEditable
      ) {
        return;
      }

      if (e.key === '?' && !e.ctrlKey && !e.metaKey && !e.altKey) {
        e.preventDefault();
        handleOpen();
      }

      if (e.key === 'Escape' && isOpen) {
        e.preventDefault();
        handleClose();
      }
    };

    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [enableHotkey, isOpen, handleClose, handleOpen]);

  // Handle click outside
  const handleBackdropClick = (e: React.MouseEvent) => {
    if (e.target === e.currentTarget) {
      handleClose();
    }
  };

  if (!isOpen) return null;

  const groupedShortcuts = groupShortcutsByCategory(shortcuts);
  const categories = Array.from(groupedShortcuts.keys());

  return (
    <div
      className={cn(
        'fixed inset-0 z-50 flex items-center justify-center bg-black/50',
        'animate-in fade-in duration-200',
        className
      )}
      onClick={handleBackdropClick}
      role="dialog"
      aria-modal="true"
      aria-labelledby="keyboard-shortcuts-title"
    >
      <div
        className={cn(
          'bg-white dark:bg-gray-900 rounded-lg shadow-xl max-w-lg w-full mx-4',
          'animate-in zoom-in-95 duration-200'
        )}
      >
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-800">
          <div className="flex items-center gap-2">
            <Icon name="terminal" className="h-5 w-5 text-gray-600 dark:text-gray-400" />
            <h2
              id="keyboard-shortcuts-title"
              className="font-semibold text-gray-900 dark:text-white"
            >
              Keyboard Shortcuts
            </h2>
          </div>
          <button
            onClick={handleClose}
            className="p-1 rounded-md hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors"
            aria-label="Close"
          >
            <Icon name="x" className="h-5 w-5 text-gray-500 dark:text-gray-400" />
          </button>
        </div>

        {/* Content */}
        <div className="p-4 max-h-96 overflow-y-auto">
          {categories.map((category) => (
            <div key={category} className="mb-6 last:mb-0">
              <h3 className="text-sm font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wide mb-3">
                {category}
              </h3>
              <div className="space-y-2">
                {groupedShortcuts.get(category)?.map((shortcut, index) => (
                  <ShortcutRow key={`${shortcut.key}-${index}`} shortcut={shortcut} />
                ))}
              </div>
            </div>
          ))}
        </div>

        {/* Footer */}
        <div className="p-4 border-t border-gray-200 dark:border-gray-800 text-center">
          <p className="text-sm text-gray-500 dark:text-gray-400">
            Press <kbd className="px-2 py-1 bg-gray-100 dark:bg-gray-800 rounded text-xs font-mono">?</kbd> anywhere to show this dialog
          </p>
        </div>
      </div>
    </div>
  );
}

interface ShortcutRowProps {
  shortcut: KeyboardShortcut;
}

function ShortcutRow({ shortcut }: ShortcutRowProps) {
  const keys = getShortcutKeys(shortcut);

  return (
    <div className="flex items-center justify-between py-2">
      <span className="text-sm text-gray-700 dark:text-gray-300">
        {shortcut.description}
      </span>
      <div className="flex items-center gap-1">
        {keys.map((key, index) => (
          <span key={index}>
            <kbd className="inline-flex items-center justify-center min-w-[1.5rem] px-2 py-1 text-xs font-mono bg-gray-100 dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded shadow-sm">
              {key}
            </kbd>
            {index < keys.length - 1 && (
              <span className="mx-0.5 text-gray-400">+</span>
            )}
          </span>
        ))}
      </div>
    </div>
  );
}

export default KeyboardShortcutsHelp;