import { compact } from 'lodash-unified' ; import { isSameAddress , useChainId , getChainIdFromName , ChainId } from '@masknet/web3-shared-evm' ; import { RedPacketJSONPayload , RedPacketStatus , RedPacketAvailability } from '../../types' ; import { useAvailability } from './useAvailability' ; param payload export function useAvailabilityComputed ( account , payload ) { const chainId = useChainId ( ) ; const parsedChainId = getChainIdFromName ( payload . network ?? '' ) ?? ChainId . Mainnet ; const asyncResult = useAvailability ( payload . contract_version , account , payload . rpid , parsedChainId ) ; const result = asyncResult ; const availability = result . value as RedPacketAvailability ; if ( ! availability ) return { ... asyncResult , payload , computed : { canClaim : false , canRefund : false , listOfStatus : [ ] as RedPacketStatus [ ] , } , } ; const isEmpty = availability . balance === '0' ; const isExpired = availability . expired ; const isClaimed = availability . claimed_amount ? availability . claimed_amount !== '0' : availability . ifclaimed ; const isRefunded = isEmpty && Number . parseInt ( availability . claimed , 10 ) < Number . parseInt ( availability . total , 10 ) ; const isCreator = isSameAddress ( payload ?. sender . address ?? '' , account ) ; const isPasswordValid = Boolean ( payload . password && payload . password !== 'PASSWORD INVALID' ) ; return { ... asyncResult , computed : { canFetch : parsedChainId === chainId , canClaim : ! isExpired && ! isEmpty && ! isClaimed && parsedChainId === chainId && isPasswordValid , canRefund : isExpired && ! isEmpty && isCreator && parsedChainId === chainId , canSend : ! isEmpty && ! isExpired && ! isRefunded && isCreator && parsedChainId === chainId , isPasswordValid , listOfStatus : compact ( [ isClaimed ? RedPacketStatus . claimed : undefined , isEmpty ? RedPacketStatus . empty : undefined , isRefunded ? RedPacketStatus . refunded : undefined , isExpired ? RedPacketStatus . expired : undefined , ] ) , } , } ; }