Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export const ResolvedPriceComponentsCard = <
}
}
},
(items) => items,
{
defaultPageSize: 50,
defaultSort: "name",
Expand Down
19 changes: 12 additions & 7 deletions apps/insights/src/components/PriceFeeds/price-feeds-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ import type {
} from "@pythnetwork/component-library/Table";
import { Table } from "@pythnetwork/component-library/Table";
import { useLogger } from "@pythnetwork/component-library/useLogger";
import { useQueryState, parseAsString } from "nuqs";
import { matchSorter } from "match-sorter";
import { parseAsString, useQueryState } from "nuqs";
import type { ReactNode } from "react";
import { Suspense, useCallback, useMemo } from "react";
import { useFilter, useCollator } from "react-aria";
import { useCollator } from "react-aria";

import styles from "./price-feeds-card.module.scss";
import { useQueryParamFilterPagination } from "../../hooks/use-query-param-filter-pagination";
import { Cluster } from "../../services/pyth";
import { AssetClassBadge } from "../AssetClassBadge";
import { FeedKey } from "../FeedKey";
import {
SKELETON_WIDTH,
LivePrice,
LiveConfidence,
LivePrice,
LiveValue,
SKELETON_WIDTH,
} from "../LivePrices";
import { PriceFeedTag } from "../PriceFeedTag";
import { PriceName } from "../PriceName";
import styles from "./price-feeds-card.module.scss";

type Props = {
id: string;
Expand All @@ -56,7 +57,6 @@ export const PriceFeedsCard = ({ priceFeeds, ...props }: Props) => (
const ResolvedPriceFeedsCard = ({ priceFeeds, ...props }: Props) => {
const logger = useLogger();
const collator = useCollator();
const filter = useFilter({ sensitivity: "base", usage: "search" });
const [assetClass, setAssetClass] = useQueryState(
"assetClass",
parseAsString.withDefault(""),
Expand All @@ -83,14 +83,19 @@ const ResolvedPriceFeedsCard = ({ priceFeeds, ...props }: Props) => {
mkPageLink,
} = useQueryParamFilterPagination(
feedsFilteredByAssetClass,
(priceFeed, search) => filter.contains(priceFeed.displaySymbol, search),
() => true,
(a, b, { column, direction }) => {
const field = column === "assetClass" ? "assetClass" : "displaySymbol";
return (
(direction === "descending" ? -1 : 1) *
collator.compare(a[field], b[field])
);
},
(items, search) => {
return matchSorter(items, search, {
keys: ["displaySymbol", "symbol", "description", "key"],
});
Comment on lines +95 to +97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance this call would be blocking? Perhaps the array is small enough that this isn't a problem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a few thousands items so I don't think it's going to impact too much

},
{ defaultSort: "priceFeedName" },
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const ResolvedPublishersCard = ({
}
}
},
(items) => items,
{ defaultSort: "ranking" },
);

Expand Down
11 changes: 8 additions & 3 deletions apps/insights/src/hooks/use-query-param-filter-pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { useCallback, useMemo } from "react";

export const useQueryParamFilterPagination = <T>(
items: T[],
predicate: (item: T, term: string) => boolean,
predicate: (item: T, search: string) => boolean,
doSort: (a: T, b: T, descriptor: SortDescriptor) => number,
doMutate: (items: T[], search: string) => T[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably have deleted predicate and doSort at this point since doMutate is a more open/powerful API anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair and I think at some point we can remove that. I didn't want to do a big refactor this time as it's used in a few places.

options?: {
defaultPageSize?: number | undefined;
defaultSort?: string | undefined;
Expand Down Expand Up @@ -100,9 +101,13 @@ export const useQueryParamFilterPagination = <T>(
[filteredItems, sortDescriptor, doSort],
);

const mutatedItems = useMemo(() => {
return doMutate(sortedItems, search);
}, [doMutate, sortedItems, search]);

const paginatedItems = useMemo(
() => sortedItems.slice((page - 1) * pageSize, page * pageSize),
[page, pageSize, sortedItems],
() => mutatedItems.slice((page - 1) * pageSize, page * pageSize),
[page, pageSize, mutatedItems],
);

const numPages = useMemo(
Expand Down
Loading