-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop using loader functions for swap page #1316
Changes from all commits
d5c9f94
8e7eda3
447cbb8
e371b9d
4a3bec9
facf73a
e18dcae
8c45d66
8bc397d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ import BalanceSelector from '../../shared/balance-selector'; | |
import { Amount } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/num/v1/num_pb'; | ||
import { useStatus } from '../../../state/status'; | ||
import { hasStakingToken } from '../../../fetchers/staking-token'; | ||
import { useStakingTokenMetadata } from '../../../state/shared'; | ||
import { useBalancesResponses, useSwappableAssets } from '../../../state/swap'; | ||
import { FadeIn } from '@repo/ui/components/ui/fade-in'; | ||
|
||
const isValidAmount = (amount: string, assetIn?: BalancesResponse) => | ||
Number(amount) >= 0 && (!assetIn || !amountMoreThanBalance(assetIn, amount)); | ||
|
@@ -39,7 +42,11 @@ const getKnownZeroValueView = (metadata?: Metadata) => { | |
}); | ||
}; | ||
|
||
const assetOutBalanceSelector = ({ swap: { balancesResponses, assetIn, assetOut } }: AllSlices) => { | ||
const getAssetOutBalance = ( | ||
balancesResponses: BalancesResponse[] = [], | ||
assetIn?: BalancesResponse, | ||
assetOut?: Metadata, | ||
) => { | ||
if (!assetIn || !assetOut) return getKnownZeroValueView(); | ||
|
||
const match = balancesResponses.find(balance => { | ||
|
@@ -54,17 +61,13 @@ const assetOutBalanceSelector = ({ swap: { balancesResponses, assetIn, assetOut | |
}; | ||
|
||
const tokenSwapInputSelector = (state: AllSlices) => ({ | ||
swappableAssets: state.swap.swappableAssets, | ||
assetIn: state.swap.assetIn, | ||
setAssetIn: state.swap.setAssetIn, | ||
assetOut: state.swap.assetOut, | ||
setAssetOut: state.swap.setAssetOut, | ||
amount: state.swap.amount, | ||
setAmount: state.swap.setAmount, | ||
balancesResponses: state.swap.balancesResponses, | ||
priceHistory: state.swap.priceHistory, | ||
assetOutBalance: assetOutBalanceSelector(state), | ||
hasStakingTokenMeta: state.swap.stakingAssetMetadata, | ||
}); | ||
|
||
/** | ||
|
@@ -76,24 +79,16 @@ const tokenSwapInputSelector = (state: AllSlices) => ({ | |
export const TokenSwapInput = () => { | ||
const status = useStatus(); | ||
const latestKnownBlockHeight = status.data?.latestKnownBlockHeight ?? 0n; | ||
const { | ||
swappableAssets, | ||
amount, | ||
setAmount, | ||
assetIn, | ||
setAssetIn, | ||
assetOut, | ||
setAssetOut, | ||
balancesResponses, | ||
priceHistory, | ||
assetOutBalance, | ||
hasStakingTokenMeta, | ||
} = useStoreShallow(tokenSwapInputSelector); | ||
const stakingTokenMetadata = useStakingTokenMetadata(); | ||
const balancesResponses = useBalancesResponses(); | ||
const swappableAssets = useSwappableAssets(); | ||
const { amount, setAmount, assetIn, setAssetIn, assetOut, setAssetOut, priceHistory } = | ||
useStoreShallow(tokenSwapInputSelector); | ||
// State to manage privacy warning display | ||
const [showNonNativeFeeWarning, setshowNonNativeFeeWarning] = useState(false); | ||
const [showNonNativeFeeWarning, setShowNonNativeFeeWarning] = useState(false); | ||
const assetOutBalance = getAssetOutBalance(balancesResponses.data, assetIn, assetOut); | ||
|
||
// Check if the user has native staking tokens | ||
const stakingToken = hasStakingToken(balancesResponses, hasStakingTokenMeta); | ||
const userHasStakingToken = hasStakingToken(balancesResponses.data, stakingTokenMetadata.data); | ||
|
||
useEffect(() => { | ||
if (!assetIn || !assetOut) return; | ||
|
@@ -131,10 +126,10 @@ export const TokenSwapInput = () => { | |
onChange={e => { | ||
if (!isValidAmount(e.target.value, assetIn)) return; | ||
setAmount(e.target.value); | ||
setshowNonNativeFeeWarning(Number(e.target.value) > 0 && !stakingToken); | ||
setShowNonNativeFeeWarning(Number(e.target.value) > 0 && !userHasStakingToken); | ||
}} | ||
/> | ||
<div className='flex gap-4 sm:contents'> | ||
<div className='flex gap-4'> | ||
{assetIn && ( | ||
<div className='ml-auto hidden h-full flex-col justify-end self-end sm:flex'> | ||
<span className='mr-2 block whitespace-nowrap text-xs text-muted-foreground'> | ||
|
@@ -143,21 +138,48 @@ export const TokenSwapInput = () => { | |
</div> | ||
)} | ||
|
||
<div className='flex h-full flex-col gap-2'> | ||
<BalanceSelector value={assetIn} onChange={setAssetIn} balances={balancesResponses} /> | ||
{assetIn?.balanceView && ( | ||
<BalanceValueView valueView={assetIn.balanceView} onClick={setInputToBalanceMax} /> | ||
)} | ||
</div> | ||
|
||
<div className='flex flex-col gap-2 pt-2'> | ||
<ArrowRight size={16} className='text-muted-foreground' /> | ||
</div> | ||
|
||
<div className='flex h-full flex-col gap-2'> | ||
<AssetSelector assets={swappableAssets} value={assetOut} onChange={setAssetOut} /> | ||
{assetOut && <BalanceValueView valueView={assetOutBalance} />} | ||
</div> | ||
<FadeIn condition={!!balancesResponses.error || !!swappableAssets.error}> | ||
<div className='flex gap-4 text-red'> | ||
{balancesResponses.error instanceof Error && balancesResponses.error.toString()} | ||
{swappableAssets.error instanceof Error && swappableAssets.error.toString()} | ||
</div> | ||
</FadeIn> | ||
|
||
<FadeIn condition={!!balancesResponses.data && !!swappableAssets.data}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we handle the error case as well? Maybe some red text with a loading error message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
<div className='flex gap-4'> | ||
<div className='flex h-full flex-col gap-2'> | ||
{balancesResponses.data && ( | ||
<BalanceSelector | ||
value={assetIn} | ||
onChange={setAssetIn} | ||
balances={balancesResponses.data} | ||
/> | ||
)} | ||
{assetIn?.balanceView && ( | ||
<BalanceValueView | ||
valueView={assetIn.balanceView} | ||
onClick={setInputToBalanceMax} | ||
/> | ||
)} | ||
</div> | ||
|
||
<div className='size-4 pt-2'> | ||
<ArrowRight size={16} className='text-muted-foreground' /> | ||
</div> | ||
|
||
<div className='flex h-full flex-col gap-2'> | ||
{swappableAssets.data && ( | ||
<AssetSelector | ||
assets={swappableAssets.data} | ||
value={assetOut} | ||
onChange={setAssetOut} | ||
/> | ||
)} | ||
|
||
{assetOut && <BalanceValueView valueView={assetOutBalance} />} | ||
</div> | ||
</div> | ||
</FadeIn> | ||
</div> | ||
{priceHistory.startMetadata && priceHistory.endMetadata && priceHistory.candles.length ? ( | ||
<CandlestickPlot | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo fix