From a5434730ede1272f195d6a4b13207b840a5ac2cf Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 1 Mar 2024 21:01:00 +0000 Subject: feature: Add full text search support --- packages/web/app/dashboard/components/Sidebar.tsx | 18 ++++- packages/web/app/dashboard/search/page.tsx | 93 +++++++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 packages/web/app/dashboard/search/page.tsx (limited to 'packages/web/app') diff --git a/packages/web/app/dashboard/components/Sidebar.tsx b/packages/web/app/dashboard/components/Sidebar.tsx index 7eea6b6d..010ee103 100644 --- a/packages/web/app/dashboard/components/Sidebar.tsx +++ b/packages/web/app/dashboard/components/Sidebar.tsx @@ -1,4 +1,12 @@ -import { Archive, Star, Tag, Home, PackageOpen, Settings } from "lucide-react"; +import { + Archive, + Star, + Tag, + Home, + PackageOpen, + Settings, + Search, +} from "lucide-react"; import { redirect } from "next/navigation"; import SidebarItem from "./SidebarItem"; import { getServerAuthSession } from "@/server/auth"; @@ -6,6 +14,7 @@ import Link from "next/link"; import SidebarProfileOptions from "./SidebarProfileOptions"; import { Separator } from "@/components/ui/separator"; import AllLists from "./AllLists"; +import serverConfig from "@hoarder/shared/config"; export default async function Sidebar() { const session = await getServerAuthSession(); @@ -34,6 +43,13 @@ export default async function Sidebar() { name="Favourites" path="/dashboard/bookmarks/favourites" /> + {serverConfig.meilisearch && ( + } + name="Search" + path="/dashboard/search" + /> + )} } name="Archive" diff --git a/packages/web/app/dashboard/search/page.tsx b/packages/web/app/dashboard/search/page.tsx new file mode 100644 index 00000000..1c26608e --- /dev/null +++ b/packages/web/app/dashboard/search/page.tsx @@ -0,0 +1,93 @@ +"use client"; + +import { api } from "@/lib/trpc"; +import { usePathname, useRouter, useSearchParams } from "next/navigation"; +import BookmarksGrid from "../bookmarks/components/BookmarksGrid"; +import { Input } from "@/components/ui/input"; +import Loading from "../bookmarks/loading"; +import { keepPreviousData } from "@tanstack/react-query"; +import { Search } from "lucide-react"; +import { ActionButton } from "@/components/ui/action-button"; +import { Suspense, useRef } from "react"; + +function SearchComp() { + const router = useRouter(); + const pathname = usePathname(); + const searchParams = useSearchParams(); + const searchQuery = searchParams.get("q") || ""; + + const { data, isPending, isPlaceholderData, error } = + api.bookmarks.searchBookmarks.useQuery( + { + text: searchQuery, + }, + { + placeholderData: keepPreviousData, + }, + ); + + if (error) { + throw error; + } + + const inputRef: React.MutableRefObject = + useRef(null); + + let timeoutId: NodeJS.Timeout | undefined; + + // Debounce user input + const doSearch = () => { + if (!inputRef.current) { + return; + } + router.replace(`${pathname}?q=${inputRef.current.value}`); + }; + + const onInputChange = () => { + if (timeoutId) { + clearTimeout(timeoutId); + } + timeoutId = setTimeout(() => { + doSearch(); + }, 200); + }; + + return ( +
+
+ + + + + Search + + +
+
+ {data ? ( + b.id) }} + bookmarks={data.bookmarks} + /> + ) : ( + + )} +
+ ); +} + +export default function SearchPage() { + return ( + + + + ); +} -- cgit v1.2.3-70-g09d2