import React from 'react' import Mentions from 'rc-mentions' import { listPeople, searchPeople } from '@/handlers/api/people.handler' import { ArrowRight, Loader2, Search, X } from 'lucide-react' import { cn } from '@/lib/utils' interface FindInputProps { onSearch: (query: string) => void; value: string; onChange: (value: string) => void; loading?: boolean; } export default function FindInput({ onSearch, value, onChange, loading }: FindInputProps) { const [options, setOptions] = React.useState<{ value: string; label: string }[]>([]) const [focused, setFocused] = React.useState(false) const nameToIdRef = React.useRef>({}) const handleMentionSearch = async (text: string, prefix: string) => { if (prefix !== '@') { setOptions([]) return } const people = text.length ? await searchPeople(text) : await listPeople({ page: 1, perPage: 50, sort: 'assetCount', sortOrder: 'desc' }).then((r) => r.people) people.forEach((person: any) => { if (person.name) nameToIdRef.current[person.name] = person.id }) setOptions( people .filter((person: any) => person.name) .map((person: any) => ({ value: person.name, label: person.name, })) ) } const handleClear = () => { onChange('') } const buildQueryWithIds = (displayValue: string) => { let query = displayValue for (const [name, id] of Object.entries(nameToIdRef.current)) { query = query.replaceAll(`@${name}`, `@${id}`) } return query } const handleSubmit = () => { if (value.trim()) { onSearch(buildQueryWithIds(value)) } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault() handleSubmit() } } return (
setFocused(true)} onBlur={() => setFocused(false)} options={options} rows={1} /> {value && !loading && ( )}
) }