vega-frontend-monorepo/libs/react-helpers/src/lib/get-nodes.ts

24 lines
531 B
TypeScript
Raw Normal View History

import type { Schema } from '@vegaprotocol/types';
type Edge<T> = {
node: T;
};
type Connection<A> = {
edges?: Schema.Maybe<Array<Schema.Maybe<A>>>;
};
export function getNodes<
T,
A extends Edge<T> = Edge<T>,
B extends Connection<A> = Connection<A>
>(data?: B | null, filterBy?: (item?: T | null) => boolean) {
const edges = data?.edges || [];
return edges.reduce<T[]>((acc, edge) => {
if (edge?.node && (filterBy ? filterBy(edge?.node) : true)) {
acc.push(edge.node);
}
return acc;
}, []);
}