import { useState, useEffect } from "react"; import { useEmployeesName } from "../../hooks/useEmployees"; import { useDebounce } from "../../utils/appUtils"; import { useController } from "react-hook-form"; import Avatar from "./Avatar"; const EmployeeSearchInput = ({ control, name, projectId,placeholder }) => { const { field: { onChange, value, ref }, fieldState: { error }, } = useController({ name, control }); const [search, setSearch] = useState(""); const [showDropdown, setShowDropdown] = useState(false); const debouncedSearch = useDebounce(search, 500); const { data: employees, isLoading, } = useEmployeesName(projectId, debouncedSearch); useEffect(() => { if (value && !search) { const found = employees?.data?.find((emp) => emp.id === value); if (found) setSearch(found.firstName + " " + found.lastName); } }, [value, employees]); const handleSelect = (employee) => { onChange(employee.id); setSearch(employee.firstName + " " + employee.lastName); setShowDropdown(false); }; return (