Skip to content

Commit

Permalink
batch queires
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Dec 15, 2024
1 parent 4da4b30 commit 9ac8905
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 43 deletions.
30 changes: 1 addition & 29 deletions client/src/ui/components/entities/Entity.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { addToSubscription } from "@/dojo/queries";
import { useDojo } from "@/hooks/context/DojoContext";
import { ArrivalInfo } from "@/hooks/helpers/use-resource-arrivals";
import { getArmyByEntityId } from "@/hooks/helpers/useArmies";
Expand All @@ -11,7 +10,7 @@ import { divideByPrecision, formatTime, getEntityIdFromKeys } from "@/ui/utils/u
import { EntityType } from "@bibliothecadao/eternum";
import { useComponentValue } from "@dojoengine/react";
import clsx from "clsx";
import React, { useEffect, useMemo, useState } from "react";
import React, { useMemo, useState } from "react";
import { DepositResources } from "../resources/DepositResources";

const entityIcon: Record<EntityType, string> = {
Expand Down Expand Up @@ -51,33 +50,6 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
return getResourcesFromBalance(arrival.entityId);
}, [weight]);

useEffect(() => {
if (entityResources.length === 0) {
const cacheKey = `${CACHE_KEY}-${arrival.entityId}`;
const cachedTime = localStorage.getItem(cacheKey);
const now = Date.now();

if (cachedTime && now - parseInt(cachedTime) < CACHE_DURATION) {
return;
}

setIsSyncing(true);
const fetch = async () => {
try {
await addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, [
arrival.entityId.toString(),
]);
localStorage.setItem(cacheKey, now.toString());
} catch (error) {
console.error("Fetch failed", error);
} finally {
setIsSyncing(false);
}
};
fetch();
}
}, [arrival.entityId, dojo.network.toriiClient, dojo.network.contractComponents, entityResources.length]);

const army = useMemo(() => getArmy(arrival.entityId), [arrival.entityId, entity.resources]);

const renderEntityStatus = useMemo(() => {
Expand Down
47 changes: 33 additions & 14 deletions client/src/ui/components/trading/ResourceArrivals.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import { addToSubscription } from "@/dojo/queries";
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 { memo } from "react";
import { memo, useEffect } from "react";
import { EntityArrival } from "../entities/Entity";
import { HintSection } from "../hints/HintModal";

export const AllResourceArrivals = memo(
({ arrivals, className = "" }: { arrivals: ArrivalInfo[]; className?: string }) => (
<div className={`p-2 flex flex-col space-y-1 overflow-y-auto gap-2 ${className}`}>
<Headline>
<div className="flex gap-2">
<div className="self-center">Transfers</div>
<HintModalButton section={HintSection.Transfers} />
</div>
</Headline>
{arrivals.map((arrival) => (
<EntityArrival arrival={arrival} key={arrival.entityId} />
))}
</div>
),
({ arrivals, className = "" }: { arrivals: ArrivalInfo[]; className?: string }) => {
const dojo = useDojo();

useEffect(() => {
const fetch = async () => {
try {
await addToSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any, [
...arrivals.map((arrival) => arrival.entityId.toString()),
]);
} catch (error) {
console.error("Fetch failed", error);
}
};
fetch();
}, [arrivals, dojo.network.toriiClient, dojo.network.contractComponents]);

return (
<div className={`p-2 flex flex-col space-y-1 overflow-y-auto gap-2 ${className}`}>
<Headline>
<div className="flex gap-2">
<div className="self-center">Transfers</div>
<HintModalButton section={HintSection.Transfers} />
</div>
</Headline>
{arrivals.map((arrival) => (
<EntityArrival arrival={arrival} key={arrival.entityId} />
))}
</div>
);
},
);

0 comments on commit 9ac8905

Please sign in to comment.