-
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.
updated /blocks components to work with new suspense support
- Loading branch information
Showing
3 changed files
with
74 additions
and
34 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
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 |
---|---|---|
@@ -1,48 +1,64 @@ | ||
"use server"; | ||
"use client"; | ||
|
||
import { columns } from "./columns"; | ||
import { HydrationBoundary, QueryClient, dehydrate } from "@tanstack/react-query"; | ||
import { useSuspenseQuery } from "@tanstack/react-query"; | ||
import { useState } from "react"; | ||
import { PaginatedDataTable } from "../ui/paginated-data-table"; | ||
import { PaginationState, getCoreRowModel, useReactTable } from "@tanstack/react-table"; | ||
import getBlocks from "./getBlocks"; | ||
import { cn } from "@/lib/utils"; | ||
|
||
export interface BlocksTableRow { | ||
height: number, | ||
createdAt: Date, | ||
// TODO: It would be nice to associate all events with a given row. Need to get testnet setup again and pull in example data for building this out. | ||
// events: any[], | ||
}; | ||
export interface QueryOptions { | ||
pageIndex: number, | ||
pageSize: number, | ||
} | ||
|
||
interface PaginatedDataTableProps { | ||
className?: string, | ||
queryName: string, | ||
defaultQueryOptions: QueryOptions, | ||
endpoint: string, | ||
errorMessage: string, | ||
} | ||
|
||
const BlocksTable = async ({ className } : { className: string }) => { | ||
const queryClient = new QueryClient(); | ||
|
||
const defaultQueryOptions = { | ||
pageIndex: 0, | ||
pageSize: 10, | ||
}; | ||
const queryName = "BlocksTable"; | ||
const endpoint = "/api/blocks"; | ||
const errorMessage = "Failed to query data while trying to generate blocks table, please try reloading the page."; | ||
export function BlocksTable ({ | ||
className, | ||
queryName, | ||
defaultQueryOptions, | ||
endpoint, | ||
errorMessage, | ||
} : PaginatedDataTableProps) { | ||
|
||
await queryClient.prefetchQuery({ | ||
queryFn: async () => await getBlocks({ endpoint, pageIndex: 0}), | ||
queryKey: [queryName, defaultQueryOptions], | ||
const [pagination, setPagination] = useState<PaginationState>({...defaultQueryOptions}); | ||
|
||
const { data } = useSuspenseQuery({ | ||
queryKey: [queryName, pagination], | ||
queryFn: () => getBlocks({ endpoint, pageIndex: pagination.pageIndex }), | ||
meta: { | ||
errorMessage, | ||
}, | ||
}); | ||
|
||
const { pages: pageCount, results: tableData } = data ?? { pages: 0, results: []}; | ||
|
||
const table = useReactTable({ | ||
data: tableData, | ||
columns, | ||
pageCount, | ||
state: { | ||
pagination, | ||
}, | ||
onPaginationChange: setPagination, | ||
getCoreRowModel: getCoreRowModel(), | ||
manualPagination: true, | ||
}); | ||
|
||
return ( | ||
<HydrationBoundary state={dehydrate(queryClient)}> | ||
<PaginatedDataTable | ||
className={className} | ||
queryName={queryName} | ||
defaultQueryOptions={defaultQueryOptions} | ||
columns={columns} | ||
fetcher={getBlocks} | ||
endpoint={endpoint} | ||
errorMessage={errorMessage}/> | ||
</HydrationBoundary> | ||
<div className={cn("", className)}> | ||
<PaginatedDataTable table={table} columns={columns}/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BlocksTable; | ||
export default BlocksTable; |