|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { motion, AnimatePresence } from "framer-motion"; |
| 4 | +import { X, Keyboard, Zap, FolderOpen, Edit3, Compass, Eye, Command, Info } from "lucide-react"; |
| 5 | +import { Shortcut, formatShortcutKey } from "@/hooks/useKeyboardShortcuts"; |
| 6 | + |
| 7 | +interface KeyboardShortcutsModalProps { |
| 8 | + isOpen: boolean; |
| 9 | + onClose: () => void; |
| 10 | + shortcuts: Shortcut[]; |
| 11 | +} |
| 12 | + |
| 13 | +const categoryConfig = { |
| 14 | + transport: { label: "Transport", icon: Zap, color: "text-green-400", bg: "bg-green-500/10" }, |
| 15 | + file: { label: "File", icon: FolderOpen, color: "text-blue-400", bg: "bg-blue-500/10" }, |
| 16 | + edit: { label: "Edit", icon: Edit3, color: "text-yellow-400", bg: "bg-yellow-500/10" }, |
| 17 | + view: { label: "View", icon: Eye, color: "text-pink-400", bg: "bg-pink-500/10" }, |
| 18 | + navigation: { label: "Navigation", icon: Compass, color: "text-purple-400", bg: "bg-purple-500/10" }, |
| 19 | +}; |
| 20 | + |
| 21 | +export function KeyboardShortcutsModal({ isOpen, onClose, shortcuts }: KeyboardShortcutsModalProps) { |
| 22 | + const isMac = typeof navigator !== "undefined" && navigator.platform.toUpperCase().indexOf("MAC") >= 0; |
| 23 | + |
| 24 | + const groupedShortcuts = shortcuts.reduce((acc, shortcut) => { |
| 25 | + if (!acc[shortcut.category]) { |
| 26 | + acc[shortcut.category] = []; |
| 27 | + } |
| 28 | + // Skip duplicate shortcuts (like Delete and Backspace) |
| 29 | + if (!acc[shortcut.category].some(s => s.description === shortcut.description)) { |
| 30 | + acc[shortcut.category].push(shortcut); |
| 31 | + } |
| 32 | + return acc; |
| 33 | + }, {} as Record<string, Shortcut[]>); |
| 34 | + |
| 35 | + const categoryOrder: (keyof typeof categoryConfig)[] = ["transport", "file", "edit", "view", "navigation"]; |
| 36 | + |
| 37 | + return ( |
| 38 | + <AnimatePresence> |
| 39 | + {isOpen && ( |
| 40 | + <> |
| 41 | + {/* Backdrop */} |
| 42 | + <motion.div |
| 43 | + initial={{ opacity: 0 }} |
| 44 | + animate={{ opacity: 1 }} |
| 45 | + exit={{ opacity: 0 }} |
| 46 | + onClick={onClose} |
| 47 | + className="fixed inset-0 bg-black/70 backdrop-blur-md z-50" |
| 48 | + /> |
| 49 | + |
| 50 | + {/* Modal */} |
| 51 | + <motion.div |
| 52 | + initial={{ opacity: 0, scale: 0.9, y: 40 }} |
| 53 | + animate={{ opacity: 1, scale: 1, y: 0 }} |
| 54 | + exit={{ opacity: 0, scale: 0.9, y: 40 }} |
| 55 | + transition={{ type: "spring", damping: 25, stiffness: 300 }} |
| 56 | + className="fixed inset-4 md:inset-auto md:left-1/2 md:top-1/2 md:-translate-x-1/2 md:-translate-y-1/2 md:w-[964px] md:max-h-[84vh] bg-linear-to-br from-background via-background to-muted/20 border border-border/50 rounded-3xl shadow-2xl z-50 overflow-hidden flex flex-col" |
| 57 | + > |
| 58 | + {/* Header */} |
| 59 | + <div className="relative p-6 pb-4 border-b border-border/50"> |
| 60 | + {/* Decorative gradient */} |
| 61 | + <div className="absolute inset-0 bg-linear-to-r from-primary/5 via-purple-500/5 to-pink-500/5 pointer-events-none" /> |
| 62 | + |
| 63 | + <div className="relative flex items-start justify-between"> |
| 64 | + <div className="flex items-center gap-4"> |
| 65 | + <motion.div |
| 66 | + initial={{ rotate: -10 }} |
| 67 | + animate={{ rotate: 0 }} |
| 68 | + className="p-3 bg-linear-to-br from-primary/20 to-purple-500/20 rounded-2xl border border-primary/20" |
| 69 | + > |
| 70 | + <Keyboard className="text-primary" size={28} /> |
| 71 | + </motion.div> |
| 72 | + <div> |
| 73 | + <h2 className="text-2xl font-black tracking-tight">Keyboard Shortcuts</h2> |
| 74 | + <p className="text-sm text-muted-foreground mt-1 flex items-center gap-2"> |
| 75 | + <span>Speed up your workflow with these shortcuts</span> |
| 76 | + <span className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full bg-muted text-xs font-medium"> |
| 77 | + <Command size={10} /> |
| 78 | + {isMac ? "Mac" : "Windows/Linux"} |
| 79 | + </span> |
| 80 | + </p> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <button |
| 84 | + onClick={onClose} |
| 85 | + aria-label="Close shortcuts modal" |
| 86 | + className="p-2 hover:bg-muted rounded-xl transition-all hover:scale-105 active:scale-95" |
| 87 | + > |
| 88 | + <X size={20} /> |
| 89 | + </button> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + |
| 93 | + {/* Content - 2 col on desktop, 1 col on mobile */} |
| 94 | + <div className="flex-1 overflow-y-auto p-6"> |
| 95 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> |
| 96 | + {categoryOrder.map((key) => { |
| 97 | + const config = categoryConfig[key]; |
| 98 | + const categoryShortcuts = groupedShortcuts[key]; |
| 99 | + if (!categoryShortcuts?.length) return null; |
| 100 | + |
| 101 | + const Icon = config.icon; |
| 102 | + |
| 103 | + return ( |
| 104 | + <motion.div |
| 105 | + key={key} |
| 106 | + initial={{ opacity: 0, y: 20 }} |
| 107 | + animate={{ opacity: 1, y: 0 }} |
| 108 | + transition={{ delay: categoryOrder.indexOf(key) * 0.05 }} |
| 109 | + className="space-y-3" |
| 110 | + > |
| 111 | + {/* Category Header */} |
| 112 | + <div className={`flex items-center gap-2 ${config.color}`}> |
| 113 | + <div className={`p-1.5 rounded-lg ${config.bg}`}> |
| 114 | + <Icon size={14} /> |
| 115 | + </div> |
| 116 | + <h3 className="text-sm font-bold uppercase tracking-wider"> |
| 117 | + {config.label} |
| 118 | + </h3> |
| 119 | + <div className="flex-1 h-px bg-current opacity-20" /> |
| 120 | + </div> |
| 121 | + |
| 122 | + {/* Shortcuts List */} |
| 123 | + <div className="space-y-2"> |
| 124 | + {categoryShortcuts.map((shortcut, idx) => ( |
| 125 | + <motion.div |
| 126 | + key={idx} |
| 127 | + initial={{ opacity: 0, x: -10 }} |
| 128 | + animate={{ opacity: 1, x: 0 }} |
| 129 | + transition={{ delay: categoryOrder.indexOf(key) * 0.05 + idx * 0.02 }} |
| 130 | + className="group flex items-center justify-between px-3 py-1.5 bg-muted/20 hover:bg-muted/40 rounded-xl transition-all duration-200 border border-transparent hover:border-border/50" |
| 131 | + > |
| 132 | + <span className="text-sm text-foreground/90 group-hover:text-foreground transition-colors"> |
| 133 | + {shortcut.description} |
| 134 | + </span> |
| 135 | + <kbd className="px-2.5 py-1 bg-background border border-border rounded-lg font-mono text-xs shadow-sm group-hover:shadow-md group-hover:border-primary/30 transition-all min-w-[60px] text-center"> |
| 136 | + {formatShortcutKey(shortcut)} |
| 137 | + </kbd> |
| 138 | + </motion.div> |
| 139 | + ))} |
| 140 | + </div> |
| 141 | + </motion.div> |
| 142 | + ); |
| 143 | + })} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + |
| 147 | + {/* Footer with tips */} |
| 148 | + <div className="p-4 border-t border-border/50 bg-muted/10"> |
| 149 | + <div className="flex flex-col md:flex-row md:items-center md:justify-between gap-3"> |
| 150 | + <div className="flex items-start gap-2 text-xs text-muted-foreground"> |
| 151 | + <Info size={14} className="mt-0.5 shrink-0" /> |
| 152 | + <div> |
| 153 | + <p className="font-medium text-foreground/70">Pro Tips:</p> |
| 154 | + <ul className="mt-1 space-y-0.5"> |
| 155 | + <li>• Use number keys <kbd className="px-1 bg-background border border-border rounded text-[10px]">1-5</kbd> for quick navigation</li> |
| 156 | + <li>• Press <kbd className="px-1 bg-background border border-border rounded text-[10px]">Space</kbd> anywhere to toggle playback</li> |
| 157 | + </ul> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + <div className="text-xs text-muted-foreground text-right"> |
| 161 | + Press <kbd className="px-1.5 py-0.5 bg-background border border-border rounded mx-1">Esc</kbd> to close |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + </motion.div> |
| 166 | + </> |
| 167 | + )} |
| 168 | + </AnimatePresence> |
| 169 | + ); |
| 170 | +} |
0 commit comments