-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
88 lines (82 loc) · 2.65 KB
/
App.tsx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useEffect } from 'react';
import {
Box,
Button,
Container,
VStack,
useColorMode,
IconButton,
Heading,
Card,
CardBody,
} from '@chakra-ui/react';
import { SunIcon, MoonIcon } from '@chakra-ui/icons';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import WalletDetails from './components/WalletDetails';
import TransferForm from './components/TransferForm';
import { transferFormSchema, type TransferFormData } from './utils/validation';
import { useWalletManager } from './hooks/useWalletManager';
import { useBalance } from './hooks/useBalance';
import { useTransfer } from './hooks/useTransfer';
function App() {
const { colorMode, toggleColorMode } = useColorMode();
const { walletManager, address, connectWallet } = useWalletManager();
const { balance, refetchBalance } = useBalance(address, walletManager);
const transferMutation = useTransfer(address, walletManager, refetchBalance);
const { register, handleSubmit, formState: { errors }, reset } = useForm<TransferFormData>({
resolver: zodResolver(transferFormSchema),
defaultValues: { amount: '0.000001' },
});
const onSubmit = (data: TransferFormData) => {
if (!balance || Number(data.amount) > balance) {
return;
}
transferMutation.mutate(data);
reset();
};
useEffect(() => {
if (walletManager) {
connectWallet();
}
}, [walletManager, connectWallet]);
return (
<Container maxW="container.sm" py={8}>
<VStack spacing={6} align="stretch">
<Box display="flex" justifyContent="flex-end">
<IconButton
aria-label="Toggle color mode"
icon={colorMode === 'light' ? <MoonIcon /> : <SunIcon />}
onClick={toggleColorMode}
/>
</Box>
<Card>
<CardBody>
<VStack spacing={4} align="stretch">
<Heading size="md">Cosmos Wallet</Heading>
{!address ? (
<Button onClick={connectWallet}>Connect Keplr</Button>
) : (
<>
<WalletDetails
address={address}
balance={balance ?? '0'}
onRefresh={refetchBalance}
/>
<TransferForm
register={register}
errors={errors}
handleSubmit={handleSubmit}
onSubmit={onSubmit}
isLoading={transferMutation.isMutating}
/>
</>
)}
</VStack>
</CardBody>
</Card>
</VStack>
</Container>
);
}
export default App;