-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widget-v2)/balance loading state (#279)
- Loading branch information
Showing
5 changed files
with
160 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { ComponentProps } from "react"; | ||
|
||
export const SpinnerIcon = (props: ComponentProps<"svg">) => { | ||
return ( | ||
<svg | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
xmlns="http://www.w3.org/2000/svg" | ||
{...props} | ||
> | ||
<circle | ||
cx="12" | ||
cy="12" | ||
opacity=".25" | ||
r="10" | ||
stroke="currentColor" | ||
strokeWidth="4" | ||
/> | ||
<path | ||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" | ||
fill="currentColor" | ||
opacity=".75" | ||
/> | ||
</svg> | ||
); | ||
}; |
116 changes: 116 additions & 0 deletions
116
packages/widget-v2/src/pages/SwapPage/ConnectedWalletContent.tsx
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,116 @@ | ||
import { Row } from "@/components/Layout"; | ||
import { useAccount } from "@/hooks/useAccount"; | ||
import { ConnectedWalletModal } from "@/modals/ConnectedWalletModal/ConnectedWalletModal"; | ||
import { useMemo } from "react"; | ||
import { useSetMaxAmount } from "./useSetMaxAmount"; | ||
import { useSourceBalance } from "./useSourceBalance"; | ||
import { useModal } from "@/components/Modal"; | ||
import { useAtomValue } from "jotai"; | ||
import { sourceAssetAtom } from "@/state/swapPage"; | ||
import { GhostButton, GhostButtonProps } from "@/components/Button"; | ||
import { useGetAssetDetails } from "@/hooks/useGetAssetDetails"; | ||
import styled, { css } from "styled-components"; | ||
import { SpinnerIcon } from "@/icons/SpinnerIcon"; | ||
|
||
export const ConnectedWalletContent = () => { | ||
const sourceAsset = useAtomValue(sourceAssetAtom); | ||
const sourceAccount = useAccount(sourceAsset?.chainID); | ||
const sourceDetails = useGetAssetDetails({ | ||
assetDenom: sourceAsset?.denom, | ||
amount: sourceAsset?.amount, | ||
chainId: sourceAsset?.chainID, | ||
}); | ||
|
||
const { data: sourceBalance, isLoading } = useSourceBalance(); | ||
const handleMaxButton = useSetMaxAmount(); | ||
const connectedWalletModal = useModal(ConnectedWalletModal); | ||
|
||
const formattedBalance = useMemo(() => { | ||
if (sourceBalance === undefined || sourceBalance.error?.message) return ""; | ||
|
||
const amount = sourceBalance?.amount; | ||
let formattedBalanceAmount = sourceBalance?.formattedAmount; | ||
|
||
if (amount === "0") { | ||
formattedBalanceAmount = amount; | ||
} | ||
|
||
return `${formattedBalanceAmount} ${sourceDetails?.symbol}`; | ||
}, [sourceBalance, sourceDetails?.symbol]); | ||
if (!sourceAccount) return null; | ||
return ( | ||
<Row | ||
gap={6} | ||
style={{ | ||
paddingRight: 13, | ||
}} | ||
> | ||
<TransparentButton | ||
onClick={() => { | ||
connectedWalletModal.show(); | ||
}} | ||
style={{ | ||
padding: "8px 13px", | ||
alignItems: "center", | ||
gap: 8, | ||
}} | ||
> | ||
{sourceAccount && ( | ||
<img | ||
style={{ objectFit: "cover" }} | ||
src={sourceAccount?.wallet.logo} | ||
height={16} | ||
width={16} | ||
/> | ||
)} | ||
{isLoading ? ( | ||
<div | ||
style={{ | ||
marginLeft: "8px", | ||
marginRight: "8px", | ||
position: "relative", | ||
}} | ||
> | ||
<SpinnerIcon | ||
style={{ | ||
animation: "spin 1s linear infinite", | ||
position: "absolute", | ||
height: 14, | ||
width: 14, | ||
}} | ||
/> | ||
</div> | ||
) : ( | ||
formattedBalance | ||
)} | ||
</TransparentButton> | ||
|
||
<TransparentButton | ||
disabled={!sourceBalance || sourceBalance?.amount === "0"} | ||
onClick={handleMaxButton} | ||
style={{ | ||
padding: "8px 13px", | ||
alignItems: "center", | ||
}} | ||
> | ||
Max | ||
</TransparentButton> | ||
</Row> | ||
); | ||
}; | ||
|
||
const TransparentButton = styled(GhostButton).attrs({ | ||
as: "button", | ||
}) <GhostButtonProps>` | ||
${({ theme, onClick, secondary, disabled }) => | ||
onClick && | ||
!disabled && | ||
css` | ||
background-color: ${secondary | ||
? theme.secondary.background.normal | ||
: theme.primary.ghostButtonHover}; | ||
cursor: pointer; | ||
`}; | ||
padding: 10px 13px; | ||
border-radius: 90px; | ||
`; |
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