import type { ChangeEventHandler, UIEventHandler } from 'react' import { Popover, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover' import { useCallback, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import Input from '@/app/components/base/input' import { useEducation } from './hooks' type SearchInputProps = { value?: string onChange: (value: string) => void } const SearchInput = ({ value, onChange }: SearchInputProps) => { const { t } = useTranslation() const [open, setOpen] = useState(false) const { schools, setSchools, querySchoolsWithDebounced, handleUpdateSchools, hasNext } = useEducation() const pageRef = useRef(0) const valueRef = useRef(value) const handleSearch = useCallback( (debounced?: boolean) => { const keywords = valueRef.current const page = pageRef.current if (debounced) { querySchoolsWithDebounced({ keywords, page, }) return } handleUpdateSchools({ keywords, page, }) }, [handleUpdateSchools, querySchoolsWithDebounced], ) const handleValueChange: ChangeEventHandler = useCallback( (e) => { setOpen(true) setSchools([]) pageRef.current = 0 const inputValue = e.target.value valueRef.current = inputValue onChange(inputValue) handleSearch(true) }, [handleSearch, onChange, setSchools], ) const handleScroll: UIEventHandler = useCallback( (e) => { const target = e.currentTarget const { scrollTop, scrollHeight, clientHeight } = target if (scrollTop + clientHeight >= scrollHeight - 5 && scrollTop > 0 && hasNext) { pageRef.current += 1 handleSearch() } }, [handleSearch, hasNext], ) return ( $['form.schoolName.placeholder'], { ns: 'education' })} value={value} onChange={handleValueChange} /> } /> {!!schools.length && !!value ? (
{schools.map((school) => (
{ onChange(school) setOpen(false) }} > {school}
))}
) : null}
) } export default SearchInput