'use client' import { useState, useRef, useCallback, useEffect, KeyboardEvent } from 'react' import api from '@/services/api' interface User { id: string username: string avatar?: string } interface Hashtag { name: string count?: number } interface MentionInputProps { value: string onChange: (value: string) => void placeholder?: string className?: string maxLength?: number rows?: number onSubmit?: () => void } export default function MentionInput({ value, onChange, placeholder = "What's happening?", className = '', maxLength, rows = 3, onSubmit, }: MentionInputProps) { const textareaRef = useRef(null) const [showSuggestions, setShowSuggestions] = useState(false) const [suggestionType, setSuggestionType] = useState<'mention' | 'hashtag' | null>(null) const [suggestions, setSuggestions] = useState<(User | Hashtag)[]>([]) const [selectedIndex, setSelectedIndex] = useState(0) const [searchQuery, setSearchQuery] = useState('') const [triggerPosition, setTriggerPosition] = useState({ start: 0, end: 0 }) const [loading, setLoading] = useState(false) // Detect @ or # trigger const detectTrigger = useCallback(() => { const textarea = textareaRef.current if (!textarea) return const cursorPos = textarea.selectionStart const textBeforeCursor = value.substring(0, cursorPos) // Find the last @ or # before cursor const mentionMatch = textBeforeCursor.match(/@(\w*)$/) const hashtagMatch = textBeforeCursor.match(/#(\w*)$/) if (mentionMatch) { const query = mentionMatch[1] setSearchQuery(query) setSuggestionType('mention') setTriggerPosition({ start: cursorPos - query.length - 1, end: cursorPos, }) setShowSuggestions(true) searchUsers(query) } else if (hashtagMatch) { const query = hashtagMatch[1] setSearchQuery(query) setSuggestionType('hashtag') setTriggerPosition({ start: cursorPos - query.length - 1, end: cursorPos, }) setShowSuggestions(true) searchHashtags(query) } else { setShowSuggestions(false) setSuggestions([]) } }, [value]) // Debounce timer ref — prevents firing API calls on every keystroke const debounceTimerRef = useRef | null>(null) const DEBOUNCE_MS = 250 /** * Debounced wrapper: cancels any pending call and schedules a new one. * Adapted from munia's search-for-mention pattern with a debounce guard. */ const debouncedSearch = useCallback( (fn: (q: string) => Promise, query: string) => { if (debounceTimerRef.current) { clearTimeout(debounceTimerRef.current) } debounceTimerRef.current = setTimeout(() => { fn(query) }, DEBOUNCE_MS) }, [] ) // Clean up the timer on unmount useEffect(() => { return () => { if (debounceTimerRef.current) clearTimeout(debounceTimerRef.current) } }, []) // Search users for @mentions const searchUsersImpl = async (query: string) => { if (query.length < 1) { setSuggestions([]) return } setLoading(true) try { const { data } = await api.get('/search/users', { params: { q: query, limit: 5 } }) setSuggestions(data.users || data || []) setSelectedIndex(0) } catch (error) { console.error('Search users error:', error) setSuggestions([]) } finally { setLoading(false) } } const searchUsers = (query: string) => debouncedSearch(searchUsersImpl, query) // Search hashtags for #tags const searchHashtagsImpl = async (query: string) => { if (query.length < 1) { // Show popular hashtags try { const { data } = await api.get('/tags/trending', { params: { limit: 5 } }) setSuggestions(data.hashtags || data || []) } catch { setSuggestions([]) } return } setLoading(true) try { const { data } = await api.get('/tags/search', { params: { q: query, limit: 5 } }) setSuggestions(data.hashtags || data || []) setSelectedIndex(0) } catch (error) { console.error('Search hashtags error:', error) setSuggestions([]) } finally { setLoading(false) } } const searchHashtags = (query: string) => debouncedSearch(searchHashtagsImpl, query) // Insert selected suggestion const insertSuggestion = useCallback((suggestion: User | Hashtag) => { const prefix = suggestionType === 'mention' ? '@' : '#' const insertText = suggestionType === 'mention' ? (suggestion as User).username : (suggestion as Hashtag).name const newValue = value.substring(0, triggerPosition.start) + prefix + insertText + ' ' + value.substring(triggerPosition.end) onChange(newValue) setShowSuggestions(false) setSuggestions([]) // Focus and move cursor setTimeout(() => { if (textareaRef.current) { const newCursorPos = triggerPosition.start + prefix.length + insertText.length + 1 textareaRef.current.focus() textareaRef.current.setSelectionRange(newCursorPos, newCursorPos) } }, 0) }, [value, onChange, triggerPosition, suggestionType]) // Handle keyboard navigation const handleKeyDown = (e: KeyboardEvent) => { if (showSuggestions && suggestions.length > 0) { switch (e.key) { case 'ArrowDown': e.preventDefault() setSelectedIndex(prev => (prev + 1) % suggestions.length) break case 'ArrowUp': e.preventDefault() setSelectedIndex(prev => (prev - 1 + suggestions.length) % suggestions.length) break case 'Enter': if (showSuggestions) { e.preventDefault() insertSuggestion(suggestions[selectedIndex]) } break case 'Escape': setShowSuggestions(false) break case 'Tab': if (showSuggestions) { e.preventDefault() insertSuggestion(suggestions[selectedIndex]) } break } } else if (e.key === 'Enter' && (e.metaKey || e.ctrlKey) && onSubmit) { e.preventDefault() onSubmit() } } // Detect trigger on input change useEffect(() => { detectTrigger() }, [value, detectTrigger]) return (