From ba2d6acd25c351b0b826c627bad17f3cf0f1759d Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Mon, 12 Feb 2024 21:27:12 -0800 Subject: [PATCH] search asynchronous working --- src/app/search/page.tsx | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index 59f6cd1..15f9b7d 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -1,27 +1,15 @@ 'use client' import { useSearchParams } from 'next/navigation' import { Zilla_Slab } from 'next/font/google' -import Link from 'next/link' import { Topics, Authors } from '@/app/components/DataDisplay' import { Status, ItemBadge } from '@/app/components/Badges' import { epoch2datestring } from '../utils/epoch2datestring' import searchDocs, { CustomSearchResult } from '@/app/utils/searchDocs' -import { useEffect } from 'react' -import { create } from 'zustand' import { navigate } from '@/app/actions' +import { useSuspenseQuery } from '@tanstack/react-query' const zillaSlab = Zilla_Slab({ subsets: ['latin'], weight: ['500', '700'] }) -interface SearchState { - results: CustomSearchResult[] - setResults: (newResults: CustomSearchResult[]) => void -} -const useSearchStore = create((set) => ({ - results: [], - setResults: (newResults: CustomSearchResult[]) => - set({ results: newResults }), -})) - const SearchResult = ({ result }: { result: CustomSearchResult }) => { const { manifest, abstract, id } = result const { title, authors, topics, dates, status, type } = manifest @@ -66,11 +54,15 @@ const SearchResult = ({ result }: { result: CustomSearchResult }) => { export default function Page() { const searchParams = useSearchParams() const search = searchParams.get('q') as string - const searchStore = useSearchStore((state) => state.results) - const setSearchStore = useSearchStore((state) => state.setResults) - useEffect(() => { - setSearchStore(searchDocs(search)) - }, [search]) + + const { data, error } = useSuspenseQuery({ + queryKey: [search], + queryFn: () => { + const data = searchDocs(search) + return data + }, + }) + if (error) throw error return (
@@ -80,12 +72,10 @@ export default function Page() { {search} {`"`} - {searchStore.length === 0 ? ( + {data.length === 0 ? (

No results found.

) : ( - searchStore.map((result) => ( - - )) + data.map((result) => ) )}
)