-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor QRModal and add FundRequirement component
- Loading branch information
1 parent
c79bbdd
commit 3b9183f
Showing
4 changed files
with
114 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { copyToClipboard } from "@/helpers/copyToClipboard"; | ||
import { useEthers, useServices, useModals } from "@/hooks"; | ||
import { message, Flex, Typography, Spin, Button } from "antd"; | ||
import { | ||
Dispatch, | ||
SetStateAction, | ||
useState, | ||
useMemo, | ||
useCallback, | ||
} from "react"; | ||
import { useInterval } from "usehooks-ts"; | ||
|
||
export const FundRequirement = ({ | ||
setReceivedFunds, | ||
serviceHash, | ||
address, | ||
requirement, | ||
}: { | ||
setReceivedFunds: Dispatch<SetStateAction<{ [address: string]: boolean }>>; | ||
serviceHash: string; | ||
address: string; | ||
requirement: number; | ||
}) => { | ||
const { getETHBalance } = useEthers(); | ||
const { getServiceFromState } = useServices(); | ||
const { setQrModalOpen, setQrModalAddress } = useModals(); | ||
|
||
const [isPollingBalance, setIsPollingBalance] = useState(true); | ||
|
||
const rpc = useMemo(() => { | ||
const service = getServiceFromState(serviceHash); | ||
if (!service) return ""; | ||
return service.ledger?.rpc || ""; | ||
}, [getServiceFromState, serviceHash]); | ||
|
||
const handleCopy = useCallback(() => { | ||
copyToClipboard(address); | ||
message.success("Copied to clipboard"); | ||
}, [address]); | ||
|
||
const handleShowQr = useCallback(() => { | ||
setQrModalAddress(address); | ||
setQrModalOpen(true); | ||
}, [address, setQrModalAddress, setQrModalOpen]); | ||
|
||
useInterval( | ||
() => | ||
getETHBalance(address, rpc).then((balance) => { | ||
if (balance && balance >= requirement) { | ||
setIsPollingBalance(false); | ||
setReceivedFunds((prev: { [address: string]: boolean }) => ({ | ||
...prev, | ||
[address]: true, | ||
})); | ||
} | ||
}), | ||
isPollingBalance ? 3000 : null, | ||
); | ||
|
||
return ( | ||
<Flex gap={8} vertical key={address}> | ||
<Typography.Text> | ||
Send {requirement} XDAI to: {address}{" "} | ||
{isPollingBalance && <Spin size="small" />} | ||
</Typography.Text> | ||
<Flex gap={8}> | ||
<Button type="primary" onClick={handleCopy}> | ||
Copy address | ||
</Button> | ||
<Button type="default" onClick={handleShowQr}> | ||
Show QR | ||
</Button> | ||
</Flex> | ||
</Flex> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters