|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Dialog, |
| 4 | + MobileDialog, |
| 5 | + DialogBody, |
| 6 | + DialogFooter, |
| 7 | + DialogHeader, |
| 8 | + Text, |
| 9 | + useIsMobile, |
| 10 | + AmountItem, |
| 11 | + SubSection, |
| 12 | +} from "@babylonlabs-io/core-ui"; |
| 13 | +import { useMemo, type ReactNode } from "react"; |
| 14 | +import { twMerge } from "tailwind-merge"; |
| 15 | +import { useBorrowService } from "../../hooks/useBorrowService"; |
| 16 | +import { usdcIcon } from "../../assets"; |
| 17 | + |
| 18 | +type DialogComponentProps = Parameters<typeof Dialog>[0]; |
| 19 | + |
| 20 | +interface ResponsiveDialogProps extends DialogComponentProps { |
| 21 | + children?: ReactNode; |
| 22 | +} |
| 23 | + |
| 24 | +function ResponsiveDialog({ className, ...restProps }: ResponsiveDialogProps) { |
| 25 | + const isMobileView = useIsMobile(640); |
| 26 | + const DialogComponent = isMobileView ? MobileDialog : Dialog; |
| 27 | + |
| 28 | + return ( |
| 29 | + <DialogComponent {...restProps} className={twMerge("w-[41.25rem] max-w-full", className)} /> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +interface BorrowModalProps { |
| 34 | + open: boolean; |
| 35 | + onClose: () => void; |
| 36 | + collateral: { |
| 37 | + amount: string; |
| 38 | + symbol: string; |
| 39 | + icon?: string | ReactNode; |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export function BorrowModal({ open, onClose, collateral }: BorrowModalProps) { |
| 44 | + const collateralBTC = useMemo( |
| 45 | + () => parseFloat(collateral.amount || "0"), |
| 46 | + [collateral.amount] |
| 47 | + ); |
| 48 | + |
| 49 | + const collateralIconUrl = useMemo(() => { |
| 50 | + if (typeof collateral.icon === "string") { |
| 51 | + return collateral.icon; |
| 52 | + } |
| 53 | + return ""; |
| 54 | + }, [collateral.icon]); |
| 55 | + |
| 56 | + const { |
| 57 | + borrowAmount, |
| 58 | + borrowAmountNum, |
| 59 | + processing, |
| 60 | + inputState, |
| 61 | + maxBorrow, |
| 62 | + collateralValueUSD, |
| 63 | + currentLTV, |
| 64 | + validation, |
| 65 | + hintText, |
| 66 | + btcPriceUSD, |
| 67 | + usdcPriceUSD, |
| 68 | + maxLTV, |
| 69 | + liquidationLTV, |
| 70 | + handleInputChange, |
| 71 | + handleBorrow, |
| 72 | + setTouched, |
| 73 | + formatUSD, |
| 74 | + formatPercentage, |
| 75 | + } = useBorrowService(collateralBTC); |
| 76 | + |
| 77 | + // Handle key down to prevent arrow keys |
| 78 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 79 | + if (e.key === "ArrowUp" || e.key === "ArrowDown") { |
| 80 | + e.preventDefault(); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + // Handle input change for collateral (read-only) |
| 85 | + const handleCollateralChange = () => { |
| 86 | + // No-op, collateral is read-only |
| 87 | + }; |
| 88 | + |
| 89 | + // Handle borrow button click |
| 90 | + const handleBorrowClick = async () => { |
| 91 | + setTouched(true); |
| 92 | + if (validation.isValid && borrowAmountNum > 0) { |
| 93 | + await handleBorrow(borrowAmountNum, collateralBTC); |
| 94 | + onClose(); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + return ( |
| 99 | + <ResponsiveDialog open={open} onClose={onClose}> |
| 100 | + <DialogHeader title="Collateral" onClose={onClose} className="text-accent-primary" /> |
| 101 | + <DialogBody className="no-scrollbar mb-8 mt-4 flex max-h-[calc(100vh-12rem)] flex-col gap-6 overflow-y-auto text-accent-primary px-4 sm:px-6"> |
| 102 | + {/* Subtitle */} |
| 103 | + <Text variant="body2" className="text-accent-secondary -mt-2 text-sm sm:text-base"> |
| 104 | + Your locked BTC collateral |
| 105 | + </Text> |
| 106 | + |
| 107 | + {/* Collateral Display */} |
| 108 | + <SubSection className="flex w-full flex-col content-center justify-between gap-4"> |
| 109 | + <AmountItem |
| 110 | + amount={collateral.amount} |
| 111 | + currencyIcon={collateralIconUrl} |
| 112 | + currencyName="BTC" |
| 113 | + placeholder="" |
| 114 | + displayBalance={true} |
| 115 | + balanceDetails={{ |
| 116 | + balance: parseFloat(collateral.amount).toFixed(2), |
| 117 | + symbol: "BTC", |
| 118 | + price: btcPriceUSD, |
| 119 | + displayUSD: true, |
| 120 | + }} |
| 121 | + min="0" |
| 122 | + step="any" |
| 123 | + autoFocus={false} |
| 124 | + onChange={handleCollateralChange} |
| 125 | + onKeyDown={handleKeyDown} |
| 126 | + amountUsd={formatUSD(collateralValueUSD)} |
| 127 | + subtitle={`Balance: ${parseFloat(collateral.amount).toFixed(2)} BTC`} |
| 128 | + disabled={true} |
| 129 | + /> |
| 130 | + </SubSection> |
| 131 | + |
| 132 | + {/* Borrow Section */} |
| 133 | + <div className="flex flex-col gap-2"> |
| 134 | + <h3 className="text-base sm:text-lg font-semibold text-accent-primary"> |
| 135 | + Borrow |
| 136 | + </h3> |
| 137 | + <Text variant="body2" className="text-accent-secondary text-sm sm:text-base"> |
| 138 | + Enter the amount you want to borrow |
| 139 | + </Text> |
| 140 | + </div> |
| 141 | + |
| 142 | + {/* Borrow Amount Input */} |
| 143 | + <SubSection className="flex w-full flex-col content-center justify-between gap-4"> |
| 144 | + <AmountItem |
| 145 | + amount={borrowAmount} |
| 146 | + currencyIcon={usdcIcon} |
| 147 | + currencyName="USDC" |
| 148 | + placeholder="0" |
| 149 | + displayBalance={true} |
| 150 | + balanceDetails={{ |
| 151 | + balance: maxBorrow.toFixed(0), |
| 152 | + symbol: "USDC", |
| 153 | + price: usdcPriceUSD, |
| 154 | + displayUSD: true, |
| 155 | + }} |
| 156 | + min="0" |
| 157 | + step="any" |
| 158 | + autoFocus={true} |
| 159 | + onChange={handleInputChange} |
| 160 | + onKeyDown={handleKeyDown} |
| 161 | + amountUsd={formatUSD(borrowAmountNum)} |
| 162 | + subtitle={`Max: ${maxBorrow.toFixed(0)} USDC`} |
| 163 | + /> |
| 164 | + {hintText && ( |
| 165 | + <Text |
| 166 | + variant="body2" |
| 167 | + className={twMerge( |
| 168 | + "text-xs sm:text-sm -mt-2", |
| 169 | + inputState === "error" && "text-error-main", |
| 170 | + inputState === "warning" && "text-warning-main" |
| 171 | + )} |
| 172 | + > |
| 173 | + {hintText} |
| 174 | + </Text> |
| 175 | + )} |
| 176 | + </SubSection> |
| 177 | + |
| 178 | + {/* Metrics Card */} |
| 179 | + <div className="flex flex-col gap-3 p-3 sm:p-4 bg-secondary-highlight rounded"> |
| 180 | + <div className="flex items-center justify-between gap-2"> |
| 181 | + <Text variant="body2" className="text-accent-secondary text-xs sm:text-sm shrink-0"> |
| 182 | + Collateral |
| 183 | + </Text> |
| 184 | + <Text variant="body1" className="font-medium text-xs sm:text-sm text-right truncate"> |
| 185 | + {collateral.amount} BTC ({formatUSD(collateralValueUSD)}) |
| 186 | + </Text> |
| 187 | + </div> |
| 188 | + <div className="flex items-center justify-between gap-2"> |
| 189 | + <Text variant="body2" className="text-accent-secondary text-xs sm:text-sm shrink-0"> |
| 190 | + Loan |
| 191 | + </Text> |
| 192 | + <Text variant="body1" className="font-medium text-xs sm:text-sm text-right truncate"> |
| 193 | + {borrowAmount || "0"} USDC ({formatUSD(borrowAmountNum)}) |
| 194 | + </Text> |
| 195 | + </div> |
| 196 | + <div className="flex items-center justify-between gap-2"> |
| 197 | + <Text variant="body2" className="text-accent-secondary text-xs sm:text-sm shrink-0"> |
| 198 | + LTV |
| 199 | + </Text> |
| 200 | + <Text |
| 201 | + variant="body1" |
| 202 | + className={twMerge( |
| 203 | + "font-medium text-xs sm:text-sm", |
| 204 | + currentLTV > 50 && "text-warning-main", |
| 205 | + currentLTV > maxLTV * 100 && "text-error-main" |
| 206 | + )} |
| 207 | + > |
| 208 | + {formatPercentage(currentLTV)} |
| 209 | + </Text> |
| 210 | + </div> |
| 211 | + <div className="flex items-center justify-between gap-2"> |
| 212 | + <Text variant="body2" className="text-accent-secondary text-xs sm:text-sm shrink-0"> |
| 213 | + Liquidation LTV |
| 214 | + </Text> |
| 215 | + <Text variant="body1" className="font-medium text-xs sm:text-sm"> |
| 216 | + {formatPercentage(liquidationLTV * 100)} |
| 217 | + </Text> |
| 218 | + </div> |
| 219 | + </div> |
| 220 | + </DialogBody> |
| 221 | + <DialogFooter className="flex flex-col gap-4 pb-8 pt-0"> |
| 222 | + <Button |
| 223 | + variant="contained" |
| 224 | + color="primary" |
| 225 | + onClick={handleBorrowClick} |
| 226 | + className="w-full" |
| 227 | + disabled={!validation.isValid || borrowAmountNum === 0 || processing} |
| 228 | + > |
| 229 | + {processing ? "Processing..." : "Borrow"} |
| 230 | + </Button> |
| 231 | + </DialogFooter> |
| 232 | + </ResponsiveDialog> |
| 233 | + ); |
| 234 | +} |
0 commit comments