|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { ZcashYellowPNG } from '../../assets'; |
| 3 | +import PageHeading from '../../components/PageHeading/PageHeading'; |
| 4 | +import useBalance from '../../hooks/useBalance'; |
| 5 | +import { zatsToZec } from '../../utils'; |
| 6 | +import Button from 'src/components/Button/Button'; |
| 7 | +import { useWebZjsActions } from 'src/hooks'; |
| 8 | +import { usePczt } from 'src/hooks/usePCZT'; |
| 9 | +import { TransferResult } from 'src/components/TransferCards/TransferResult'; |
| 10 | + |
| 11 | +export enum ShieldStatus { |
| 12 | + DEFAULT = 'default', |
| 13 | + SHIELDING = 'shielding', |
| 14 | +} |
| 15 | + |
| 16 | +export function ShieldBalance(): React.JSX.Element { |
| 17 | + const { unshieldedBalance } = useBalance(); |
| 18 | + const [addresses, setAddresses] = useState<{ |
| 19 | + unifiedAddress: string; |
| 20 | + transparentAddress: string; |
| 21 | + }>({ |
| 22 | + unifiedAddress: '', |
| 23 | + transparentAddress: '', |
| 24 | + }); |
| 25 | + |
| 26 | + const { getAccountData } = useWebZjsActions(); |
| 27 | + const { handlePcztShieldTransaction, pcztTransferStatus } = usePczt(); |
| 28 | + const [shieldStatus, setShieldStatus] = useState(ShieldStatus.DEFAULT); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const fetchData = async () => { |
| 32 | + const data = await getAccountData(); |
| 33 | + if (data) |
| 34 | + setAddresses({ |
| 35 | + unifiedAddress: data.unifiedAddress, |
| 36 | + transparentAddress: data.transparentAddress, |
| 37 | + }); |
| 38 | + }; |
| 39 | + fetchData(); |
| 40 | + }, [getAccountData]); |
| 41 | + |
| 42 | + |
| 43 | + const handleShieldBalance = () => { |
| 44 | + setShieldStatus(ShieldStatus.SHIELDING); |
| 45 | + handlePcztShieldTransaction(1, addresses.unifiedAddress, unshieldedBalance.toString()); |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <div className="flex flex-col w-full"> |
| 50 | + <PageHeading title="Shield Balance"> |
| 51 | + <div className="flex items-center gap-2.5"> |
| 52 | + <span className="text-black text-base font-normal font-inter leading-tight"> |
| 53 | + Available unshielded balance: |
| 54 | + </span> |
| 55 | + <div className="px-4 py-2 bg-[#e8e8e8] rounded-3xl flex items-center gap-2.5"> |
| 56 | + <img |
| 57 | + src={ZcashYellowPNG} |
| 58 | + alt="Zcash Yellow" |
| 59 | + className="w-5 h-5" |
| 60 | + /> |
| 61 | + <span className="text-[#434343] text-base font-semibold font-inter leading-tight"> |
| 62 | + {zatsToZec(unshieldedBalance)} ZEC |
| 63 | + </span> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + </PageHeading> |
| 67 | + {shieldStatus === ShieldStatus.DEFAULT && ( |
| 68 | + <div className="min-h-[460px] px-12 py-6 bg-white rounded-3xl border border-[#afafaf] flex-col justify-start items-center gap-6 inline-flex"> |
| 69 | + <div className="self-stretch h-[413px] flex-col justify-center items-center gap-3 flex"> |
| 70 | + <div className="self-stretch justify-start items-center gap-2 inline-flex"> |
| 71 | + <div className="grow shrink basis-0 text-black text-base font-medium font-['Roboto'] leading-normal"> |
| 72 | + To: |
| 73 | + </div> |
| 74 | + <div className="p-3 rounded-xl justify-start items-center gap-2 flex"> |
| 75 | + <div className="text-[#4f4f4f] text-base font-normal font-['Roboto'] break-all leading-normal"> |
| 76 | + {addresses.unifiedAddress} |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + <div className="self-stretch justify-start items-center gap-2 inline-flex"> |
| 81 | + <div className="grow shrink basis-0 text-black text-base font-normal font-['Roboto'] leading-normal"> |
| 82 | + Amount: |
| 83 | + </div> |
| 84 | + <div className="px-4 py-1.5 bg-[#e8e8e8] rounded-3xl justify-center items-center gap-2.5 flex"> |
| 85 | + <div className="text-[#0e0e0e] text-sm font-medium font-['Roboto'] leading-[21px]"> |
| 86 | + {zatsToZec(unshieldedBalance)} ZEC |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + <div className="self-stretch pt-6 flex-col justify-center items-center gap-3 flex"> |
| 91 | + <div className="justify-start items-start inline-flex"> |
| 92 | + <Button |
| 93 | + onClick={handleShieldBalance} |
| 94 | + label={'Shield balance'} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + )} |
| 101 | + {shieldStatus === ShieldStatus.SHIELDING && ( |
| 102 | + <TransferResult |
| 103 | + pcztTransferStatus={pcztTransferStatus} |
| 104 | + resetForm={() => { }} |
| 105 | + isShieldTransaction={true} |
| 106 | + /> |
| 107 | + )} |
| 108 | + |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
0 commit comments