|
| 1 | +"use client"; |
| 2 | + |
| 3 | +// "Your positions" panel on /stake — reads the connected wallet's Morpheus |
| 4 | +// Capital stakes (stETH + USDC) and shows, per asset: deposit, MOR earned, when |
| 5 | +// the MOR claim-lock lifts (the power lock chosen at deposit) and when the 7-day |
| 6 | +// principal lock lifts. Read-only; the actual claim happens in the reward box. |
| 7 | + |
| 8 | +import { useState } from "react"; |
| 9 | +import { useTranslations } from "next-intl"; |
| 10 | +import { useActiveAccount } from "thirdweb/react"; |
| 11 | +import { cn } from "@/lib/utils"; |
| 12 | +import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card"; |
| 13 | +import { useMorpheusPosition, type MorpheusPoolPosition } from "@/hooks/use-morpheus-position"; |
| 14 | + |
| 15 | +const fmt = (n: number, d = 4) => n.toLocaleString(undefined, { maximumFractionDigits: d }); |
| 16 | +const fmtDate = (ts: number) => |
| 17 | + new Date(ts * 1000).toLocaleDateString(undefined, { day: "2-digit", month: "short", year: "numeric" }); |
| 18 | + |
| 19 | +type T = ReturnType<typeof useTranslations>; |
| 20 | + |
| 21 | +function Badge({ tone, children }: { tone: "ok" | "warn"; children: React.ReactNode }) { |
| 22 | + return ( |
| 23 | + <span |
| 24 | + className={cn( |
| 25 | + "inline-block rounded-full px-2 py-0.5 text-[11px] font-semibold", |
| 26 | + tone === "ok" |
| 27 | + ? "bg-emerald-500/15 text-emerald-600 dark:text-emerald-400" |
| 28 | + : "bg-amber-500/15 text-amber-600 dark:text-amber-400", |
| 29 | + )} |
| 30 | + > |
| 31 | + {children} |
| 32 | + </span> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function Metric({ label, children }: { label: string; children: React.ReactNode }) { |
| 37 | + return ( |
| 38 | + <div className="flex flex-col gap-0.5"> |
| 39 | + <span className="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground">{label}</span> |
| 40 | + <span className="text-sm tabular-nums">{children}</span> |
| 41 | + </div> |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function PositionRow({ p, now, t }: { p: MorpheusPoolPosition; now: number; t: T }) { |
| 46 | + const morUnlocked = p.morUnlockAt > 0 && p.morUnlockAt <= now; |
| 47 | + const depUnlocked = p.unlockAt > 0 && p.unlockAt <= now; |
| 48 | + return ( |
| 49 | + <div className="rounded-xl border p-3"> |
| 50 | + <div className="flex items-center justify-between gap-2"> |
| 51 | + <span className="flex items-center gap-2 font-semibold"> |
| 52 | + <span className={cn("size-2 shrink-0 rounded-full", p.asset === "usdc" ? "bg-sky-500" : "bg-emerald-500")} /> |
| 53 | + {p.symbol} |
| 54 | + </span> |
| 55 | + <span className="font-mono text-sm tabular-nums"> |
| 56 | + {fmt(p.staked)} <span className="text-xs font-normal text-muted-foreground">{p.symbol}</span> |
| 57 | + </span> |
| 58 | + </div> |
| 59 | + |
| 60 | + <div className="mt-3 grid grid-cols-2 gap-x-4 gap-y-2.5 sm:grid-cols-3"> |
| 61 | + <Metric label={t("earned")}> |
| 62 | + <span className="font-mono font-semibold">{fmt(p.pendingMor)}</span>{" "} |
| 63 | + <span className="text-xs text-muted-foreground">MOR</span> |
| 64 | + </Metric> |
| 65 | + <Metric label={t("morLock")}> |
| 66 | + {p.morUnlockAt <= 0 ? ( |
| 67 | + <span className="text-muted-foreground">—</span> |
| 68 | + ) : morUnlocked ? ( |
| 69 | + <Badge tone="ok">{t("claimable")}</Badge> |
| 70 | + ) : ( |
| 71 | + <Badge tone="warn">{t("lockedUntil", { date: fmtDate(p.morUnlockAt) })}</Badge> |
| 72 | + )} |
| 73 | + </Metric> |
| 74 | + <Metric label={t("principalLock")}> |
| 75 | + {p.unlockAt <= 0 ? ( |
| 76 | + <span className="text-muted-foreground">—</span> |
| 77 | + ) : depUnlocked ? ( |
| 78 | + <span className="text-emerald-600 dark:text-emerald-400">{t("unlocked")}</span> |
| 79 | + ) : ( |
| 80 | + <span>{fmtDate(p.unlockAt)}</span> |
| 81 | + )} |
| 82 | + </Metric> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +export function StakePositions() { |
| 89 | + const t = useTranslations("stake.positions"); |
| 90 | + const you = useActiveAccount()?.address; |
| 91 | + const pos = useMorpheusPosition(you); |
| 92 | + // Lazy initializer keeps render pure — the 7-day / power locks don't need a |
| 93 | + // live tick; a refresh re-reads the clock. |
| 94 | + const [now] = useState(() => Math.floor(Date.now() / 1000)); |
| 95 | + |
| 96 | + const staked = pos?.pools.filter((p) => p.staked > 0) ?? []; |
| 97 | + const anyLocked = staked.some((p) => p.morUnlockAt > now); |
| 98 | + |
| 99 | + let body: React.ReactNode; |
| 100 | + if (!you) body = <p className="text-sm text-muted-foreground">{t("connect")}</p>; |
| 101 | + else if (!pos) body = <p className="text-sm text-muted-foreground">{t("loading")}</p>; |
| 102 | + else if (staked.length === 0) body = <p className="text-sm text-muted-foreground">{t("none")}</p>; |
| 103 | + else |
| 104 | + body = ( |
| 105 | + <div className="space-y-3"> |
| 106 | + {staked.map((p) => ( |
| 107 | + <PositionRow key={p.asset} p={p} now={now} t={t} /> |
| 108 | + ))} |
| 109 | + <p className="pt-1 text-xs leading-relaxed text-muted-foreground">{t("claimHint")}</p> |
| 110 | + {anyLocked && <p className="text-xs leading-relaxed text-muted-foreground">{t("powerLockNote")}</p>} |
| 111 | + </div> |
| 112 | + ); |
| 113 | + |
| 114 | + return ( |
| 115 | + <Card> |
| 116 | + <CardHeader> |
| 117 | + <CardTitle>{t("title")}</CardTitle> |
| 118 | + <CardDescription>{t("subtitle")}</CardDescription> |
| 119 | + </CardHeader> |
| 120 | + <CardContent>{body}</CardContent> |
| 121 | + </Card> |
| 122 | + ); |
| 123 | +} |
0 commit comments