Skip to content

Commit

Permalink
Show and load filters from URL (#23)
Browse files Browse the repository at this point in the history
* Add param filter utils

* Use param filter utils
  • Loading branch information
abefernan authored Oct 23, 2024
1 parent cc3254d commit 9d29f9e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 10 additions & 1 deletion components/txs-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useTxs } from "@/hooks/api";
import { useDebounce } from "@/hooks/use-debounce";
import { getFiltersFromParams, setParamsFromFilters } from "@/lib/params";
import { Span, Tx } from "@/types/txs";
import {
ColumnDef,
Expand All @@ -16,6 +17,7 @@ import {
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table";
import { useSearchParams } from "next/navigation";
import { useEffect, useMemo, useState } from "react";
import { DataTable } from "../data-table";
import { DataTableColumnHeader } from "../data-table/data-table-column-header";
Expand All @@ -25,10 +27,13 @@ import { txsColumns } from "./txs-columns";
import { DataTableToolbar } from "./txs-table-toolbar";

export function TxsTable() {
const params = useSearchParams();
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({
tags: false,
});
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(
getFiltersFromParams(params),
);
const [sorting, setSorting] = useState<SortingState>([]);

const operationNameValue = columnFilters.find(
Expand Down Expand Up @@ -101,6 +106,10 @@ export function TxsTable() {
table.setPageSize(15);
}, [table]);

useEffect(() => {
setParamsFromFilters(columnFilters);
}, [columnFilters]);

return (
<div className="space-y-4">
<DataTableToolbar table={table} />
Expand Down
42 changes: 42 additions & 0 deletions lib/params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ColumnFiltersState } from "@tanstack/react-table";
import { ReadonlyURLSearchParams } from "next/navigation";

export const getFiltersFromParams = (params: ReadonlyURLSearchParams) => {
let filters: ColumnFiltersState = [];

const operationName = params.get("operationName");
operationName && filters.push({ id: "operationName", value: operationName });

const tags = params.get("tags");
if (tags) {
try {
const tagsMap: Map<string, string> = new Map(JSON.parse(tags));
filters.push({ id: "tags", value: tagsMap });
} catch {}
}

return filters;
};

export const setParamsFromFilters = (filters: ColumnFiltersState) => {
const params = new URLSearchParams();

const operationName =
(filters.find((filter) => filter.id === "operationName")?.value as
| string
| undefined) ?? undefined;

const tags =
(filters.find((filter) => filter.id === "tags")?.value as
| Map<string, string>
| undefined) ?? undefined;

operationName
? params.set("operationName", operationName)
: params.delete("operationName");
tags
? params.set("tags", JSON.stringify(Array.from(tags)))
: params.delete("tags");

window.history.replaceState(null, "", `?${params.toString()}`);
};

0 comments on commit 9d29f9e

Please sign in to comment.