Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sadmann7 committed Jan 21, 2024
2 parents eb5744d + c095b88 commit eb8eb2c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
22 changes: 20 additions & 2 deletions src/app/_components/tasks-table-shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import * as React from "react"
import { type Task } from "@/db/schema"
import { type ColumnDef } from "@tanstack/react-table"
import { type SearchParams } from "@/types"
import { type ColumnDef, type ColumnFiltersState } from "@tanstack/react-table"

import { useDataTable } from "@/hooks/use-data-table"
import { DataTable } from "@/components/data-table/data-table"
Expand All @@ -20,9 +21,13 @@ import {

interface TasksTableShellProps {
tasksPromise: TasksPromise
searchParams: SearchParams
}

export function TasksTableShell({ tasksPromise }: TasksTableShellProps) {
export function TasksTableShell({
tasksPromise,
searchParams,
}: TasksTableShellProps) {
// Learn more about React.use here: https://react.dev/reference/react/use
const { data, pageCount } = React.use(tasksPromise)

Expand All @@ -34,12 +39,25 @@ export function TasksTableShell({ tasksPromise }: TasksTableShellProps) {
[isPending]
)

const initialColumnFilters = Object.entries(searchParams)
.map(([columnName, columnValue]) => {
if (!["page", "perPage", "sort"].includes(columnName) && columnValue) {
const parsedValue =
typeof columnValue === "string" && columnValue.includes(".")
? columnValue.split(".")
: columnValue
return { id: columnName, value: parsedValue }
}
})
.filter((ele) => !!ele) as ColumnFiltersState

const { dataTable } = useDataTable({
data,
columns,
pageCount,
searchableColumns,
filterableColumns,
initialColumnFilters,
})

return (
Expand Down
5 changes: 4 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export default function IndexPage({ searchParams }: IndexPageProps) {
* This is done because the table columns need to be memoized, and the `useDataTable` hook needs to be called in a client component.
* By encapsulating the `DataTable` component within the `tasktableshell` component, we can ensure that the necessary logic and state management is handled correctly.
*/}
<TasksTableShell tasksPromise={tasksPromise} />
<TasksTableShell
tasksPromise={tasksPromise}
searchParams={searchParams}
/>
</React.Suspense>
</Shell>
)
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/use-data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ interface UseDataTableProps<TData, TValue> {
* @example filterableColumns={[{ id: "status", title: "Status", options: ["todo", "in-progress", "done", "canceled"]}]}
*/
filterableColumns?: DataTableFilterableColumn<TData>[]
initialColumnFilters?: ColumnFiltersState
}

export function useDataTable<TData, TValue>({
Expand All @@ -67,6 +68,7 @@ export function useDataTable<TData, TValue>({
pageCount,
searchableColumns = [],
filterableColumns = [],
initialColumnFilters = [],
}: UseDataTableProps<TData, TValue>) {
const router = useRouter()
const pathname = usePathname()
Expand Down Expand Up @@ -105,9 +107,8 @@ export function useDataTable<TData, TValue>({
const [rowSelection, setRowSelection] = React.useState({})
const [columnVisibility, setColumnVisibility] =
React.useState<VisibilityState>({})
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
[]
)
const [columnFilters, setColumnFilters] =
React.useState<ColumnFiltersState>(initialColumnFilters)

// Handle server-side pagination
const [{ pageIndex, pageSize }, setPagination] =
Expand Down

0 comments on commit eb8eb2c

Please sign in to comment.