|
| 1 | +import React, { MouseEventHandler, ReactNode } from "react"; |
| 2 | +import { Button, Icon, Stack, Text, useColorModeValue } from "@chakra-ui/react"; |
| 3 | +import { FiAlertTriangle } from "react-icons/fi"; |
| 4 | +import { WalletStatus } from "./types"; |
| 5 | +import { IoWallet } from "react-icons/io5"; |
| 6 | +import { ConnectWalletType } from "./types"; |
| 7 | + |
| 8 | +export const ConnectWalletButton = ({ |
| 9 | + buttonText, |
| 10 | + isLoading, |
| 11 | + isDisabled, |
| 12 | + icon, |
| 13 | + onClickConnectBtn, |
| 14 | +}: ConnectWalletType) => { |
| 15 | + return ( |
| 16 | + <Button |
| 17 | + minW="fit-content" |
| 18 | + colorScheme="primary" |
| 19 | + size="lg" |
| 20 | + isLoading={isLoading} |
| 21 | + isDisabled={isDisabled} |
| 22 | + onClick={onClickConnectBtn} |
| 23 | + > |
| 24 | + <Icon as={icon ? icon : IoWallet} mr={2} /> |
| 25 | + {buttonText ? buttonText : "Connect Wallet"} |
| 26 | + </Button> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +export const Disconnect = ({ |
| 31 | + buttonText, |
| 32 | + onClick, |
| 33 | +}: { |
| 34 | + buttonText: string; |
| 35 | + onClick: MouseEventHandler<HTMLButtonElement>; |
| 36 | +}) => { |
| 37 | + return ( |
| 38 | + <ConnectWalletButton buttonText={buttonText} onClickConnectBtn={onClick} /> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +export const Connected = ({ |
| 43 | + buttonText, |
| 44 | + onClick, |
| 45 | +}: { |
| 46 | + buttonText: string; |
| 47 | + onClick: MouseEventHandler<HTMLButtonElement>; |
| 48 | +}) => { |
| 49 | + return ( |
| 50 | + <ConnectWalletButton buttonText={buttonText} onClickConnectBtn={onClick} /> |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +export const Connecting = () => { |
| 55 | + return <ConnectWalletButton isLoading={true} />; |
| 56 | +}; |
| 57 | + |
| 58 | +export const Rejected = ({ |
| 59 | + buttonText, |
| 60 | + wordOfWarning, |
| 61 | +}: { |
| 62 | + buttonText: string; |
| 63 | + wordOfWarning?: string; |
| 64 | +}) => { |
| 65 | + return ( |
| 66 | + <Stack> |
| 67 | + <ConnectWalletButton buttonText={buttonText} isDisabled={true} /> |
| 68 | + <Stack |
| 69 | + isInline={true} |
| 70 | + borderRadius="md" |
| 71 | + bg={useColorModeValue("orange.200", "orange.300")} |
| 72 | + color="blackAlpha.900" |
| 73 | + p={4} |
| 74 | + spacing={1} |
| 75 | + > |
| 76 | + <Icon as={FiAlertTriangle} mt={1} /> |
| 77 | + <Text> |
| 78 | + <Text fontWeight="semibold" as="span"> |
| 79 | + Warning:  |
| 80 | + </Text> |
| 81 | + {wordOfWarning} |
| 82 | + </Text> |
| 83 | + </Stack> |
| 84 | + </Stack> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +export const NotExist = ({ buttonText }: { buttonText: string }) => { |
| 89 | + return <ConnectWalletButton buttonText={buttonText} isDisabled={true} />; |
| 90 | +}; |
| 91 | + |
| 92 | +export const WalletConnectComponent = ({ |
| 93 | + walletStatus, |
| 94 | + disconnect, |
| 95 | + connecting, |
| 96 | + connected, |
| 97 | + rejected, |
| 98 | + notExist, |
| 99 | +}: { |
| 100 | + walletStatus: WalletStatus; |
| 101 | + disconnect: ReactNode; |
| 102 | + connecting: ReactNode; |
| 103 | + connected: ReactNode; |
| 104 | + rejected: ReactNode; |
| 105 | + notExist: ReactNode; |
| 106 | +}) => { |
| 107 | + switch (walletStatus) { |
| 108 | + case WalletStatus.NotInit: |
| 109 | + return <>{disconnect}</>; |
| 110 | + case WalletStatus.Loading: |
| 111 | + return <>{connecting}</>; |
| 112 | + case WalletStatus.Loaded: |
| 113 | + return <>{connected}</>; |
| 114 | + case WalletStatus.Rejected: |
| 115 | + return <>{rejected}</>; |
| 116 | + case WalletStatus.NotExist: |
| 117 | + return <>{notExist}</>; |
| 118 | + default: |
| 119 | + return <>{disconnect}</>; |
| 120 | + } |
| 121 | +}; |
0 commit comments