import { useCallback, useEffect, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react' import { Bot, FileText, Sparkles, Play, type LucideIcon } from 'lucide-react' import { SWIPE_COMMIT_DISTANCE, useTouchTapSelect } from '@/hooks/useTouchTapSelect' import type { MatchRange } from '@/lib/fuzzyMatch' export type SuggestionKind = 'command' | 'agent' | 'skill' | 'file' export interface PromptSuggestion { key: string value: T kind: SuggestionKind label: string ranges: MatchRange[] detail?: string tag?: string } interface PromptSuggestionsProps { isOpen: boolean items: PromptSuggestion[] selectedIndex: number takeover: boolean onSelect: (value: T) => void onSwipeRight?: (value: T) => void onClose: () => void } const TAKEOVER_TOP_GAP_PX = 8 const TAKEOVER_MIN_HEIGHT_PX = 160 const KIND_ICONS: Partial> = { agent: Bot, skill: Sparkles, file: FileText, } export function PromptSuggestions({ isOpen, items, selectedIndex, takeover, onSelect, onSwipeRight, onClose, }: PromptSuggestionsProps) { const listRef = useRef(null) const [expandedKey, setExpandedKey] = useState(null) const isVisible = isOpen && items.length > 0 const takeoverHeight = useTakeoverHeight(listRef, isVisible && takeover) const toggleExpanded = useCallback((value: T) => { const key = items.find((item) => item.value === value)?.key ?? null setExpandedKey((current) => (current === key ? null : key)) navigator.vibrate?.(10) }, [items]) const touch = useTouchTapSelect(onSelect, { onSwipeRight, onLongPress: takeover ? toggleExpanded : undefined }) useEffect(() => { if (!isOpen) return const handleClickOutside = (e: MouseEvent) => { if (listRef.current && !listRef.current.contains(e.target as Node)) { onClose() } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, [isOpen, onClose]) useEffect(() => { if (!isVisible || !listRef.current) return const selectedItem = listRef.current.querySelector(`[data-index="${selectedIndex}"]`) selectedItem?.scrollIntoView?.({ block: 'nearest' }) }, [selectedIndex, isVisible]) useLayoutEffect(() => { if (!isVisible || !takeover || !listRef.current) return listRef.current.scrollTop = listRef.current.scrollHeight }, [isVisible, takeover, items]) if (!isVisible) return null const rows = items.map((item, index) => { const isSelected = index === selectedIndex const isExpanded = item.key === expandedKey const swipeOffset = touch.swipe?.item === item.value ? touch.swipe.offset : 0 const Icon = KIND_ICONS[item.kind] return (
{swipeOffset > 0 && (
= SWIPE_COMMIT_DISTANCE ? 'bg-success text-success-foreground' : 'bg-success/40 text-foreground'}`}> RUN
)}
) }) if (takeover) { return (
{rows.reverse()}
) } return (
{rows}
) } function renderHighlighted(label: string, ranges: MatchRange[]): ReactNode { if (ranges.length === 0) return label const parts: ReactNode[] = [] let cursor = 0 for (const [start, end] of ranges) { if (start > cursor) parts.push(label.slice(cursor, start)) parts.push( {label.slice(start, end)} , ) cursor = end } if (cursor < label.length) parts.push(label.slice(cursor)) return parts } function useTakeoverHeight(listRef: RefObject, enabled: boolean) { const [height, setHeight] = useState(TAKEOVER_MIN_HEIGHT_PX) useLayoutEffect(() => { if (!enabled) return const anchor = listRef.current?.parentElement if (!anchor) return const boundary = findClippingAncestor(anchor) const measure = () => { const boundaryTop = boundary ? boundary.getBoundingClientRect().top : (window.visualViewport?.offsetTop ?? 0) const available = anchor.getBoundingClientRect().top - boundaryTop - TAKEOVER_TOP_GAP_PX setHeight(Math.max(TAKEOVER_MIN_HEIGHT_PX, available)) } measure() const resizeObserver = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(measure) resizeObserver?.observe(anchor) if (boundary) resizeObserver?.observe(boundary) const viewport = window.visualViewport viewport?.addEventListener('resize', measure) viewport?.addEventListener('scroll', measure) return () => { resizeObserver?.disconnect() viewport?.removeEventListener('resize', measure) viewport?.removeEventListener('scroll', measure) } }, [enabled, listRef]) return height } function findClippingAncestor(element: HTMLElement): HTMLElement | null { for (let current = element.parentElement; current; current = current.parentElement) { const { overflowY } = getComputedStyle(current) if (overflowY === 'hidden' || overflowY === 'auto' || overflowY === 'scroll' || overflowY === 'clip') return current } return null }