Skip to content

Commit

Permalink
fix: nft card (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
leduyhien152 authored May 9, 2024
1 parent 1131d35 commit 519d1aa
Show file tree
Hide file tree
Showing 20 changed files with 759 additions and 128 deletions.
19 changes: 8 additions & 11 deletions app/[server]/staking/page.tsx → app/[server]/earn/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { Footer } from "@/components/footer";
import { Button, IconButton, Typography } from "@mochi-ui/core";
import { EyeHiddenSolid, EyeShowSolid } from "@mochi-ui/icons";
import { useLoginWidget } from "@mochi-web3/login-widget";
import { Suspense, useEffect, useState } from "react";
import { Suspense, useEffect, useRef, useState } from "react";
import { FlexibleStakingCard } from "@/components/staking/FlexibleStakingCard";
import { FixedStakingCard } from "@/components/staking/FixedStakingCard";
import { NFTCard } from "@/components/staking/NFTCard";
import { useFlexibleStaking } from "@/store/flexible-staking";
import { utils } from "@consolelabs/mochi-formatter";
import { useTokenStaking } from "@/store/token-staking";
import { formatUnits, parseUnits } from "ethers/lib/utils";
import clsx from "clsx";
import { TokenImage } from "@/components/token-image";
import { Header } from "@/components/header/header";

Expand All @@ -28,8 +27,10 @@ const Overview = () => {
} = useFlexibleStaking();
const [showInfo, setShowInfo] = useState(false);

const initShowInfo = useRef(false);
useEffect(() => {
if (!!balance && !showInfo) {
if (!!balance && !balance.isZero() && !showInfo && !initShowInfo.current) {
initShowInfo.current = true;
setShowInfo(true);
}
}, [balance, showInfo]);
Expand Down Expand Up @@ -81,10 +82,10 @@ const Overview = () => {
</IconButton>
</div>
<div className="flex items-center space-x-4">
<Button variant="link" className="pr-0 pl-0 h-fit">
<Button variant="link" className="pr-0 pl-0 h-fit" disabled>
Claim all
</Button>
<Button variant="link" className="pr-0 pl-0 h-fit">
<Button variant="link" className="pr-0 pl-0 h-fit" disabled>
Restake
</Button>
</div>
Expand Down Expand Up @@ -154,12 +155,7 @@ const Overview = () => {
</div>
)}
</div>
<div
className={clsx("grid grid-cols-1 mx-auto gap-4", {
"md:grid-cols-2": stakingPools.length >= 2,
"lg:grid-cols-3": stakingPools.length >= 3,
})}
>
<div className="flex flex-wrap justify-center gap-6">
{stakingPools.some((each) => each.type === "flexible") && (
<FlexibleStakingCard hidden={isLoggedIn && !showInfo} />
)}
Expand All @@ -169,6 +165,7 @@ const Overview = () => {
{stakingPools.some((each) => each.type === "nft") && (
<NFTCard hidden={isLoggedIn && !showInfo} />
)}
<NFTCard hidden={isLoggedIn && !showInfo} />
</div>
</div>
<Footer />
Expand Down
7 changes: 5 additions & 2 deletions app/[server]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { Highlights } from "@/components/overview/highlights";
import { About } from "@/components/overview/about";
import { BountyTable } from "@/components/overview/bounty-table";
import Link from "next/link";
import { ROUTES } from "@/constants/routes";
import { useParams } from "next/navigation";

const Overview = () => {
const { getInfo } = useServerInfo();
const params = useParams();
const { abort, getInfo } = useServerInfo();

useEffect(() => {
getInfo();
Expand Down Expand Up @@ -81,7 +84,7 @@ const Overview = () => {
<Button variant="link" color="neutral">
Share
</Button>
<Link href="/staking">
<Link href={ROUTES.EARN(params.server as string)}>
<Button variant="outline" color="neutral">
<VaultSolid />
Stake
Expand Down
6 changes: 5 additions & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { Platform } from "@consolelabs/mochi-formatter";
import { AUTH_TELEGRAM_ID, MOCHI_PROFILE_API } from "@/envs";
import { Toaster } from "@mochi-ui/core";
import { useTokenStaking } from "@/store/token-staking";
import { useEffect } from "react";
import { Suspense, useEffect } from "react";
import { NFTModal } from "@/components/stake/nft/nft-modal";

const inter = Inter({ subsets: ["latin"] });

Expand Down Expand Up @@ -36,6 +37,9 @@ export default function RootLayout({
profileApi={MOCHI_PROFILE_API}
>
{children as any}
<Suspense fallback={null}>
<NFTModal />
</Suspense>
</LoginWidgetProvider>
<div className="fixed top-16 right-6 z-50 max-w-[500px] pointer-events-none mx-auto">
<Toaster />
Expand Down
8 changes: 5 additions & 3 deletions components/profile-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import { LoginWidget, useLoginWidget } from "@mochi-web3/login-widget";
import { ReactNode, useEffect, useRef } from "react";
import { utils } from "@consolelabs/mochi-formatter";
import { useParams, useRouter } from "next/navigation";
import { ROUTES } from "@/constants/routes";
import { PARAMS, ROUTES } from "@/constants/routes";
import { useFlexibleStaking } from "@/store/flexible-staking";
import Image from "next/image";
import {
ChevronLeftLine,
Discord,
LifeBuoySolid,
WalletAddSolid,
WalletSolid,
X,
} from "@mochi-ui/icons";
import { useWalletBalance } from "@/store/wallet-balance";
Expand All @@ -35,6 +34,7 @@ import { formatUnits } from "ethers/lib/utils";
import { getUsdAmount } from "@/utils/number";
import { useDisclosure } from "@dwarvesf/react-hooks";
import Link from "next/link";
import { useNFTStaking } from "@/store/nft-staking";

const DropdownContent = () => {
const { profile, logout } = useLoginWidget();
Expand All @@ -47,6 +47,7 @@ const DropdownContent = () => {
balance: flexibleStakingBalance,
stakingToken: flexibleStakingToken,
} = useFlexibleStaking();
const { reset: resetNFTStaking } = useNFTStaking();
const {
isOpen: isOpenLoginWidget,
onOpen: onOpenLoginWidget,
Expand Down Expand Up @@ -143,7 +144,7 @@ const DropdownContent = () => {
</AccordionContent>
</AccordionItem>
</Accordion>
<Link href={ROUTES.OVERVIEW_NFT(params.server)}>
<Link href={`?modal=${PARAMS.OVERVIEW.NFT}`}>
<DropdownMenuItem
leftIcon={
<Image src="/svg/image.svg" alt="" width={20} height={20} />
Expand All @@ -166,6 +167,7 @@ const DropdownContent = () => {
<DropdownMenuItem
onClick={() => {
resetFlexibleStaking();
resetNFTStaking();
logout();
push(ROUTES.HOME);
}}
Expand Down
2 changes: 1 addition & 1 deletion components/stake/flexible/flexible-stake-response.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const FlexibleStakeResponse = (props: Props) => {
</Typography>
<div className="flex items-center justify-between">
<Typography level="p4" className="text-text-tertiary">
Youre staking
You&apos;re staking
</Typography>
<div className="flex items-center space-x-1">
<TokenImage symbol={stakingToken?.token_symbol} size={24} />
Expand Down
154 changes: 154 additions & 0 deletions components/stake/flexible/flexible-unstake-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import {
Button,
Drawer,
DrawerContent,
DrawerOverlay,
DrawerPortal,
DrawerTrigger,
Typography,
} from "@mochi-ui/core";
import { useMemo, useState } from "react";
import { TokenAmount, formatTokenAmount } from "@/utils/number";
import { StakeInput } from "../stake-input";
import { useFlexibleStaking } from "@/store/flexible-staking";
import { LoginWidget } from "@mochi-web3/login-widget";
import { formatUnits, parseUnits } from "ethers/lib/utils";
import { useWalletNetwork } from "@/hooks/useWalletNetwork";
import { formatDate } from "@/utils/datetime";
import { utils } from "@consolelabs/mochi-formatter";

interface Props {
container: HTMLDivElement | null;
onConfirm: () => void;
}

export const FlexibleUnstakeContent = (props: Props) => {
const { container, onConfirm } = props;
const {
stakedAmount,
stakingToken,
unclaimedRewards,
rewardToken,
apr,
setValues,
} = useFlexibleStaking();
const chain = useMemo(() => stakingToken?.token_chain_id, [stakingToken]);
const [amount, setAmount] = useState<TokenAmount>({
value: 0,
display: "",
});
const { isConnected, isCorrectNetwork, changeNetwork, checkNetwork } =
useWalletNetwork({
chain,
});

const maxAmount = Number(
formatUnits(stakedAmount, stakingToken?.token_decimal)
);

return (
<div className="flex flex-col">
<div className="p-3 space-y-3 rounded-2xl border border-divider">
<StakeInput
{...{ amount, setAmount }}
balance={maxAmount}
convertedValue={amount.value * (stakingToken?.token_price || 1)}
token={stakingToken}
type="unstake"
/>
<div className="rounded-lg border border-divider p-4 space-y-2">
<Typography level="h9" color="primary">
Unstake now
</Typography>
<div className="flex items-center justify-between">
<Typography level="p5">Today</Typography>
<Typography level="p5">{formatDate(new Date())}</Typography>
</div>
<div className="flex items-center justify-between">
<Typography level="p5">APY</Typography>
<Typography level="p5" color="success">
{utils.formatPercentDigit(
formatUnits(apr, stakingToken?.token_decimal)
)}
/year
</Typography>
</div>
<div className="flex items-center justify-between">
<Typography level="p5">Current reward</Typography>
<Typography level="p5">
{
formatTokenAmount(
formatUnits(unclaimedRewards, rewardToken?.token_decimal)
).display
}{" "}
{rewardToken?.token_symbol}
</Typography>
</div>
<div className="flex items-center justify-between">
<Typography level="p5">Total received</Typography>
<Typography level="p5">
{
formatTokenAmount(
formatUnits(
unclaimedRewards.add(
parseUnits(amount.value ? amount.value.toString() : "0")
),
stakingToken?.token_decimal
)
).display
}{" "}
{stakingToken?.token_symbol}
</Typography>
</div>
</div>
</div>
{isConnected && isCorrectNetwork ? (
<Button
size="lg"
disabled={amount.value <= 0 || amount.value > maxAmount}
className="mt-3"
onClick={() => {
setValues({
latestUnstaking: {
txHash: "",
date: new Date(),
amount: amount.value,
rewards: 0,
},
});
onConfirm();
}}
>
{amount.value > maxAmount ? "Insufficient amount" : "Unstake"}
</Button>
) : isConnected && !isCorrectNetwork ? (
<Button size="lg" className="mt-3" onClick={changeNetwork}>
Switch to Base
</Button>
) : (
<Drawer anchor="bottom">
<DrawerTrigger asChild>
<Button size="lg" className="mt-3">
Connect wallet
</Button>
</DrawerTrigger>
<DrawerPortal container={container}>
<DrawerOverlay />
<DrawerContent className="!w-full rounded-xl h-[calc(100%-60px)]">
<div className="flex justify-center items-center h-full">
<LoginWidget
raw
onchain
chain="evm"
onWalletConnectSuccess={async () => {
checkNetwork();
}}
/>
</div>
</DrawerContent>
</DrawerPortal>
</Drawer>
)}
</div>
);
};
Loading

0 comments on commit 519d1aa

Please sign in to comment.