Skip to content

Commit

Permalink
disable collateral when only 1 asset
Browse files Browse the repository at this point in the history
nicer formatting of message

fix label
  • Loading branch information
vidvidvid committed Dec 20, 2024
1 parent 205e5b5 commit af8ac21
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
39 changes: 34 additions & 5 deletions packages/ui/app/_components/dialogs/manage/SupplyTab.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useEffect, useMemo } from 'react';

import { useSearchParams } from 'next/navigation';

import { formatUnits } from 'viem';
import { mode } from 'viem/chains';

import { Button } from '@ui/components/ui/button';
import { Switch } from '@ui/components/ui/switch';
Expand All @@ -14,6 +17,7 @@ import {
useManageDialogContext
} from '@ui/context/ManageDialogContext';
import { useCollateralToggle } from '@ui/hooks/market/useCollateralToggle';
import { useMarketData } from '@ui/hooks/market/useMarketData';
import { useSupply } from '@ui/hooks/market/useSupply';

import Amount from './Amount';
Expand Down Expand Up @@ -57,6 +61,29 @@ const SupplyTab = ({
onSuccess: refetchUsedQueries
});

const searchParams = useSearchParams();
const querychain = searchParams.get('chain');
const querypool = searchParams.get('pool');
const selectedPool = querypool ?? '0';
const chain = querychain ? querychain : mode.id.toString();

const { marketData } = useMarketData(selectedPool, chain);

const getTooltipContent = () => {
if (hasActiveTransactions) {
return 'Cannot modify collateral during an active transaction';
}
if (!selectedMarketData.supplyBalance) {
return 'You need to supply assets first before enabling as collateral';
}
if (disableCollateral) {
return 'Unavailable until borrowing is enabled';
}
return null;
};

const disableCollateral = marketData.length === 1;

const {
isWaitingForIndexing,
supplyAmount,
Expand Down Expand Up @@ -85,6 +112,10 @@ const SupplyTab = ({

const isDisabled = !amount || amountAsBInt === 0n;
const hasActiveTransactions = combinedTransactionSteps.length > 0;
const showTooltip =
hasActiveTransactions ||
!selectedMarketData.supplyBalance ||
disableCollateral;

return (
<div className="space-y-4 pt-2">
Expand Down Expand Up @@ -149,18 +180,16 @@ const SupplyTab = ({
checked={enableCollateral}
onCheckedChange={handleCollateralToggle}
disabled={
disableCollateral ||
hasActiveTransactions ||
!selectedMarketData.supplyBalance
}
/>
</div>
</TooltipTrigger>
{(hasActiveTransactions ||
!selectedMarketData.supplyBalance) && (
{showTooltip && (
<TooltipContent side="top">
{hasActiveTransactions
? 'Cannot modify collateral during an active transaction'
: 'You need to supply assets first before enabling as collateral'}
{getTooltipContent()}
</TooltipContent>
)}
</Tooltip>
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/hooks/market/useMarketData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type MarketRowData = MarketData & {
export const useMarketData = (
selectedPool: string,
chain: number | string,
selectedSymbol: string | undefined
selectedSymbol?: string | undefined
) => {
const { data: poolData, isLoading: isLoadingPoolData } = useFusePoolData(
selectedPool,
Expand Down

0 comments on commit af8ac21

Please sign in to comment.