diff --git a/src/app/api/search/route.ts b/src/app/api/search/route.ts index 5a28c69..3679781 100644 --- a/src/app/api/search/route.ts +++ b/src/app/api/search/route.ts @@ -3,8 +3,8 @@ import { sql } from "@pgtyped/runtime"; import { QueryKind, SearchValidator } from "@/lib/validators/search"; import { IGetBlockSearchQuery, IGetChannelSearchQuery, IGetClientSearchQuery, IGetConnectionSearchQuery, IGetTransactionSearchQuery } from "./route.types"; -export async function POST(req: Request) { - console.log("POST req on /api/search"); +export async function GET(req: Request) { + console.log("GET req on /api/search"); try { const url = new URL(req.url); const queryParam = url.searchParams.get("q")?.trim() ?? ""; @@ -79,6 +79,8 @@ export async function POST(req: Request) { if (clientSearch.length === 0) return new Response("No results.", { status: 404 }); + console.log("Results found:", { kind: searchQuery.kind, identifier: searchQuery.value, related: clientSearch}); + return new Response(JSON.stringify({ kind: searchQuery.kind, identifier: searchQuery.value, diff --git a/src/app/search/[query]/page.tsx b/src/app/search/[query]/page.tsx index 2b8ee4c..c6e8eaf 100644 --- a/src/app/search/[query]/page.tsx +++ b/src/app/search/[query]/page.tsx @@ -1,8 +1,6 @@ -"use client"; import SearchResultsTable from "@/components/SearchResultsTable"; -// import { SearchResultValidator } from "@/lib/validators/search"; -import { useQuery } from "@tanstack/react-query"; -import axios from "axios"; +import { getBaseURL, getQueryClient } from "@/lib/utils"; +import { dehydrate, HydrationBoundary } from "@tanstack/react-query"; import { type FC } from "react"; interface PageProps { @@ -13,43 +11,27 @@ interface PageProps { const Page : FC = ({ params }) => { const { query } = params; + const queryClient = getQueryClient(); - const { data: searchResultData , isError } = useQuery({ + queryClient.prefetchQuery({ queryFn: async () => { - console.log(`Fetching: GET /api/search?q=${query}`); - const { data } = await axios.post(`/api/search?q=${query}`); - console.log("Fetched result:", data); - return data; - // const result = SearchResultValidator.safeParse(data); - // if (result.success) { - // console.log(result.data); - // return result.data; - // } else { - // throw new Error(result.error.message); - // } + const baseUrl = getBaseURL(); + console.log(`FETCHING: GET ${baseUrl}/api/search?q=${query}`); + const data = await fetch(`${baseUrl}/api/search?q=${query}`, { method: "GET" }); + return await data.json(); }, queryKey: ["searchResult", query], - retry: false, meta: { - errorMessage: "Failed to find any results from the provided query. Please try a different query.", + errorMessage: "Failed to find any results for the search term. Please try looking for something else.", }, }); - if (isError) { - return ( -
-

No results found.

-
- ); - } - return ( -
-

Search results

- {// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - searchResultData ? - :

No results

- } +
+

Search results

+ + +
); }; diff --git a/src/components/SearchResultsTable/columns.tsx b/src/components/SearchResultsTable/columns.tsx index e71bc14..e42355d 100644 --- a/src/components/SearchResultsTable/columns.tsx +++ b/src/components/SearchResultsTable/columns.tsx @@ -1,18 +1,11 @@ -"use client"; - -// import { QueryKind } from "@/lib/validators/search"; import { type ColumnDef } from "@tanstack/react-table"; -// import Link from "next/link"; -import { type RelatedQuery, type SearchResult } from "."; +import { SearchResult, type RelatedQuery } from "."; import Link from "next/link"; - -type SearchResultsColumns = SearchResult; - // NOTE: Search Results diverges from all the other table stylings in that most of the text is not xs on smaller devices. // Until I start getting more data, it feels overkill to make it so small for now. This could turn out to be a dumb // pre-caution that I will have to revert sooner than later. -export const columns : Array> = [ +export const columns : Array> = [ { accessorKey: "kind", header: () =>
Kind
, @@ -32,7 +25,7 @@ export const columns : Array> = [ }, }, { - id: "related", + // id: "related", accessorKey: "related", header: () =>
Related Queries
, cell: ({ row }) => { @@ -50,16 +43,19 @@ export const columns : Array> = [ // } if (related !== undefined && related.length !== 0) { return ( -
    - {/* {related.map(({ type, indentifier }, i) =>
  • {type} : {indentifier}
  • )} */} - {related.map(({ type, hash }, i) => { - return
  • {type}:
    {hash}
  • ; - }) - } -
+
+ {related.map(({type, hash}, i) => { + return ( +
+

{type}

+
{hash}
+
+ ); + })} +
); } - return

None

; + return

None

; }, }, ]; diff --git a/src/components/SearchResultsTable/index.tsx b/src/components/SearchResultsTable/index.tsx index c330db4..de05b49 100644 --- a/src/components/SearchResultsTable/index.tsx +++ b/src/components/SearchResultsTable/index.tsx @@ -1,7 +1,9 @@ +"use client"; import { columns } from "./columns"; import { DataTable } from "../ui/data-table"; import { type FC } from "react"; - +import { useSuspenseQuery } from "@tanstack/react-query"; +import { getBaseURL } from "@/lib/utils"; export interface RelatedQuery { type: string, @@ -13,19 +15,28 @@ export interface SearchResult { identifier: string, related?: RelatedQuery[], } -// TODO? -// interface TransactionSearchResult {} -// interface BlockSearchResult {} + interface SearchResultsTableProps { className?: string, - data: SearchResult[] + query: string, } -const SearchResultsTable : FC = ({ className, data }) => { - const relatedVisible = !!data[0].related; - console.log("relatedVisible", relatedVisible); +const SearchResultsTable : FC = ({ className, query }) => { + const { data } = useSuspenseQuery({ + queryFn: async () => { + const baseUrl = getBaseURL(); + console.log(`FETCHING: GET /api/search?q=${query}`); + const data = await fetch(`${baseUrl}/api/search?q=${query}`, { method: "GET" }); + return await data.json(); + }, + queryKey: ["searchResult", query], + }) as { data: SearchResult }; + + const relatedVisible = !!data?.related?.at(0); + const columnVisibility = { "related": relatedVisible }; + return ( - + ); }; diff --git a/src/components/Searchbar/index.tsx b/src/components/Searchbar/index.tsx index 542de18..9846df4 100644 --- a/src/components/Searchbar/index.tsx +++ b/src/components/Searchbar/index.tsx @@ -32,7 +32,6 @@ const SearchBar : FC = ({ className }) => { const searchCmd = () => { router.push(`/search/${input}`); - router.refresh(); }; return ( @@ -59,7 +58,7 @@ const SearchBar : FC = ({ className }) => { toast({ variant: "destructive", title: "Invalid search query.", - description: "Please try again with a block height or transaction hash.", + description: "Try again with a block height, hash hash, or IBC identifier.", }); } } diff --git a/src/components/TransactionsTable/columns.tsx b/src/components/TransactionsTable/columns.tsx index 6446f7e..41fa1b2 100644 --- a/src/components/TransactionsTable/columns.tsx +++ b/src/components/TransactionsTable/columns.tsx @@ -33,7 +33,7 @@ export const columns : Array> = [ header: () =>
Hash
, cell: ({ row }) => { const tx_hash : string = row.getValue("tx_hash"); - return
{tx_hash}
; + return
{tx_hash}
; }, }, ];