-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Implemented basic search result table, validators, and client page for #10
- Loading branch information
Showing
6 changed files
with
168 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"use client"; | ||
import SearchResultsTable from "@/components/SearchResultsTable"; | ||
import { SearchResultValidator } from "@/lib/validators/search"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import axios from "axios"; | ||
import { type FC } from "react"; | ||
|
||
interface PageProps { | ||
params: { | ||
query: string | ||
} | ||
} | ||
|
||
const Page : FC<PageProps> = ({ params }) => { | ||
const { query } = params; | ||
|
||
const { data: searchResultData , isFetched, isError } = useQuery({ | ||
queryFn: async () => { | ||
console.log(`Fetching: GET /api/search?q=${query}`); | ||
const { data } = await axios.post(`/api/search?q=${query}`); | ||
console.log("Fetched result:", data); | ||
const result = SearchResultValidator.safeParse(data); | ||
if (result.success) { | ||
console.log(result.data); | ||
return result.data; | ||
} else { | ||
throw new Error(result.error.message); | ||
} | ||
}, | ||
queryKey: ["searchResult", query], | ||
retry: false, | ||
meta: { | ||
errorMessage: "Failed to find any results from the provided query. Please try a different query.", | ||
}, | ||
}); | ||
|
||
if (isError) { | ||
return ( | ||
<div className="py-5 flex justify-center"> | ||
<h1 className="text-4xl font-semibold">No results found.</h1> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div> | ||
{isFetched ? ( | ||
<div> | ||
<h1 className="text-3xl mx-auto py-5 font-semibold">Search results</h1> | ||
{searchResultData ? ( | ||
<div className="flex flex-col justify-center w-full"> | ||
<SearchResultsTable data={[searchResultData]}/> | ||
</div> | ||
) : ( | ||
<p>No results</p> | ||
)} | ||
</div> | ||
) : ( | ||
<p>loading...</p> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use client"; | ||
|
||
import { QueryKind } from "@/lib/validators/search"; | ||
import { type ColumnDef } from "@tanstack/react-table"; | ||
import Link from "next/link"; | ||
|
||
export interface SearchResultsColumns { | ||
kind: string, | ||
created_at?: string, | ||
value?: string | bigint | ||
}; | ||
|
||
export const columns : Array<ColumnDef<SearchResultsColumns>> = [ | ||
{ | ||
accessorKey: "kind", | ||
header: () => <div className="font-semibold text-gray-800">Kind</div>, | ||
cell: ({ row }) => { | ||
const kind : string = row.getValue("kind"); | ||
return <p className="">{kind}</p>; | ||
}, | ||
}, | ||
{ | ||
accessorKey: "created_at", | ||
header: () => <div className="font-semibold text-gray-800 text-center">Timestamp</div>, | ||
cell: ({ row }) => { | ||
const createdAt : string = row.getValue("created_at"); | ||
return <p className="text-xs text-center">{createdAt}</p>; | ||
}, | ||
}, | ||
{ | ||
accessorKey: "value", | ||
header: () => <div className="font-semibold text-gray-800 text-center">value</div>, | ||
cell: ({ row }) => { | ||
console.log(row); | ||
const kind : string = row.getValue("kind"); | ||
if (kind === QueryKind.BlockHeight) { | ||
const height: bigint = row.getValue("value"); | ||
return <Link href={`/block/${height}`} className="underline">{height.toString()}</Link>; | ||
} else if (kind === QueryKind.TxHash) { | ||
const txHash: string = row.getValue("value"); | ||
return <Link href={`/transaction/${txHash}`} className="underline">{txHash}</Link>; | ||
} | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { columns } from "./columns"; | ||
import { DataTable } from "../ui/data-table"; | ||
// import { type QueryKind } from "@/lib/validators/search"; | ||
import { type FC } from "react"; | ||
|
||
interface Props { | ||
data: Array<{ | ||
kind: string, | ||
created_at?: string, | ||
value?: string | bigint | ||
}>, | ||
} | ||
|
||
const SearchResultsTable : FC<Props> = ({ data }) => { | ||
return ( | ||
<div> | ||
<DataTable columns={columns} data={data}/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchResultsTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters