From a59df488f3c8b1667f6f5203c33350e41b16c8de Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Mon, 16 Dec 2024 14:25:28 +1100 Subject: [PATCH] return memo --- .../components/trading/ResourceArrivals.tsx | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/client/src/ui/components/trading/ResourceArrivals.tsx b/client/src/ui/components/trading/ResourceArrivals.tsx index 28ab30d31..389d85cc2 100644 --- a/client/src/ui/components/trading/ResourceArrivals.tsx +++ b/client/src/ui/components/trading/ResourceArrivals.tsx @@ -3,47 +3,49 @@ import { useDojo } from "@/hooks/context/DojoContext"; import { ArrivalInfo } from "@/hooks/helpers/use-resource-arrivals"; import { Headline } from "@/ui/elements/Headline"; import { HintModalButton } from "@/ui/elements/HintModalButton"; -import { useEffect, useState } from "react"; +import { memo, useEffect, useState } from "react"; import { EntityArrival } from "../entities/Entity"; import { HintSection } from "../hints/HintModal"; -export const AllResourceArrivals = ({ arrivals, className = "" }: { arrivals: ArrivalInfo[]; className?: string }) => { - const dojo = useDojo(); - const [subscribedIds, setSubscribedIds] = useState>(new Set()); +export const AllResourceArrivals = memo( + ({ arrivals, className = "" }: { arrivals: ArrivalInfo[]; className?: string }) => { + const dojo = useDojo(); + const [subscribedIds, setSubscribedIds] = useState>(new Set()); - useEffect(() => { - // Create a single Set from newIds for O(1) lookup - const newIdsSet = new Set(arrivals.map((arrival) => arrival.entityId.toString())); + useEffect(() => { + // Create a single Set from newIds for O(1) lookup + const newIdsSet = new Set(arrivals.map((arrival) => arrival.entityId.toString())); - // Find ids that aren't already subscribed - const unsubscribedIds = Array.from(newIdsSet).filter((id) => !subscribedIds.has(id)); + // Find ids that aren't already subscribed + const unsubscribedIds = Array.from(newIdsSet).filter((id) => !subscribedIds.has(id)); - if (unsubscribedIds.length === 0) return; + if (unsubscribedIds.length === 0) return; - // Batch the state update with the API call - setSubscribedIds((prev) => { - // If nothing changed, return the previous state to prevent re-render - if (unsubscribedIds.every((id) => prev.has(id))) return prev; - return new Set([...prev, ...unsubscribedIds]); - }); + // Batch the state update with the API call + setSubscribedIds((prev) => { + // If nothing changed, return the previous state to prevent re-render + if (unsubscribedIds.every((id) => prev.has(id))) return prev; + return new Set([...prev, ...unsubscribedIds]); + }); - // Move API call outside of state updates - addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, unsubscribedIds).catch( - (error) => console.error("Fetch failed", error), - ); - }, [arrivals, subscribedIds]); + // Move API call outside of state updates + addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, unsubscribedIds).catch( + (error) => console.error("Fetch failed", error), + ); + }, [arrivals, subscribedIds]); - return ( -
- -
-
Transfers
- -
-
- {arrivals.map((arrival) => ( - - ))} -
- ); -}; + return ( +
+ +
+
Transfers
+ +
+
+ {arrivals.map((arrival) => ( + + ))} +
+ ); + }, +);