diff --git a/landing/src/components/modules/bridge-in.tsx b/landing/src/components/modules/bridge-in.tsx index c2c4d2373..1beb42d53 100644 --- a/landing/src/components/modules/bridge-in.tsx +++ b/landing/src/components/modules/bridge-in.tsx @@ -1,7 +1,6 @@ import { configManager } from "@/dojo/setup"; import { execute } from "@/hooks/gql/execute"; import { useEntities } from "@/hooks/helpers/useEntities"; -import { useRealm } from "@/hooks/helpers/useRealms"; import { useResourceBalance } from "@/hooks/helpers/useResources"; import { GET_CAPACITY_SPEED_CONFIG } from "@/hooks/query/capacityConfig"; import { useLords } from "@/hooks/use-lords"; @@ -25,30 +24,24 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "../ui/ import { calculateDonkeysNeeded, getSeasonAddresses, getTotalResourceWeight } from "../ui/utils/utils"; import { BridgeFees } from "./bridge-fees"; -interface S0EternumRealm { - __typename: "s0_eternum_Realm"; - realm_id: number; -} - -function isS0EternumRealm(model: any): model is S0EternumRealm { - return model?.__typename === "s0_eternum_Realm"; -} - export const BridgeIn = () => { const { address } = useAccount(); const [realmEntityId, setRealmEntityId] = useState(); - const { getBalance, isLoading: isResourcesLoading } = useResourceBalance({entityId: realmEntityId}); + const { getBalance, isLoading: isResourcesLoading } = useResourceBalance({ entityId: realmEntityId }); const { data } = useQuery({ queryKey: ["capacitySpeedConfig"], - queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }), + queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }), refetchInterval: 10_000, }); - const donkeyConfig = useMemo(() => ({ - capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0), - speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0 - }), [data]); + const donkeyConfig = useMemo( + () => ({ + capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0), + speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0, + }), + [data], + ); const [resourceFees, setResourceFees] = useState< { @@ -66,7 +59,6 @@ export const BridgeIn = () => { configManager.getSpeedConfig(DONKEY_ENTITY_TYPE), true, ); - const { getRealmNameById } = useRealm(); const [isLoading, setIsLoading] = useState(false); const [selectedResourceIds, setSelectedResourceIds] = useState([ResourcesIds.Lords]); const [selectedResourceAmounts, setSelectedResourceAmounts] = useState<{ [key: string]: number }>({ @@ -85,26 +77,11 @@ export const BridgeIn = () => { }); }; - const { data: playerRealms } = useEntities(); - const playerRealmsIdAndName = useMemo(() => { - return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => { - const realmModel = realm?.node?.entity?.models?.find(isS0EternumRealm); - return { - realmId: realmModel?.realm_id, - entityId: realm?.node?.entity_id, - name: getRealmNameById(realmModel?.realm_id ?? 0), - }; - }); - }, [playerRealms, getRealmNameById]); + const { playerRealms } = useEntities(); const travelTime = useMemo(() => { if (realmEntityId) { - return computeTravelTime( - Number(ADMIN_BANK_ENTITY_ID), - Number(realmEntityId!), - donkeyConfig.speed, - true, - ); + return computeTravelTime(Number(ADMIN_BANK_ENTITY_ID), Number(realmEntityId!), donkeyConfig.speed, true); } else { return 0; } @@ -139,7 +116,7 @@ export const BridgeIn = () => { } }, [orderWeight, donkeyConfig.capacity]); - const donkeyBalance = getBalance(ResourcesIds.Donkey) + const donkeyBalance = getBalance(ResourcesIds.Donkey); const { bridgeIntoRealm } = useBridgeAsset(); useEffect(() => { @@ -267,8 +244,8 @@ export const BridgeIn = () => { )} - {playerRealmsIdAndName?.length - ? playerRealmsIdAndName.map((realm) => { + {playerRealms?.length + ? playerRealms.map((realm) => { return ( #{realm.realmId} - {realm.name} @@ -399,7 +376,7 @@ const ResourceInputRow = ({ const { data: balance } = useBalance({ token: resourceAddress as `0x${string}`, address: address }); const { lordsBalance } = useLords({ disabled: id !== ResourcesIds.Lords }); - const { data, getBalance } = useResourceBalance({entityId: realmEntityId}); + const { data, getBalance } = useResourceBalance({ entityId: realmEntityId }); const fetchedBalance = id !== ResourcesIds.Lords diff --git a/landing/src/components/modules/bridge-out-step-1.tsx b/landing/src/components/modules/bridge-out-step-1.tsx index c8c178680..231c62aff 100644 --- a/landing/src/components/modules/bridge-out-step-1.tsx +++ b/landing/src/components/modules/bridge-out-step-1.tsx @@ -1,7 +1,6 @@ import { configManager } from "@/dojo/setup"; import { execute } from "@/hooks/gql/execute"; import { useEntities } from "@/hooks/helpers/useEntities"; -import { useRealm } from "@/hooks/helpers/useRealms"; import { useResourceBalance } from "@/hooks/helpers/useResources"; import { GET_CAPACITY_SPEED_CONFIG } from "@/hooks/query/capacityConfig"; import { useBridgeAsset } from "@/hooks/useBridge"; @@ -33,35 +32,30 @@ import { } from "../ui/utils/utils"; import { BridgeFees } from "./bridge-fees"; -interface S0EternumRealm { - __typename: "s0_eternum_Realm"; - realm_id: number; -} - -function isS0EternumRealm(model: any): model is S0EternumRealm { - return model?.__typename === "s0_eternum_Realm"; -} export const BridgeOutStep1 = () => { const { address } = useAccount(); const [realmEntityId, setRealmEntityId] = useState(""); - const { getRealmNameById } = useRealm(); const { computeTravelTime } = useTravel( Number(ADMIN_BANK_ENTITY_ID), Number(realmEntityId!), configManager.getSpeedConfig(DONKEY_ENTITY_TYPE), true, - ); const [isFeesOpen, setIsFeesOpen] = useState(false); + ); + const [isFeesOpen, setIsFeesOpen] = useState(false); const { data } = useQuery({ queryKey: ["capacitySpeedConfig"], - queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }), + queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }), refetchInterval: 10_000, }); - const donkeyConfig = useMemo(() => ({ - capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0), - speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0 - }), [data]); + const donkeyConfig = useMemo( + () => ({ + capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0), + speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0, + }), + [data], + ); const [isLoading, setIsLoading] = useState(false); const { bridgeStartWithdrawFromRealm } = useBridgeAsset(); @@ -84,7 +78,7 @@ export const BridgeOutStep1 = () => { }[] >([]); - const { getBalance } = useResourceBalance({entityId: Number(realmEntityId)}); + const { getBalance } = useResourceBalance({ entityId: Number(realmEntityId) }); const donkeyBalance = useMemo(() => { if (realmEntityId) { return getBalance(ResourcesIds.Donkey); @@ -93,26 +87,11 @@ export const BridgeOutStep1 = () => { } }, [getBalance, realmEntityId]); - const { data: playerRealms } = useEntities(); - const playerRealmsIdAndName = useMemo(() => { - return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => { - const realmModel = realm?.node?.entity?.models?.find(isS0EternumRealm); - return { - realmId: realmModel?.realm_id, - entityId: realm?.node?.entity_id, - name: getRealmNameById(realmModel?.realm_id ?? 0), - }; - }); - }, [playerRealms, getRealmNameById]); + const { playerRealms } = useEntities(); const travelTime = useMemo(() => { if (realmEntityId) { - return computeTravelTime( - Number(ADMIN_BANK_ENTITY_ID), - Number(realmEntityId!), - donkeyConfig.speed, - false, - ); + return computeTravelTime(Number(ADMIN_BANK_ENTITY_ID), Number(realmEntityId!), donkeyConfig.speed, false); } else { return 0; } @@ -250,7 +229,7 @@ export const BridgeOutStep1 = () => { {address ? :
-- Connect your wallet --
} - {playerRealmsIdAndName?.map((realm) => { + {playerRealms?.map((realm) => { return ( #{realm.realmId} - {realm.name} @@ -295,8 +274,7 @@ export const BridgeOutStep1 = () => {
- {donkeysNeeded} / {divideByPrecision(donkeyBalance)}{" "} - + {donkeysNeeded} / {divideByPrecision(donkeyBalance)}
{ const { address } = useAccount(); - const dojo = useDojo(); + const { playerRealms } = useEntities(); - const { data: playerRealms } = useEntities(); - const realmEntityIds = useMemo(() => { - return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => realm?.node?.entity_id) ?? []; - }, [playerRealms]); + console.log({ playerRealms }); - const { donkeyInfos } = useDonkeyArrivals(realmEntityIds); + const { donkeyInfos } = useDonkeyArrivals(playerRealms.map((realm) => realm.entityId)); const [isLoading, setIsLoading] = useState(false); const [isRefreshing, setIsRefreshing] = useState(false); @@ -46,7 +42,6 @@ export const BridgeOutStep2 = () => { }[] >([]); - const [refreshTrigger, setRefreshTrigger] = useState(0); /*const donkeysArrivals = useMemo(() => { if (bankPosition) { @@ -63,8 +58,6 @@ export const BridgeOutStep2 = () => { //useSyncEntity(donkeyArrivalsEntityIds); - - const { bridgeFinishWithdrawFromRealm } = useBridgeAsset(); const onFinishWithdrawFromBank = async () => { @@ -97,7 +90,8 @@ export const BridgeOutStep2 = () => { const updateResourcesFromSelectedDonkeys = (selectedDonkeyIds: Set) => { const allResources = Array.from(selectedDonkeyIds).flatMap( - (id) => donkeyInfos?.find((d) => d?.donkeyEntityId && BigInt(d.donkeyEntityId) === id)?.donkeyResourceBalances || [], + (id) => + donkeyInfos?.find((d) => d?.donkeyEntityId && BigInt(d.donkeyEntityId) === id)?.donkeyResourceBalances || [], ); setSelectedResourceIds(allResources.map((r) => r.resourceId as never)); @@ -250,7 +244,7 @@ export const BridgeOutStep2 = () => { className={`${ selectedDonkeys.has(BigInt(donkey?.donkeyEntityId || 0)) ? "bg-gold/10" : "" } hover:bg-gold/5 ${!isArrived ? "opacity-60" : "cursor-pointer"}`} - onClick={(e) => { + onClick={(e) => { if (!isArrived) return; const newSelected = new Set(selectedDonkeys); diff --git a/landing/src/hooks/gql/gql.ts b/landing/src/hooks/gql/gql.ts index 7effd4df3..dd58633a6 100644 --- a/landing/src/hooks/gql/gql.ts +++ b/landing/src/hooks/gql/gql.ts @@ -1,7 +1,5 @@ /* eslint-disable */ -import * as types from './graphql'; - - +import * as types from "./graphql"; /** * Map of all GraphQL operations in the project. @@ -15,49 +13,72 @@ import * as types from './graphql'; * Learn more about it here: https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#reducing-bundle-size */ const documents = { - "\n query getCapacitySpeedConfig($category: Enum!, $entityType: u32!) {\n s0EternumCapacityConfigModels(where: {category: $category }) {\n edges{\n node {\n weight_gram\n }\n }\n }\n s0EternumSpeedConfigModels(where: {entity_type: $entityType }) {\n edges{\n node {\n sec_per_km\n }\n }\n }\n }\n": types.GetCapacitySpeedConfigDocument, - "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n": types.GetEternumOwnerRealmIdsDocument, - "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {\n edges {\n node {\n entity_id\n entity_owner_id\n entity {\n models {\n __typename\n ... on s0_eternum_OwnedResourcesTracker {\n resource_types\n }\n ... on s0_eternum_Position {\n x\n y\n }\n ... on s0_eternum_ArrivalTime {\n arrives_at\n }\n ... on s0_eternum_Weight {\n value\n }\n }\n }\n }\n }\n }\n }\n": types.GetEternumEntityOwnerDocument, - "\n query getAccountTokens($accountAddress: String!) {\n tokenBalances(accountAddress: $accountAddress, limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": types.GetAccountTokensDocument, - "\n query getERC721Mints {\n tokenTransfers(accountAddress: \"0x0\", limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": types.GetErc721MintsDocument, - "\n query eternumStatistics {\n s0EternumAddressNameModels {\n totalCount\n }\n s0EternumHyperstructureModels {\n totalCount\n }\n s0EternumRealmModels {\n totalCount\n }\n s0EternumFragmentMineDiscoveredModels {\n totalCount\n }\n }\n": types.EternumStatisticsDocument, - "\n query getEntityPosition($entityIds: [u32!]!) {\n s0EternumPositionModels(where: { entity_idIN: $entityIds }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityPositionDocument, - "\n query getEntitiesResources($entityIds: [u32!]!) {\n s0EternumResourceModels(\n where: { \n entity_idIN: $entityIds\n }\n limit: 100\n ) {\n edges {\n node {\n entity_id\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntitiesResourcesDocument, + "\n query getCapacitySpeedConfig($category: Enum!, $entityType: u32!) {\n s0EternumCapacityConfigModels(where: {category: $category }) {\n edges{\n node {\n weight_gram\n }\n }\n }\n s0EternumSpeedConfigModels(where: {entity_type: $entityType }) {\n edges{\n node {\n sec_per_km\n }\n }\n }\n }\n": + types.GetCapacitySpeedConfigDocument, + "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }, limit: 1000) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n": + types.GetEternumOwnerRealmIdsDocument, + "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {\n edges {\n node {\n entity_id\n entity_owner_id\n entity {\n models {\n __typename\n ... on s0_eternum_OwnedResourcesTracker {\n resource_types\n }\n ... on s0_eternum_Position {\n x\n y\n }\n ... on s0_eternum_ArrivalTime {\n arrives_at\n }\n ... on s0_eternum_Weight {\n value\n }\n }\n }\n }\n }\n }\n }\n": + types.GetEternumEntityOwnerDocument, + "\n query getAccountTokens($accountAddress: String!) {\n tokenBalances(accountAddress: $accountAddress, limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": + types.GetAccountTokensDocument, + '\n query getERC721Mints {\n tokenTransfers(accountAddress: "0x0", limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n': + types.GetErc721MintsDocument, + "\n query eternumStatistics {\n s0EternumAddressNameModels {\n totalCount\n }\n s0EternumHyperstructureModels {\n totalCount\n }\n s0EternumRealmModels {\n totalCount\n }\n s0EternumFragmentMineDiscoveredModels {\n totalCount\n }\n }\n": + types.EternumStatisticsDocument, + "\n query getEntityPosition($entityIds: [u32!]!) {\n s0EternumPositionModels(where: { entity_idIN: $entityIds }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n": + types.GetEntityPositionDocument, + "\n query getEntitiesResources($entityIds: [u32!]!) {\n s0EternumResourceModels(\n where: { \n entity_idIN: $entityIds\n }\n limit: 100\n ) {\n edges {\n node {\n entity_id\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n": + types.GetEntitiesResourcesDocument, }; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getCapacitySpeedConfig($category: Enum!, $entityType: u32!) {\n s0EternumCapacityConfigModels(where: {category: $category }) {\n edges{\n node {\n weight_gram\n }\n }\n }\n s0EternumSpeedConfigModels(where: {entity_type: $entityType }) {\n edges{\n node {\n sec_per_km\n }\n }\n }\n }\n"): typeof import('./graphql').GetCapacitySpeedConfigDocument; +export function graphql( + source: "\n query getCapacitySpeedConfig($category: Enum!, $entityType: u32!) {\n s0EternumCapacityConfigModels(where: {category: $category }) {\n edges{\n node {\n weight_gram\n }\n }\n }\n s0EternumSpeedConfigModels(where: {entity_type: $entityType }) {\n edges{\n node {\n sec_per_km\n }\n }\n }\n }\n", +): typeof import("./graphql").GetCapacitySpeedConfigDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEternumOwnerRealmIdsDocument; +export function graphql( + source: "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }, limit: 1000) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n", +): typeof import("./graphql").GetEternumOwnerRealmIdsDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {\n edges {\n node {\n entity_id\n entity_owner_id\n entity {\n models {\n __typename\n ... on s0_eternum_OwnedResourcesTracker {\n resource_types\n }\n ... on s0_eternum_Position {\n x\n y\n }\n ... on s0_eternum_ArrivalTime {\n arrives_at\n }\n ... on s0_eternum_Weight {\n value\n }\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEternumEntityOwnerDocument; +export function graphql( + source: "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {\n edges {\n node {\n entity_id\n entity_owner_id\n entity {\n models {\n __typename\n ... on s0_eternum_OwnedResourcesTracker {\n resource_types\n }\n ... on s0_eternum_Position {\n x\n y\n }\n ... on s0_eternum_ArrivalTime {\n arrives_at\n }\n ... on s0_eternum_Weight {\n value\n }\n }\n }\n }\n }\n }\n }\n", +): typeof import("./graphql").GetEternumEntityOwnerDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getAccountTokens($accountAddress: String!) {\n tokenBalances(accountAddress: $accountAddress, limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetAccountTokensDocument; +export function graphql( + source: "\n query getAccountTokens($accountAddress: String!) {\n tokenBalances(accountAddress: $accountAddress, limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n", +): typeof import("./graphql").GetAccountTokensDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getERC721Mints {\n tokenTransfers(accountAddress: \"0x0\", limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetErc721MintsDocument; +export function graphql( + source: '\n query getERC721Mints {\n tokenTransfers(accountAddress: "0x0", limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n', +): typeof import("./graphql").GetErc721MintsDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query eternumStatistics {\n s0EternumAddressNameModels {\n totalCount\n }\n s0EternumHyperstructureModels {\n totalCount\n }\n s0EternumRealmModels {\n totalCount\n }\n s0EternumFragmentMineDiscoveredModels {\n totalCount\n }\n }\n"): typeof import('./graphql').EternumStatisticsDocument; +export function graphql( + source: "\n query eternumStatistics {\n s0EternumAddressNameModels {\n totalCount\n }\n s0EternumHyperstructureModels {\n totalCount\n }\n s0EternumRealmModels {\n totalCount\n }\n s0EternumFragmentMineDiscoveredModels {\n totalCount\n }\n }\n", +): typeof import("./graphql").EternumStatisticsDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getEntityPosition($entityIds: [u32!]!) {\n s0EternumPositionModels(where: { entity_idIN: $entityIds }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntityPositionDocument; +export function graphql( + source: "\n query getEntityPosition($entityIds: [u32!]!) {\n s0EternumPositionModels(where: { entity_idIN: $entityIds }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n", +): typeof import("./graphql").GetEntityPositionDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getEntitiesResources($entityIds: [u32!]!) {\n s0EternumResourceModels(\n where: { \n entity_idIN: $entityIds\n }\n limit: 100\n ) {\n edges {\n node {\n entity_id\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntitiesResourcesDocument; - +export function graphql( + source: "\n query getEntitiesResources($entityIds: [u32!]!) {\n s0EternumResourceModels(\n where: { \n entity_idIN: $entityIds\n }\n limit: 100\n ) {\n edges {\n node {\n entity_id\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n", +): typeof import("./graphql").GetEntitiesResourcesDocument; export function graphql(source: string) { return (documents as any)[source] ?? {}; diff --git a/landing/src/hooks/helpers/useEntities.tsx b/landing/src/hooks/helpers/useEntities.tsx index c0b9c3451..f605b2087 100644 --- a/landing/src/hooks/helpers/useEntities.tsx +++ b/landing/src/hooks/helpers/useEntities.tsx @@ -1,36 +1,22 @@ import { useAccount } from "@starknet-react/core"; import { useQuery } from "@tanstack/react-query"; +import { useMemo } from "react"; import { execute } from "../gql/execute"; import { GET_ETERNUM_OWNER_REALM_IDS } from "../query/entities"; +import { useRealm } from "./useRealms"; -export const useEntities = () => { - /*const { - account: { account }, - setup: { - components: { Realm, Owner }, - }, - } = useDojo();*/ - - const { address } = useAccount(); - - /* const dojoAddress = ContractAddress(account?.address || ""); - - // Get all realms - const allRealms = useEntityQuery([Has(Realm)]); +export interface S0EternumRealm { + __typename: "s0_eternum_Realm"; + realm_id: number; +} - const filterPlayerRealms = useMemo(() => { - return allRealms.filter((id) => { - const owner = getComponentValue(Owner, id); - return owner && ContractAddress(owner.address) === ContractAddress(dojoAddress); - }); - }, [allRealms, dojoAddress]); +export function isS0EternumRealm(model: any): model is S0EternumRealm { + return model?.__typename === "s0_eternum_Realm"; +} - const playerRealms = useMemo(() => { - return filterPlayerRealms.map((id) => { - const realm = getComponentValue(Realm, id); - return realm; - }); - }, [filterPlayerRealms]);*/ +export const useEntities = () => { + const { address } = useAccount(); + const { getRealmNameById } = useRealm(); const { data, isLoading } = useQuery({ queryKey: ["entityResources", address], @@ -38,8 +24,24 @@ export const useEntities = () => { refetchInterval: 10_000, }); + const playerRealms = useMemo(() => { + if (!data) return []; + + return data.s0EternumOwnerModels?.edges + ?.map((realm) => { + const realmModel = realm?.node?.entity?.models?.find(isS0EternumRealm); + if (!realmModel) return null; + return { + realmId: realmModel?.realm_id, + entityId: realm?.node?.entity_id, + name: getRealmNameById(realmModel?.realm_id ?? 0), + }; + }) + .filter(Boolean) as { realmId: number; entityId: number; name: string }[]; + }, [data, getRealmNameById]); + return { - data, - //playerRealms, + playerRealms, + isLoading, }; }; diff --git a/landing/src/hooks/query/entities.tsx b/landing/src/hooks/query/entities.tsx index 430d61194..0abfb41db 100644 --- a/landing/src/hooks/query/entities.tsx +++ b/landing/src/hooks/query/entities.tsx @@ -2,7 +2,7 @@ import { graphql } from "../gql"; export const GET_ETERNUM_OWNER_REALM_IDS = graphql(` query getEternumOwnerRealmIds($accountAddress: ContractAddress!) { - s0EternumOwnerModels(where: { address: $accountAddress }) { + s0EternumOwnerModels(where: { address: $accountAddress }, limit: 1000) { edges { node { address