mirror of
https://github.com/langgenius/dify.git
synced 2026-07-31 09:19:29 +08:00
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import type { SearchParams } from './types'
|
|
import { useDebounceFn } from 'ahooks'
|
|
import { useCallback, useState } from 'react'
|
|
import { useEducationAutocomplete } from '@/service/use-education'
|
|
|
|
export const useEducation = () => {
|
|
const { mutateAsync, isPending, data } = useEducationAutocomplete()
|
|
|
|
const [prevSchools, setPrevSchools] = useState<string[]>([])
|
|
const handleUpdateSchools = useCallback(
|
|
(searchParams: SearchParams) => {
|
|
if (searchParams.keywords) {
|
|
mutateAsync(searchParams).then((res) => {
|
|
const currentPage = searchParams.page || 0
|
|
const resSchools = res.data
|
|
if (currentPage > 0)
|
|
setPrevSchools((prevSchools) => [...(prevSchools || []), ...resSchools])
|
|
else setPrevSchools(resSchools)
|
|
})
|
|
}
|
|
},
|
|
[mutateAsync],
|
|
)
|
|
|
|
const { run: querySchoolsWithDebounced } = useDebounceFn(
|
|
(searchParams: SearchParams) => {
|
|
handleUpdateSchools(searchParams)
|
|
},
|
|
{
|
|
wait: 300,
|
|
},
|
|
)
|
|
|
|
return {
|
|
schools: prevSchools,
|
|
setSchools: setPrevSchools,
|
|
querySchoolsWithDebounced,
|
|
handleUpdateSchools,
|
|
isLoading: isPending,
|
|
hasNext: data?.has_next,
|
|
}
|
|
}
|