ai-bot/app/_ui/Search.tsx

120 lines
4.6 KiB
TypeScript
Raw 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";
2025-02-17 18:17:12 +08:00
import { getSearchTypeList, getSearchWayList, SearchWayItemType } from "../_lib/data/search";
2025-02-07 19:02:22 +08:00
import { doSearch } from "../_lib/utils";
2025-01-16 18:30:24 +08:00
2025-02-17 10:30:40 +08:00
const defaultSearchEngine: SearchWayItemType = {
_id: 'default',
label: '站内',
value: '/search?wd=%s',
fullName: '站内',
}
2025-01-16 18:30:24 +08:00
export default function Search() {
2025-02-07 19:02:22 +08:00
2025-02-17 10:30:40 +08:00
const { data: searchWayList = [] } = useRequest(() => getSearchWayList({ page: 1, pageSize: 9999 }).then(res => [defaultSearchEngine, ...res.list]))
const { data: searchTypeList = [] } = useRequest(() => getSearchTypeList({ page: 1, pageSize: 9999 }).then(res => res.list.map((val, idx) => {
if (idx === 0) {
return {
...val,
includes: ['default', ...val.includes]
}
}
return val
})))
2025-02-07 19:02:22 +08:00
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)
}
2025-02-17 18:17:12 +08:00
}, [filterSeachList, selectKey])
2025-01-16 18:30:24 +08:00
return (
2025-02-17 10:30:40 +08:00
<div className="w-full flex justify-center flex-col items-center py-10 h-[300px]">
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-17 10:30:40 +08:00
2025-01-16 18:30:24 +08:00
{
2025-02-17 10:30:40 +08:00
searchTypeList.map((item, idx) => (
2025-02-07 19:02:22 +08:00
<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-17 10:30:40 +08:00
className={clsx(item._id === selectKey || (idx === 0 && selectKey === 'default') ?
2025-01-16 18:30:24 +08:00
"text-[#333]" : "text-[#999] cursor-pointer")}>
{item.name}
</div>
))
2025-02-17 10:30:40 +08:00
2025-01-16 18:30:24 +08:00
}
2025-02-17 10:30:40 +08:00
2025-01-16 18:30:24 +08:00
</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)
}}
2025-02-17 10:30:40 +08:00
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-17 10:30:40 +08:00
2025-01-16 18:30:24 +08:00
{
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>
);
}