Skip to content

Commit

Permalink
use a map instead of both Set and array
Browse files Browse the repository at this point in the history
  • Loading branch information
levalleux-ludo committed Dec 1, 2023
1 parent a47dd5b commit 8163cda
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/lib/utils/hooks/product/useProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ export default function useProducts(
);

const { allProducts, allSellers } = useMemo(() => {
const sellerIdSet = new Set<string>();
const allSellers: subgraph.Seller[] = [];
const sellerMap = new Map<subgraph.Seller["id"], subgraph.Seller>();
const allProducts =
(productsVariants?.data || [])
?.map((product) => {
Expand All @@ -142,9 +141,8 @@ export default function useProducts(
: (product as subgraph.ProductV1Product).variants;
const offers = (allVariantsOrOnlyNotVoided || [])?.map(
({ offer }) => {
if (!sellerIdSet.has(offer.seller.id)) {
sellerIdSet.add(offer.seller.id);
allSellers.push(offer.seller);
if (!sellerMap.has(offer.seller.id)) {
sellerMap.set(offer.seller.id, offer.seller);
}
const status = offersSdk.getOfferStatus(offer);
const offerPrice = convertPrice({
Expand Down Expand Up @@ -249,7 +247,7 @@ export default function useProducts(
return null;
})
.filter(isTruthy) || [];
return { allProducts, allSellers };
return { allProducts, allSellers: Array.from(sellerMap.values()) };
}, [
productsVariants?.data,
props?.quantityAvailable_gte,
Expand Down

0 comments on commit 8163cda

Please sign in to comment.