Skip to content

Commit

Permalink
feat: Create Slot filter (#525)
Browse files Browse the repository at this point in the history
* refactor(web): create Option type for Dropdown options

* chore(web): update changeset

* refactor(web): use label in Dropdown selected option display

* refactor(web): update Dropdown component main filename

* refactor(web): remove nested /components folder in Dropdown

* feat(web): render value when no label in Dropdown

* feat(web): add prefix slot to Dropdown option

* feat(web): add filter panel with rollup filter in PaginatedTable

* chore(web): update changeset

* refactor(web): rename main PaginedTable filename

* refactor(web): rename main FilersPanel filename

* refactor(web): update RollupFilter component to fetch rollups from API

* feat(web): add filters to query params

* fix(web): pages number calculation in PaginatedTable

* fix(web): pages number calculation in PaginatedTable

* feat(web): install React DatePicker Tailwind library

* feat(web): add full width prop to Button

* feat(web): add custom height prop to Dropdown

* feat(web): add Timestamp filter to FilterPanel

* chore(web): update changeset

* feat(web): add timestamp filters to URL

* fix(web): Skeleton custom height not working

* refactor(web): simplify dropdowns options

* refactor(web): reduce contitions to filter in Filters

* fix(web): reset filters when all are reset after being filtered

* feat(web): create BlockNumber filter

* feat(web): move block number validations to filters form

* chore(web): update changeset

* fix(web): reset filters when all are reset after being filtered

* feat(web): add clear button for filter

* fix(web): clear/filter buttons responsiveness

* fix(web): clear/filter buttons responsiveness for desktop

* fix(web): fix block number filter responsiveness

* refactor(web): decouple Filters from PaginatedTable

* feat(web): add slot filter to blocks table

* fix(web): fix filters reponsiveness

* chore(web): update changeset

* fix(web): display block number and slot range filters on the same row on small screens

* chore(web): remove unused leftover files

* fix(web): align number range input values to the left

* refactor(web): change number range in filters to close interval

---------

Co-authored-by: Luis Herasme <[email protected]>
Co-authored-by: PJColombo <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent c9a6ada commit 9ba363e
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-years-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/web": minor
---

Added slot range filter to blocks view
18 changes: 18 additions & 0 deletions apps/web/src/components/Filters/SlotFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { FC } from "react";

import type { NumberRangeInputProps } from "../Inputs/NumberRangeInput";
import { NumberRangeInput } from "../Inputs/NumberRangeInput";

type SlotFilterProps = Pick<NumberRangeInputProps, "range" | "onChange">;

export const SlotFilter: FC<SlotFilterProps> = function (props) {
return (
<NumberRangeInput
className="w-full"
type="uint"
inputStartProps={{ placeholder: "Start Slot" }}
inputEndProps={{ placeholder: "End Slot" }}
{...props}
/>
);
};
75 changes: 61 additions & 14 deletions apps/web/src/components/Filters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import type { Option } from "../Dropdown";
import type { NumberRange } from "../Inputs/NumberRangeInput";
import { BlockNumberFilter } from "./BlockNumberFilter";
import { ROLLUP_OPTIONS, RollupFilter } from "./RollupFilter";
import { SlotFilter } from "./SlotFilter";
import { TimestampFilter } from "./TimestampFilter";

type FiltersState = {
rollup: Option | null;
timestampRange: DateRangeType | null;
blockNumberRange: NumberRange | null;
slotRange: NumberRange | null;
};

type ClearAction<V extends keyof FiltersState> = {
Expand All @@ -41,6 +43,7 @@ const INIT_STATE: FiltersState = {
startDate: null,
},
blockNumberRange: null,
slotRange: null,
};

function reducer<V extends keyof FiltersState>(
Expand Down Expand Up @@ -68,11 +71,15 @@ export const Filters: FC = function () {
const queryParams = useQueryParams();
const [filters, dispatch] = useReducer(reducer, INIT_STATE);
const disableClear =
!filters.rollup && !filters.timestampRange && !filters.blockNumberRange;
!filters.rollup &&
!filters.timestampRange?.endDate &&
!filters.timestampRange?.startDate &&
!filters.blockNumberRange &&
!filters.slotRange;

const handleFilter = () => {
const query: UrlObject["query"] = {};
const { rollup, timestampRange, blockNumberRange } = filters;
const { rollup, timestampRange, blockNumberRange, slotRange } = filters;

if (rollup) {
if (rollup.value === "null") {
Expand Down Expand Up @@ -106,15 +113,35 @@ export const Filters: FC = function () {
}
}

if (slotRange) {
const { start, end } = slotRange;

if (start) {
query.startSlot = start;
}

if (end) {
query.endSlot = end;
}
}

router.push({
pathname: router.pathname,
query,
});
};

useEffect(() => {
const { rollup, from, startDate, endDate, startBlock, endBlock } =
queryParams;
const {
rollup,
from,
startDate,
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
} = queryParams;
const newFilters: Partial<FiltersState> = {};

if (rollup || from) {
Expand All @@ -141,6 +168,13 @@ export const Filters: FC = function () {
};
}

if (startSlot || endSlot) {
newFilters.slotRange = {
start: startSlot,
end: endSlot,
};
}

dispatch({ type: "UPDATE", payload: newFilters });
}, [queryParams]);

Expand All @@ -167,16 +201,29 @@ export const Filters: FC = function () {
}
/>
</div>
<div className="w-full md:w-52">
<BlockNumberFilter
range={filters.blockNumberRange}
onChange={(newBlockNumberRange) =>
dispatch({
type: "UPDATE",
payload: { blockNumberRange: newBlockNumberRange },
})
}
/>
<div className="flex gap-2">
<div className="w-full md:w-52">
<BlockNumberFilter
range={filters.blockNumberRange}
onChange={(newBlockNumberRange) =>
dispatch({
type: "UPDATE",
payload: { blockNumberRange: newBlockNumberRange },
})
}
/>
</div>
<div className="w-full md:w-52">
<SlotFilter
range={filters.slotRange}
onChange={(newSlotRange) =>
dispatch({
type: "UPDATE",
payload: { slotRange: newSlotRange },
})
}
/>
</div>
</div>
</div>
<div className="flex flex-col gap-2 md:flex-row">
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Inputs/NumberRangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const NumberRangeInput: React.FC<NumberRangeInputProps> = ({
type={type}
variant="filled"
placeholder="Start"
className={`rounded-l-lg rounded-r-none border-r-controlBorder-light text-right text-sm dark:border-r-border-dark ${className}`}
className={`rounded-l-lg rounded-r-none border-r-controlBorder-light text-sm dark:border-r-border-dark ${className}`}
onChange={(newStartValue) =>
onChange({
start: newStartValue,
Expand Down
10 changes: 9 additions & 1 deletion apps/web/src/components/PaginatedTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback } from "react";
import type { FC } from "react";
import type { FC, ReactNode } from "react";
import { useRouter } from "next/router";
import Skeleton from "react-loading-skeleton";

Expand All @@ -10,6 +10,7 @@ import type { PaginationProps } from "~/components/Pagination";
import { Pagination } from "~/components/Pagination";
import type { TableProps } from "~/components/Table";
import { Table } from "~/components/Table";
import type { Rollup } from "~/types";

const DEFAULT_TABLE_EMPTY_STATE = "No items";
const PAGE_SIZES_OPTIONS: DropdownProps["options"] = [
Expand All @@ -20,6 +21,12 @@ const PAGE_SIZES_OPTIONS: DropdownProps["options"] = [
];
const DEFAULT_ROW_SKELETON_HEIGHT = 22;

export interface PaginatedTableQueryFilters {
rollup: Rollup;
startDate: Date;
endDate: Date;
}

type PaginationData = {
page: number;
pageSize: number;
Expand All @@ -31,6 +38,7 @@ export type PaginatedTableProps = {
isExpandable?: boolean;
paginationData: PaginationData;
rowSkeletonHeight?: string | number;
tableTopSlot?: ReactNode;
} & Pick<TableProps, "headers" | "rows">;

const getRowsSkeleton = (
Expand Down
18 changes: 16 additions & 2 deletions apps/web/src/hooks/useQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type QueryParams = {
endDate?: Date;
startBlock?: number;
endBlock?: number;
startSlot?: number;
endSlot?: number;
};

const DEFAULT_INITIAL_PAGE_SIZE = 50;
Expand All @@ -31,8 +33,18 @@ export function useQueryParams() {
return;
}

const { from, p, ps, rollup, startDate, endDate, startBlock, endBlock } =
router.query;
const {
from,
p,
ps,
rollup,
startDate,
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
} = router.query;

setQueryParams({
from: (from as string)?.toLowerCase(),
Expand All @@ -49,6 +61,8 @@ export function useQueryParams() {
endDate: endDate ? new Date(endDate as string) : undefined,
startBlock: parseInt(startBlock as string) || undefined,
endBlock: parseInt(endBlock as string) || undefined,
startSlot: parseInt(startSlot as string) || undefined,
endSlot: parseInt(endSlot as string) || undefined,
});
}, [router]);

Expand Down
16 changes: 14 additions & 2 deletions apps/web/src/pages/blobs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ const BLOBS_TABLE_HEADERS = [
];

const Blobs: NextPage = function () {
const { from, p, ps, rollup, startDate, endDate, startBlock, endBlock } =
useQueryParams();
const {
from,
p,
ps,
rollup,
startDate,
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
} = useQueryParams();
const { data, error, isLoading } = api.blob.getAll.useQuery({
p,
ps,
Expand All @@ -62,6 +72,8 @@ const Blobs: NextPage = function () {
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
});
const { blobs, totalBlobs } = data || {};

Expand Down
16 changes: 14 additions & 2 deletions apps/web/src/pages/blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,18 @@ export const BLOCKS_TABLE_HEADERS = [
];

const Blocks: NextPage = function () {
const { from, p, ps, rollup, startDate, endDate, startBlock, endBlock } =
useQueryParams();
const {
from,
p,
ps,
rollup,
startDate,
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
} = useQueryParams();
const {
data: rawBlocksData,
isLoading,
Expand All @@ -72,6 +82,8 @@ const Blocks: NextPage = function () {
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
});
const blocksData = useMemo(() => {
if (!rawBlocksData) {
Expand Down
16 changes: 14 additions & 2 deletions apps/web/src/pages/txs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,18 @@ export const TRANSACTIONS_TABLE_HEADERS = [
];

const Txs: NextPage = function () {
const { from, p, ps, rollup, startDate, endDate, startBlock, endBlock } =
useQueryParams();
const {
from,
p,
ps,
rollup,
startDate,
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
} = useQueryParams();

const {
data: rawTxsData,
Expand All @@ -102,6 +112,8 @@ const Txs: NextPage = function () {
endDate,
startBlock,
endBlock,
startSlot,
endSlot,
expand: "block,blob",
});
const txsData = useMemo(() => {
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/middlewares/withFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

type NumberRange = {
gte?: number;
lt?: number;
lte?: number;
};

type DateRange = {
Expand Down Expand Up @@ -106,7 +106,7 @@ export const withFilters = t.middleware(({ next, input = {} }) => {

if (blockRangeExists) {
filters.blockNumber = {
lt: endBlock,
lte: endBlock,
gte: startBlock,
};
}
Expand All @@ -120,7 +120,7 @@ export const withFilters = t.middleware(({ next, input = {} }) => {

if (slotRangeExists) {
filters.blockSlot = {
lt: endSlot,
lte: endSlot,
gte: startSlot,
};
}
Expand Down
12 changes: 1 addition & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9ba363e

Please sign in to comment.