|
| 1 | +import React from 'react'; |
| 2 | +import { TokenGate } from '.'; |
| 3 | +import { NETWORKS, Provider, useTokenBalance, useWallet } from '@web3-ui/hooks'; |
| 4 | +import { Text } from '@chakra-ui/layout'; |
| 5 | +import { Button } from '@chakra-ui/react'; |
| 6 | +export default { |
| 7 | + title: 'Components/TokenGate', |
| 8 | + component: TokenGate, |
| 9 | + parameters: { |
| 10 | + // TODO: Fix window.ethereum is undefined breaking chromatic |
| 11 | + chromatic: { disableSnapshot: true }, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +const Component = ({ ...props }) => { |
| 16 | + /** |
| 17 | + * requiredQuantity was done this way because when requiredQuantity is not passed to the component, the required quantity |
| 18 | + * should default to 1 not 0 |
| 19 | + */ |
| 20 | + const requiredQuantity = props.requiredQuantity === undefined ? 1 : props.requiredQuantity; |
| 21 | + return ( |
| 22 | + <> |
| 23 | + <TokenGate walletBalance={props.walletBalance} {...props}> |
| 24 | + <Text> |
| 25 | + {`This is the child component of TokenGate. You were able to access this component because you hold at least ${requiredQuantity} token. Your token balance: ${props.walletBalance} `} |
| 26 | + </Text> |
| 27 | + </TokenGate> |
| 28 | + </> |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +const WithUseWallet = ({ ...props }) => { |
| 33 | + const { connected, connectWallet, connection } = useWallet(); |
| 34 | + const { formattedBalance, error } = useTokenBalance({ |
| 35 | + // GTC token contract address |
| 36 | + tokenAddress: '0xde30da39c46104798bb5aa3fe8b9e0e1f348163f', |
| 37 | + accountAddress: connection.userAddress!, |
| 38 | + }); |
| 39 | + // TokenGate only returned if there is a connection and a balance. Done this way to accomplish rendering the loading state. |
| 40 | + // Using the loading state from useTokenBalance would not work because loading status changes simultaneously with connected status |
| 41 | + if (connected && formattedBalance) { |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <TokenGate |
| 45 | + walletBalance={Number(formattedBalance)} |
| 46 | + requiredQuantity={props.requiredQuantity} |
| 47 | + {...props} |
| 48 | + > |
| 49 | + <Text> |
| 50 | + {`This is the child component of TokenGate. You were able to access this component because you hold at least ${ |
| 51 | + props.requiredQuantity === undefined ? 1 : props.requiredQuantity |
| 52 | + } of the required token: GTC`} |
| 53 | + </Text> |
| 54 | + </TokenGate> |
| 55 | + </> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + if (error) { |
| 60 | + return <Text>Error occured while trying to fetch balance.</Text>; |
| 61 | + } |
| 62 | + // Using the loading state from useTokenBalance hook does not work here because connected status and loading status change simultaneously. |
| 63 | + return !connected ? ( |
| 64 | + <Button onClick={connectWallet}>Connect Wallet</Button> |
| 65 | + ) : ( |
| 66 | + <Text>Loading...</Text> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +export const Default = () => <Component walletBalance={1} />; |
| 71 | + |
| 72 | +export const UsingWeb3Hooks = () => { |
| 73 | + return ( |
| 74 | + <Provider network={NETWORKS.mainnet}> |
| 75 | + <WithUseWallet requiredQuantity={0} /> |
| 76 | + </Provider> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +export const AccessGrantedDefault = () => <Component walletBalance={150} requiredQuantity={100} />; |
| 81 | + |
| 82 | +export const AccessDeniedDefault = () => ( |
| 83 | + <Component walletBalance={0} requiredQuantity={1000} label='Denied' /> |
| 84 | +); |
| 85 | + |
| 86 | +/** |
| 87 | + * Example of custom access denied node for the deniedMessage prop |
| 88 | + */ |
| 89 | +const DeniedAccess = props => ( |
| 90 | + <div> |
| 91 | + <h1>This is a custom component for when access is denied</h1> |
| 92 | + <ul> |
| 93 | + <li>Make sure your wallet is connected</li> |
| 94 | + <li>Verify you are connected to the correct address</li> |
| 95 | + <li> |
| 96 | + {`Make sure you hold the number of tokens required to access this component: |
| 97 | + ${props.requiredQuantity === undefined ? 1 : props.requiredQuantity}`} |
| 98 | + </li> |
| 99 | + <li>Not providing a "deniedMessage" will return null when access is denied</li> |
| 100 | + </ul> |
| 101 | + </div> |
| 102 | +); |
| 103 | + |
| 104 | +export const AccessDeniedWithCustomMessage = () => ( |
| 105 | + <Component |
| 106 | + requiredQuantity={1000} |
| 107 | + deniedMessage={<DeniedAccess requiredQuantity={1000} />} |
| 108 | + label='Denied With Message' |
| 109 | + /> |
| 110 | +); |
0 commit comments