forked from labscommunity/arweave-wallet-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd2da96
commit 547fb99
Showing
9 changed files
with
191 additions
and
30 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,111 @@ | ||
import { ChevronDownIcon } from "@iconicicons/react"; | ||
import useAddress from "../hooks/active_address"; | ||
import useConnection from "../hooks/connection"; | ||
import useBalance from "../hooks/balance"; | ||
import { formatAddress } from "../utils"; | ||
import type { Radius } from "./Provider"; | ||
import type { HTMLProps } from "react"; | ||
import styled from "styled-components"; | ||
import { Button } from "./Button"; | ||
|
||
export default function ConnectButton({ accent, onClick, ...props }: HTMLProps<HTMLButtonElement> & Props) { | ||
// connection | ||
const { | ||
connected, | ||
connect, | ||
disconnect | ||
} = useConnection(); | ||
|
||
// active address | ||
const address = useAddress(); | ||
|
||
// balance | ||
const balance = useBalance(); | ||
|
||
return ( | ||
<Wrapper | ||
accent={accent} | ||
onClick={async (e) => { | ||
if (!connected) await connect(); | ||
else await disconnect(); | ||
|
||
if (onClick) return onClick(e); | ||
}} | ||
{...props as any} | ||
> | ||
{(connected && ( | ||
<> | ||
<Balance> | ||
{balance.toLocaleString(undefined, { maximumFractionDigits: 2 }) + " AR"} | ||
</Balance> | ||
<ProfileSection> | ||
<Avatar src="https://arweave.net/mzv2LMPSpoYcCPNtfq-IvB5pgnfk2k4_5XCCBefkZ_A" /> | ||
{formatAddress(address || "", 5)} | ||
<ExpandIcon /> | ||
</ProfileSection> | ||
</> | ||
)) || ( | ||
<ConnectText> | ||
Connect Wallet | ||
</ConnectText> | ||
)} | ||
</Wrapper> | ||
); | ||
} | ||
|
||
const radius: Record<Radius, number> = { | ||
default: 18, | ||
minimal: 10, | ||
none: 0 | ||
}; | ||
|
||
const Wrapper = styled(Button)<{ accent?: string; }>` | ||
border-radius: ${props => radius[props.theme.themeConfig.radius] + "px"}; | ||
text-transform: none; | ||
padding: 0.3rem; | ||
background-color: ${(props) => props.accent || `rgb(${props.theme.theme})`}; | ||
`; | ||
|
||
const ConnectText = styled.span` | ||
line-height: 2.6rem; | ||
padding: 0 .9rem; | ||
`; | ||
|
||
const ProfileSection = styled.div` | ||
display: flex; | ||
align-items: center; | ||
background-color: rgb(${props => props.theme.background}, .2); | ||
height: 2.6rem; | ||
border-radius: ${props => radius[props.theme.themeConfig.radius] - 3 + "px"}; | ||
padding: 0 .3rem 0 .6rem; | ||
gap: .25rem; | ||
`; | ||
|
||
const Balance = styled.span` | ||
padding: 0 .5rem; | ||
`; | ||
|
||
const ExpandIcon = styled(ChevronDownIcon)` | ||
font-size: 1.5rem !important; | ||
`; | ||
|
||
const avatarRadius: Record<Radius, string> = { | ||
default: "100%", | ||
minimal: "5px", | ||
none: "0px" | ||
}; | ||
|
||
const Avatar = styled.img.attrs({ | ||
draggable: false | ||
})` | ||
user-select: none; | ||
border-radius: ${props => avatarRadius[props.theme.themeConfig.radius]}; | ||
object-fit: cover; | ||
width: 1.7rem; | ||
height: 1.7rem; | ||
margin-right: .4rem; | ||
`; | ||
|
||
interface Props { | ||
accent?: string; | ||
} |
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
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,33 @@ | ||
import { useEffect, useState } from "react"; | ||
import Arweave from "arweave/node/common"; | ||
import useGlobalState from "./global"; | ||
|
||
/** | ||
* Balance hook | ||
*/ | ||
export default function useBalance() { | ||
const [balance, setBalance] = useState(0); | ||
const { state } = useGlobalState(); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (!state.activeAddress) return; | ||
|
||
const arweave = new Arweave( | ||
state?.config?.gatewayConfig || { | ||
host: "arweave.net", | ||
port: 443, | ||
protocol: "https" | ||
} | ||
); | ||
|
||
const bal = arweave.ar.winstonToAr( | ||
await arweave.wallets.getBalance(state.activeAddress) | ||
); | ||
|
||
setBalance(Number(bal)); | ||
})(); | ||
}, [state?.activeAddress]); | ||
|
||
return balance; | ||
} |
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
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,16 @@ | ||
import ConnectButton from "../components/ConnectButton"; | ||
import { ArConnectKit } from "../components/Provider"; | ||
import { rgbToString } from "../utils"; | ||
|
||
export default { | ||
name: "ConnectButton", | ||
component: ConnectButton | ||
}; | ||
|
||
export const Basic = () => ( | ||
<ArConnectKit> | ||
<ConnectButton | ||
accent={"rgb(0, 122, 255)"} | ||
/> | ||
</ArConnectKit> | ||
); |
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