"use client"; import { useRef, useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { RiSearch2Line, RiAddCircleFill, RiCalendarCloseLine, RiLoader2Fill, } from "react-icons/ri"; import { useGlobalContext } from "@/providers/GlobalProvider"; import { getCategoryList, getEmployeeSearch } from "@/api/client"; import { CategoryListResult, EmployeeSearchResult } from "@/utils/clientsApis"; import { SetChatLastUsedForLocalStorage } from "@/utils/chat"; export default function Home() { const router = useRouter(); const boxRef = useRef(null); const { changeMenuIndex, listType, changeListType, agentId, changeAgentId, conversationId, changeConversationId, } = useGlobalContext(); const [categoryList, setCategoryList] = useState([]); const [employeeList, setEmployeeList] = useState([]); const [categoryId, setCategoryId] = useState(0); const [keyword, setKeyword] = useState(""); const [page, setPage] = useState(1); const [isBottom, setIsBottom] = useState(false); const page_getEmployeeSearch = () => { getEmployeeSearch({ page: page, pageSize: 20, categoryId: categoryId, title: keyword, }).then((data) => { if (data.code === 0 && data.data.data !== null) { setEmployeeList((prevEmployeeList) => [ ...prevEmployeeList, ...data.data.data, // Append new data to the existing list ]); if (data.data.total <= 20) { setIsBottom(true); } } else { setIsBottom(true); } }); }; const handleScroll = () => { if ( boxRef.current && boxRef.current.scrollHeight - boxRef.current.clientHeight === boxRef.current.scrollTop ) { setPage((prevPage) => { const nextPage = prevPage + 1; page_getEmployeeSearch(); // Load the next page return nextPage; }); } }; const selectCategory = (id: number) => { if (categoryId != id) { setIsBottom(false); setCategoryId(id); setPage(1); setEmployeeList([]); } }; const handleKeywordChange = (event: React.ChangeEvent) => { setKeyword(event.target.value); }; const handleSearch = () => { setIsBottom(false); setPage(1); setEmployeeList([]); }; const addChat = (id: number) => { changeListType(1); changeAgentId(id); changeConversationId(""); SetChatLastUsedForLocalStorage({listType:1,agentId:id,conversationId:''}); router.push("/dialogue"); }; const [isFocused, setIsFocused] = useState(false); const handleFocus = () => setIsFocused(true); const handleBlur = () => setIsFocused(false); useEffect(() => { changeMenuIndex(2); getCategoryList({ page: 1, pageSize: 20 }).then((data) => { if (data.code === 0) { setCategoryList(data.data.data); } }); page_getEmployeeSearch(); setPage(page + 1); }, [isBottom, categoryId, keyword]); return (
{ if (e.key === "Enter") { handleSearch(); } }} />
{categoryList !== null && categoryList.length > 0 && (
{categoryList.map((item) => ( ))}
)}
{employeeList !== null && employeeList.length > 0 && (
{isBottom && (
已显示全部智能体
)}
)} {employeeList === null || (employeeList.length === 0 && (
查询结果为空
))}
); }