"use client"; import { useState, useEffect, useRef, forwardRef } from "react"; import { motion, AnimatePresence } from "framer-motion"; import ActionSearchBar from "@/app/_components/action-search-bar"; export interface Action { id: string label: string icon: React.ReactNode description?: string shortcut?: string category?: string onClick?: () => void } export interface ActionSearchBarProps { actions?: Action[] defaultActions?: boolean autoFocus?: boolean isFloating?: boolean placeholder?: string onSearch?: (query: string) => void onActionSelect?: (action: Action) => void className?: string inputClassName?: string dropdownClassName?: string showShortcutHint?: boolean commandKey?: string } export const FloatingActionSearchBar = forwardRef( ( { actions, defaultActions = true, autoFocus = false, isFloating = false, placeholder = "What's up?", onSearch, onActionSelect, className, inputClassName, dropdownClassName, showShortcutHint = true, commandKey = "⌘K", }, ref, ) => { const [isOpen, setIsOpen] = useState(false); const searchBarRef = useRef(null); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if ((event.ctrlKey || event.metaKey) && event.key === "k") { event.preventDefault(); setIsOpen((prev) => !prev); } else if (event.key === "Escape") { setIsOpen(false); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, []); useEffect(() => { if (isOpen && searchBarRef.current) { searchBarRef.current.focus(); } }, [isOpen]); return ( {isOpen && ( setIsOpen(false)} > e.stopPropagation()} > )} ); });