import { useEffect, useRef } from "react"; import { LoaderCircle } from "lucide-react"; import { Avatar, AvatarFallback } from "@/src/components/ui/avatar"; import { Command, CommandEmpty, CommandGroup, CommandItem, CommandList, } from "@/src/components/ui/command"; interface User { id: string; name: string | null; email: string | null; } interface MentionAutocompleteProps { users: User[]; isLoading: boolean; selectedIndex: number; onSelect: (userId: string, displayName: string) => void; onClose: () => void; onSelectedIndexChange: (index: number) => void; } export function MentionAutocomplete({ users, isLoading, selectedIndex, onSelect, onClose: _onClose, onSelectedIndexChange, }: MentionAutocompleteProps) { const selectedItemRef = useRef(null); // Scroll selected item into view useEffect(() => { selectedItemRef.current?.scrollIntoView({ block: "nearest" }); }, [selectedIndex]); // Get the currently selected user's ID for Command's value prop const selectedUserId = users[selectedIndex]?.id; // Limit displayed users to first 3 const MAX_DISPLAYED_USERS = 3; const displayedUsers = users.slice(0, MAX_DISPLAYED_USERS); const remainingCount = users.length - MAX_DISPLAYED_USERS; return (
{isLoading && (
Loading users...
)} {!isLoading && users.length === 0 && ( No users found )} {!isLoading && users.length > 0 && ( <> {displayedUsers.map((user, index) => { const displayName = user.name || user.email || "User"; const isSelected = index === selectedIndex; return ( onSelect(user.id, displayName)} onMouseEnter={() => onSelectedIndexChange(index)} ref={isSelected ? selectedItemRef : null} role="option" aria-selected={isSelected} id={user.id} >
{user.name || "Unknown"}
{user.email && (
{user.email}
)}
); })}
{remainingCount > 0 && (
and {remainingCount} more...
)} )}
); }