import { useState, useEffect, useRef } from 'react'; interface User { id: string; username: string; display_name: string; } interface MentionPopupProps { users: User[]; filter: string; onSelect: (user: User) => void; position: { top: number; left: number }; } export function MentionPopup({ users, filter, onSelect, position }: MentionPopupProps) { const [selectedIndex, setSelectedIndex] = useState(0); const listRef = useRef(null); const filtered = users.filter(u => u.username.toLowerCase().includes(filter.toLowerCase()) || u.display_name?.toLowerCase().includes(filter.toLowerCase()) ).slice(0, 8); useEffect(() => { setSelectedIndex(0); }, [filter]); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'ArrowDown') { e.preventDefault(); setSelectedIndex(prev => Math.min(prev + 1, filtered.length - 1)); } else if (e.key === 'ArrowUp') { e.preventDefault(); setSelectedIndex(prev => Math.max(prev - 1, 0)); } else if (e.key === 'Enter' || e.key === 'Tab') { e.preventDefault(); if (filtered[selectedIndex]) { onSelect(filtered[selectedIndex]); } } else if (e.key === 'Escape') { e.preventDefault(); onSelect(null as any); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [filtered, selectedIndex, onSelect]); if (filtered.length === 0) return null; return (
MEMBERS
{filtered.map((user, index) => ( ))}
); }