forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFundAccountButton.tsx
More file actions
74 lines (68 loc) · 2.21 KB
/
Copy pathFundAccountButton.tsx
File metadata and controls
74 lines (68 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { useState, useTransition } from "react";
import { useNotification } from "../hooks/useNotification.ts";
import { useWallet } from "../hooks/useWallet.ts";
import { Button, Tooltip } from "@stellar/design-system";
import { getFriendbotUrl } from "../util/friendbot";
import { useWalletBalance } from "../hooks/useWalletBalance.ts";
const FundAccountButton: React.FC = () => {
const { addNotification } = useNotification();
const [isPending, startTransition] = useTransition();
const { isFunded, isLoading } = useWalletBalance();
const [isTooltipVisible, setIsTooltipVisible] = useState(false);
const { address } = useWallet();
if (!address) return null;
const handleFundAccount = () => {
startTransition(async () => {
try {
const response = await fetch(getFriendbotUrl(address));
if (response.ok) {
addNotification("Account funded successfully!", "success");
} else {
const body: unknown = await response.json();
if (
body !== null &&
typeof body === "object" &&
"detail" in body &&
typeof body.detail === "string"
) {
addNotification(`Error funding account: ${body.detail}`, "error");
} else {
addNotification("Error funding account: Unknown error", "error");
}
}
} catch {
addNotification("Error funding account. Please try again.", "error");
}
});
};
return (
<div
onMouseEnter={() => setIsTooltipVisible(true)}
onMouseLeave={() => setIsTooltipVisible(false)}
>
<Tooltip
isVisible={isTooltipVisible}
isContrast
title="Fund Account"
placement="bottom"
triggerEl={
<Button
disabled={isPending || isLoading || isFunded}
onClick={handleFundAccount}
variant="primary"
size="md"
>
Fund Account
</Button>
}
>
<div style={{ width: "13em" }}>
{isFunded
? "Account is already funded"
: "Fund your account using the Stellar Friendbot"}
</div>
</Tooltip>
</div>
);
};
export default FundAccountButton;