From 84a2d62180bd3ee50e760f6b857544b1a86c06a7 Mon Sep 17 00:00:00 2001
From: expdsn <18111002318@163.com>
Date: Tue, 25 Feb 2025 19:04:37 +0800
Subject: [PATCH] save

---
 app/(main)/detail/[id]/page.tsx |  3 +++
 app/(main)/search/ResultBox.tsx | 26 ++++++++++++++++++++------
 app/_lib/actions/search.ts      | 23 ++++++++++++++++++++++-
 app/_lib/data/linkType.ts       |  8 ++++++++
 app/_ui/Search.tsx              | 11 +++++++----
 5 files changed, 60 insertions(+), 11 deletions(-)

diff --git a/app/(main)/detail/[id]/page.tsx b/app/(main)/detail/[id]/page.tsx
index 4e664af..d8618e8 100644
--- a/app/(main)/detail/[id]/page.tsx
+++ b/app/(main)/detail/[id]/page.tsx
@@ -1,4 +1,5 @@
 import { getLinkList, getLinkListAll } from "@/app/_lib/data/link";
+import { getLinkType } from "@/app/_lib/data/linkType";
 import { LinkBlock } from "@/app/_ui/LinkListBox";
 import Search from "@/app/_ui/Search";
 import { use } from "react";
@@ -6,6 +7,7 @@ import { use } from "react";
 export default async function Page({ params }: { params: Promise<{ id: string }> }) {
 
     const id = (await params).id
+    const linkType = await getLinkType(id)
     const { list: linkList } = await getLinkList({ typeId: id })
 
     return (
@@ -13,6 +15,7 @@ export default async function Page({ params }: { params: Promise<{ id: string }>
             {/* <HeaderNav></HeaderNav> */}
             <main className="w-full p-5">
                 <Search></Search >
+                <h1 className="text-3xl font-bold mb-6 flex justify-center">{linkType?.label}</h1>
                 <div className=" grid grid-cols-3 lg:grid-cols-6 gap-4 ">
                     {
                         linkList.map(val => (
diff --git a/app/(main)/search/ResultBox.tsx b/app/(main)/search/ResultBox.tsx
index f029216..38c8396 100644
--- a/app/(main)/search/ResultBox.tsx
+++ b/app/(main)/search/ResultBox.tsx
@@ -1,10 +1,24 @@
-export default function ResultBox() {
-    
-    return <div className="h-[40vh] w-full px-4">
-        <div className="font-bold">搜索结果:</div>
-        <div className="pl-2 border-l-blue-400 border-0 border-l-2 my-2">网址</div>
-        <div className="w-full grid grid-rows-6">
+"use client";
 
+import { doSearch } from "@/app/_lib/actions/search";
+import { getLinkList } from "@/app/_lib/data/link";
+import { LinkBlock } from "@/app/_ui/LinkListBox";
+import { useSearchParams } from "next/navigation"
+import useSWR from "swr"
+export default function ResultBox() {
+    const searchParams = useSearchParams()
+    const { data } = useSWR(`${searchParams.get('wd')}`, (e) => doSearch(e))
+
+    return <div className="h-[40vh] w-full px-4">
+        <div className="font-bold">搜索结果:{`(共${data?.count || 0}条结果)`}</div>
+        <div className="pl-2 border-l-blue-400 border-0 border-l-2 my-2">网站链接</div>
+        <div className=" grid grid-cols-3 lg:grid-cols-6 gap-4 ">
+
+            {
+                data?.list.map((e: any) => {
+                    return <LinkBlock val={e} key={e._id}></LinkBlock>
+                })
+            }
         </div>
     </div>
 }
\ No newline at end of file
diff --git a/app/_lib/actions/search.ts b/app/_lib/actions/search.ts
index 24a69a0..6238f85 100644
--- a/app/_lib/actions/search.ts
+++ b/app/_lib/actions/search.ts
@@ -2,6 +2,7 @@
 
 import { ArticleType } from "../data/article"
 import { Link } from "../data/link"
+import { getCollection } from "../mongodb";
 
 export type SiteSearchType = {
     articles: ArticleType[];
@@ -11,8 +12,28 @@ export type SiteSearchType = {
 
 }
 export async function doSearch(wd: string) {
+    //实现对link的搜索
+    const linkCol = await getCollection('link')
 
+
+    const linkList = await linkCol.aggregate([
+        {
+            $match: {
+                $or: [ // 任意字段匹配即返回
+                    { name: { $regex: wd, $options: "i" } },
+                    { description: { $regex: wd, $options: "i" } },
+                    { tags: { $regex: wd, $options: "i" } }
+                ]
+            }
+        },
+        {
+            $addFields: {
+                _id: { $toString: "$_id" }
+            }
+        }
+    ]).toArray();
     return {
-        
+        list: linkList,
+        count: linkList.length
     }
 }
\ No newline at end of file
diff --git a/app/_lib/data/linkType.ts b/app/_lib/data/linkType.ts
index 6745cb3..3b9ddf7 100644
--- a/app/_lib/data/linkType.ts
+++ b/app/_lib/data/linkType.ts
@@ -1,5 +1,6 @@
 "use server";
 
+import { ObjectId } from "mongodb";
 import { getCollection } from "../mongodb";
 import { ReactNode } from "react";
 
@@ -14,6 +15,13 @@ export type LinkType = {
     location?: string;
 
 }
+export async function getLinkType(id: string) {
+    const collection = await getCollection('link-type');
+    return collection.findOne({ _id: new ObjectId(id) }).then(res => res ? {
+        ...res,
+        _id: res._id.toString()
+    } as LinkType : null)
+}
 export async function getLinkTypeList({ page = 1, pageSize = 9999 }: {
     page?: number;
     pageSize?: number;
diff --git a/app/_ui/Search.tsx b/app/_ui/Search.tsx
index 4213efb..a36224b 100644
--- a/app/_ui/Search.tsx
+++ b/app/_ui/Search.tsx
@@ -8,6 +8,7 @@ import Logo from "./Logo";
 import { useRequest } from "ahooks";
 import { getSearchTypeList, getSearchWayList, SearchWayItemType } from "../_lib/data/search";
 import { doSearch } from "../_lib/utils";
+import { useSearchParams } from "next/navigation";
 
 
 const defaultSearchEngine: SearchWayItemType = {
@@ -31,7 +32,8 @@ export default function Search() {
     })))
     const [selectKey, setSelectKey] = useState<string | null>(null)
     const [activeSearchKey, setActiveSearchKey] = useState<string | null>(null)
-    const [inputStr, setInputStr] = useState('')
+    const [inputStr, setInputStr] = useState('') 
+    const searchParams = useSearchParams();
     const nowSelectConfig = useMemo(() => {
         const idx = searchTypeList.findIndex(val => val._id === selectKey)
         if (idx !== -1) return searchTypeList[idx]
@@ -93,11 +95,12 @@ export default function Search() {
 
                         }}
                         type="text"
-                        onChange={e => {
+                        onChange={(e)=> {
                             setInputStr(e.target.value)
-
                         }}
-                        placeholder={activeSearch?.label ? `${activeSearch?.label}搜索` : ''} className="w-full bg-[#C4C2C6] px-4 h-[50px] rounded-3xl  outline-none" />
+                        defaultValue={searchParams.get('wd')?.toString()}
+                        placeholder={activeSearch?.label ? `${activeSearch?.label}搜索` : ''} 
+                        className="w-full bg-[#C4C2C6] px-4 h-[50px] rounded-3xl  outline-none" />
                     <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 h-[50px] text-[#666666]">