From 4317963775bf71473c2734b183244091e58433ab Mon Sep 17 00:00:00 2001 From: expdsn <18111002318@163.com> Date: Thu, 23 Jan 2025 17:14:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/_lib/data/search.ts | 79 +++++++++++++++++++++++++++++++++++++++++ app/_ui/LinkListBox.tsx | 19 ++++++++-- app/_ui/SiderNav.tsx | 3 +- app/_ui/footer.tsx | 5 +++ app/page.tsx | 21 ++++++----- 5 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 app/_lib/data/search.ts create mode 100644 app/_ui/footer.tsx diff --git a/app/_lib/data/search.ts b/app/_lib/data/search.ts new file mode 100644 index 0000000..22f5da1 --- /dev/null +++ b/app/_lib/data/search.ts @@ -0,0 +1,79 @@ +"use server"; + +import { getCollection } from "../mongodb"; + +export type SearchTypeItem = { + name: string; + key: string; + includes: string[]; + priority: number; + +} +export type SearchWayItemType = { + label: string; + value: string; + fullName: string; + key: string; + +} +export async function getSearchTypeList({ page = 1, pageSize = 9999 }: { + page?: number; + pageSize?: number; +}) { + const collection = await getCollection('search-type'); + const startIndex = (page - 1) * pageSize; + const pipeline = [ + { $sort: { priority: 1 } }, + { $skip: startIndex }, + { $limit: pageSize }, + { + $addFields: { + _id: { $toString: "$_id" } + } + } + ]; + const cursor = collection.aggregate(pipeline); + const list = await cursor.toArray(); + + // 计算总数量 + const total = (await collection.find({}).toArray()).length + return { + total, + list + } +} +export async function getSearchWayList({ page = 1, pageSize = 9999 }: { + page?: number; + pageSize?: number; +}) { + const collection = await getCollection('search'); + const startIndex = (page - 1) * pageSize; + const pipeline = [ + { $sort: { priority: 1 } }, + { $skip: startIndex }, + { $limit: pageSize }, + { + $addFields: { + _id: { $toString: "$_id" } + } + } + ]; + const cursor = collection.aggregate(pipeline); + const list = await cursor.toArray(); + + // 计算总数量 + const total = (await collection.find({}).toArray()).length + return { + total, + list + } +} +export async function addSearchType(searchTypeItem: SearchTypeItem) { + const collection = await getCollection('search-type') + collection.insertOne(searchTypeItem) +} +export async function addSearchWay(searchItem: SearchWayItemType) { + const collection = await getCollection('search') + collection.insertOne(searchItem) + +} \ No newline at end of file diff --git a/app/_ui/LinkListBox.tsx b/app/_ui/LinkListBox.tsx index 6e16b9f..c4e1f5b 100644 --- a/app/_ui/LinkListBox.tsx +++ b/app/_ui/LinkListBox.tsx @@ -2,13 +2,28 @@ import Link from "next/link"; import { LinkType } from "../_lib/data/linkType"; import { Link as _Link } from "../_lib/data/link"; -import { useMemo } from "react"; +import { useEffect, useMemo } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faClock, faFire, faTimeline, faTimesCircle, faTimesRectangle, faVolumeTimes } from "@fortawesome/free-solid-svg-icons"; +import { useAtom } from "jotai"; +import { linkTypeAtom } from "../_lib/atom"; export default function LinkListBox({ linkTypeList, linkList, showHot, showRecent }: { linkTypeList: LinkType[]; linkList: _Link[]; showHot: boolean; showRecent: boolean }) { const hotList = useMemo(() => linkList.filter((val, index) => val.isHot && index < 12), []) const recentList = useMemo(() => linkList.map(val => val).sort((a, b) => b.addTime - a.addTime).filter((_, idx) => idx < 12), []) + const [currentId] = useAtom(linkTypeAtom) + useEffect(() => { + console.log(currentId); + + if (currentId) { + // 根据 targetId 查找对应的元素 + const element = document.getElementById(currentId); + if (element) { + // 滚动到找到的元素位置 + element.scrollIntoView({ behavior: 'smooth' }); + } + } + }, [currentId]) return
{ showHot &&
@@ -57,7 +72,7 @@ export default function LinkListBox({ linkTypeList, linkList, showHot, showRecen } { linkTypeList.map(item => ( -
+
diff --git a/app/_ui/SiderNav.tsx b/app/_ui/SiderNav.tsx index ed49543..28a9d8c 100644 --- a/app/_ui/SiderNav.tsx +++ b/app/_ui/SiderNav.tsx @@ -10,8 +10,7 @@ import { LinkType } from "../_lib/data/linkType"; export default function SiderNav({ linkList }: { linkList: LinkType[] }) { const pathname = usePathname() - console.log(pathname); - const [selectType, setSelectType] = useAtom(linkTypeAtom) + const [, setSelectType] = useAtom(linkTypeAtom) return (
diff --git a/app/_ui/footer.tsx b/app/_ui/footer.tsx new file mode 100644 index 0000000..a15ed9c --- /dev/null +++ b/app/_ui/footer.tsx @@ -0,0 +1,5 @@ +export default function Footer() { + return
+ +
+} \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index 36adddb..be67029 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -13,6 +13,7 @@ import Link from "next/link"; import LinkListBox from "./_ui/LinkListBox"; import { getLinkTypeList } from "./_lib/data/linkType"; import { getLinkList } from "./_lib/data/link"; +import Footer from "./_ui/footer"; export default async function Home() { @@ -23,19 +24,23 @@ export default async function Home() { return (
-
+
+
+ +
+
-
+
+ {/* */} + + {/* */} + +
+
-
- {/* */} - - {/* */} - -
); }