import React, { useState, useEffect } from "react"; import { IInventoryItem, IStakingSource, IClaimableResource } from "typings"; import CardLayout from "../Layouts/CardLayout"; const StakingSource = (props: { stakingSource: IStakingSource; inventoryItems: IInventoryItem[] | null | undefined; handleAddItem: ( inventoryItem: IInventoryItem, stakingSource: IStakingSource ) => void; claimResource: ( stakingSource: IStakingSource, claimedResource: IClaimableResource ) => boolean; }) => { const [isMining, setIsMining] = useState(false); const [miningTime, setMiningTime] = useState(0); const [claimableResource, setClaimableResource] = useState(null); const [activeInventoryItem, setActiveInventoryItem] = useState(null); const [activeTier, setActiveTier] = useState(0); useEffect(() => { if (isMining && activeInventoryItem) { const intervalId = setInterval(() => { setMiningTime((prev) => prev - 1); }, 1000); if (miningTime === 0) { setIsMining(false); // Get a random resource from available wells let randResource = props.stakingSource.resourceWells[ Math.floor(Math.random() * props.stakingSource.resourceWells.length) ]; // Get yield based on the tier const totalYield = activeInventoryItem.storeItem.tiers[activeTier].tier; // Construct the claimableResource const claimableResource: IClaimableResource = { resourceType: randResource.resourceType, balance: totalYield, }; setClaimableResource(claimableResource); clearInterval(intervalId); setMiningTime(activeInventoryItem.storeItem.timeToClaim); } return () => { clearInterval(intervalId); }; } }, [isMining, miningTime]); const isChecked = (item: IInventoryItem) => { if (!props.stakingSource.inventoryItem) return false; return item.id === props.stakingSource.inventoryItem.id; }; const handleStartMining = () => { if (props.stakingSource.inventoryItem) { setActiveInventoryItem(props.stakingSource.inventoryItem); setMiningTime(props.stakingSource.inventoryItem.storeItem.timeToClaim); setActiveTier(props.stakingSource.inventoryItem.currentTierIndex); setIsMining(true); } }; const handleClaim = () => { if (!claimableResource) return; if (props.claimResource(props.stakingSource, claimableResource)) { setClaimableResource(null); } }; const renderButton = () => { if (props.stakingSource.inventoryItem) { if (!claimableResource && !isMining) { return ( ); } } if (isMining) return ( ); if (claimableResource) { return ( ); } }; return (

{props.stakingSource.name}

{props.stakingSource.description}

Resources

    {props.stakingSource.resourceWells.map((resourceWell, id) => (
  • {resourceWell.resourceType.name}{" "} {resourceWell.supply}
  • ))}

Status

  • Operational

Equipment

{!isMining ? props.inventoryItems && props.inventoryItems.map((item, id) => (
props.handleAddItem(item, props.stakingSource) } />
)) : props.stakingSource.inventoryItem && (

{props.stakingSource.inventoryItem.storeItem.name}

)}
{renderButton()}
); }; export default StakingSource;