Skip to content

Commit

Permalink
fix: build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders authored and chambaz committed Dec 7, 2023
1 parent b848a04 commit fab8486
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
31 changes: 20 additions & 11 deletions apps/marginfi-v2-ui/src/components/common/ActionBox/ActionBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ export const ActionBox = () => {
const maxAmount = React.useMemo(() => {
switch (selectedMode) {
case ActionType.Deposit:
return selectedToken?.userInfo.maxDeposit;
return selectedToken?.userInfo.maxDeposit ?? 0;
case ActionType.Withdraw:
return selectedToken?.userInfo.maxWithdraw;
return selectedToken?.userInfo.maxWithdraw ?? 0;
case ActionType.Borrow:
return selectedToken?.userInfo.maxBorrow;
return selectedToken?.userInfo.maxBorrow ?? 0;
case ActionType.Repay:
return selectedToken?.userInfo.maxRepay;
return selectedToken?.userInfo.maxRepay ?? 0;
default:
return 0;
}
}, [selectedToken, selectedMode]);
const isInputDisabled = React.useMemo(() => maxAmount === 0 && !showCloseBalance, [maxAmount, showCloseBalance]);
Expand All @@ -65,18 +67,19 @@ export const ActionBox = () => {
const hasActivePosition = React.useMemo(
() =>
selectedToken?.isActive &&
((!selectedToken.isLending && lendingMode === LendingModes.LEND) ||
(selectedToken.isLending && lendingMode === LendingModes.BORROW)),
((!selectedToken.position.isLending && lendingMode === LendingModes.LEND) ||
(selectedToken.position.isLending && lendingMode === LendingModes.BORROW)),
[selectedToken, lendingMode]
);

React.useEffect(() => {
setAmount(0);
if (lendingMode === LendingModes.LEND) {
setSelectMode(ActionType.Deposit);
} else if (lendingMode === LendingModes.BORROW) {
setSelectMode(ActionType.Borrow);
}
}, [lendingMode, setSelectMode, selectedToken]);
}, [lendingMode, setSelectMode, setAmount, selectedToken]);

React.useEffect(() => {
if (!selectedToken || !amount) {
Expand Down Expand Up @@ -108,7 +111,7 @@ export const ActionBox = () => {
if (!selectedToken || !amountInputRef.current) return;
setAmount(0);
amountInputRef.current.focus();
}, [selectedToken]);
}, [selectedToken, setAmount]);

const executeLendingActionCb = React.useCallback(
async ({
Expand Down Expand Up @@ -146,6 +149,9 @@ export const ActionBox = () => {

const handleCloseBalance = React.useCallback(async () => {
try {
if (!selectedToken || !selectedAccount) {
throw new Error();
}
await closeBalance({ marginfiAccount: selectedAccount, bank: selectedToken });
} catch (error) {
return;
Expand All @@ -164,6 +170,9 @@ export const ActionBox = () => {

const handleLendingAction = React.useCallback(async () => {
// TODO implement LST dialog
if (!selectedMode || !selectedToken || !selectedAccount || !amount) {
return;
}

await executeLendingActionCb({
mfiClient,
Expand Down Expand Up @@ -240,7 +249,7 @@ export const ActionBox = () => {
<IconWallet size={16} />
</div>
<span className="text-sm font-normal">
{(walletAmount > 0.01 ? numeralFormatter(walletAmount) : "< 0.01").concat(
{(walletAmount && walletAmount > 0.01 ? numeralFormatter(walletAmount) : "< 0.01").concat(
" ",
selectedToken?.meta.tokenSymbol
)}
Expand All @@ -265,9 +274,9 @@ export const ActionBox = () => {
</div>
<ActionBoxActions
selectedMode={selectedMode}
amount={amount}
amount={amount ?? 0}
maxAmount={maxAmount}
showCloseBalance={showCloseBalance}
showCloseBalance={showCloseBalance ?? false}
handleAction={() => (showCloseBalance ? handleCloseBalance() : handleLendingAction())}
/>
{selectedToken !== null && amount !== null && preview.length > 0 && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useUiStore } from "~/store";
import { Button } from "~/components/ui/button";

type ActionBoxActionsProps = {
selectedMode: ActionType;
selectedMode?: ActionType;
amount: number;
maxAmount: number;
showCloseBalance: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke
state.extendedBankInfos,
state.nativeSolBalance,
]);
const [lendingMode, selectedToken, setSelectedToken] = useUiStore((state) => [
state.lendingMode,
state.selectedToken,
state.setSelectedToken,
]);
const [lendingMode] = useUiStore((state) => [state.lendingMode]);
const [searchQuery, setSearchQuery] = React.useState("");
const [isTokenPopoverOpen, setIsTokenPopoverOpen] = React.useState(false);

Expand Down Expand Up @@ -62,15 +58,15 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke
return extendedBankInfos
.filter((bankInfo) => bankInfo.meta.tokenSymbol.toLowerCase().includes(lowerCaseSearchQuery))
.filter((bankInfo) => bankInfo.isActive && bankInfo.position?.isLending)
.sort((a, b) => b.userInfo.position?.amount - a.userInfo.position?.amount);
.sort((a, b) => (b.isActive ? b?.position?.amount : 0) - (a.isActive ? a?.position?.amount : 0));
}, [extendedBankInfos, searchQuery]);

const filteredBanksActiveBorrowing = React.useMemo(() => {
const lowerCaseSearchQuery = searchQuery.toLowerCase();
return extendedBankInfos
.filter((bankInfo) => bankInfo.meta.tokenSymbol.toLowerCase().includes(lowerCaseSearchQuery))
.filter((bankInfo) => bankInfo.isActive && !bankInfo.position?.isLending)
.sort((a, b) => b.userInfo.position?.amount - a.userInfo.position?.amount);
.sort((a, b) => (b.isActive ? b?.position?.amount : 0) - (a.isActive ? a?.position?.amount : 0));
}, [extendedBankInfos, searchQuery]);

const filteredBanksUserOwns = React.useMemo(() => {
Expand Down Expand Up @@ -131,7 +127,7 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke
lendingMode === LendingModes.BORROW && "text-error"
)}
>
{calculateRate(selectedToken) + ` ${lendingMode === LendingModes.LEND ? "APY" : "APR"}`}
{calculateRate(currentToken) + ` ${lendingMode === LendingModes.LEND ? "APY" : "APR"}`}
</p>
</div>
</>
Expand All @@ -150,9 +146,9 @@ export const ActionBoxTokens = ({ currentToken, setCurrentToken }: ActionBoxToke
<Command
className="bg-background-gray relative"
shouldFilter={false}
value={selectedToken?.address?.toString().toLowerCase() ?? ""}
value={currentToken?.address?.toString().toLowerCase() ?? ""}
onValueChange={(value) =>
setSelectedToken(extendedBankInfos.find((bank) => bank.address.toString() === value) || selectedToken)
setCurrentToken(extendedBankInfos.find((bank) => bank.address.toString() === value) || currentToken)
}
>
<CommandInput
Expand Down

0 comments on commit fab8486

Please sign in to comment.