ai-bot/app/_ui/Search.tsx

104 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2025-01-16 18:30:24 +08:00
"use client";
import { faSearch } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import clsx from "clsx";
2025-02-07 19:02:22 +08:00
import { useEffect, useMemo, useState } from "react";
2025-01-16 18:30:24 +08:00
import Logo from "./Logo";
2025-02-07 19:02:22 +08:00
import { useRequest } from "ahooks";
import { mRequest } from "../_lib/request";
import { SearchTypeItem, SearchWayItemType } from "../_lib/data/search";
import { doSearch } from "../_lib/utils";
import { log } from "node:console";
2025-01-16 18:30:24 +08:00
export default function Search() {
2025-02-07 19:02:22 +08:00
const { data: searchWayList = [] } = useRequest(() => mRequest<{ list: SearchWayItemType[] }>('GET', '/api/search?page=1&pageSize=999').then(res => res.list))
const { data: searchTypeList = [] } = useRequest(() => mRequest<{ list: SearchTypeItem[] }>('GET', '/api/searchType?page=1&pageSize=999').then(res => res.list))
const [selectKey, setSelectKey] = useState<string | null>(null)
const [activeSearchKey, setActiveSearchKey] = useState<string | null>(null)
const [inputStr, setInputStr] = useState('')
2025-01-16 18:30:24 +08:00
const nowSelectConfig = useMemo(() => {
2025-02-07 19:02:22 +08:00
const idx = searchTypeList.findIndex(val => val._id === selectKey)
if (idx !== -1) return searchTypeList[idx]
2025-01-16 18:30:24 +08:00
else return null
2025-02-07 19:02:22 +08:00
}, [selectKey, searchTypeList])
const filterSeachList = useMemo(() => {
return searchWayList.filter(val => nowSelectConfig?.includes.includes(val._id))
}, [searchWayList, nowSelectConfig])
2025-01-16 18:30:24 +08:00
const activeSearch = useMemo(() => {
2025-02-07 19:02:22 +08:00
const idx = searchWayList.findIndex(val => val._id === activeSearchKey)
2025-01-16 18:30:24 +08:00
if (idx !== -1) {
2025-02-07 19:02:22 +08:00
return searchWayList[idx]
2025-01-16 18:30:24 +08:00
} else {
return null
}
2025-02-07 19:02:22 +08:00
}, [activeSearchKey, searchWayList])
useEffect(() => {
if (searchTypeList.length > 0) {
setSelectKey(searchTypeList[0]._id)
}
}, [searchTypeList])
useEffect(() => {
if (filterSeachList.length > 0) {
setActiveSearchKey(filterSeachList[0]._id)
}
}, [selectKey])
2025-01-16 18:30:24 +08:00
return (
<div className="w-full flex justify-center flex-col items-center py-10 h-[500px]">
2025-01-16 18:30:24 +08:00
<div className="w-[200px]">
<Logo></Logo>
</div>
<div className="w-full lg:w-[800px] flex flex-col gap-y-2">
2025-01-16 18:30:24 +08:00
<div className="flex w-full justify-center gap-x-5 text-[#666666]">
{
2025-02-07 19:02:22 +08:00
searchTypeList.map(item => (
<div key={item._id}
2025-01-16 18:30:24 +08:00
onClick={() => {
2025-02-07 19:02:22 +08:00
setSelectKey(item._id)
2025-01-16 18:30:24 +08:00
}}
2025-02-07 19:02:22 +08:00
className={clsx(item._id === selectKey ?
2025-01-16 18:30:24 +08:00
"text-[#333]" : "text-[#999] cursor-pointer")}>
{item.name}
</div>
))
}
</div>
<div className="w-full relative">
2025-02-07 19:02:22 +08:00
<input
onKeyDown={(e) => {
if (!activeSearch?.value) return
if (e.key === 'Enter') {
doSearch(activeSearch?.value, inputStr)
}
}}
type="text"
onChange={e => {
setInputStr(e.target.value)
}}
placeholder={`${activeSearch?.label}搜索`} className="w-full bg-[#C4C2C6] px-4 h-[50px] rounded-3xl outline-none" />
2025-01-16 18:30:24 +08:00
<FontAwesomeIcon className="text-white absolute top-1/2 -translate-y-1/2 right-6 text-xl" icon={faSearch}></FontAwesomeIcon>
</div>
<div className="w-full flex justify-center gap-x-4 ">
{
2025-02-07 19:02:22 +08:00
filterSeachList.map(item => (
<div key={item._id} onClick={() => {
setActiveSearchKey(item._id)
}} className={clsx(activeSearchKey === item._id ?
2025-01-16 18:30:24 +08:00
"text-[#333]" : "text-[#999] cursor-pointer")}>{item.label}</div>
))
}
</div>
2025-01-16 18:30:24 +08:00
</div>
</div>
);
}