|
| 1 | +import { getTranslations } from "next-intl/server"; |
| 2 | +import { ArrowDownLeft, ExternalLink, Gavel } from "lucide-react"; |
| 3 | +import { AddressDisplay } from "@/components/ui/address-display"; |
| 4 | +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
| 5 | +import { loadTreasuryInflows, type InflowAsset } from "@/services/treasury-inflows"; |
| 6 | + |
| 7 | +/** Per-asset accent. Deliberately not the semantic tokens — these identify a currency. */ |
| 8 | +const ASSET_TONE: Record<InflowAsset, string> = { |
| 9 | + ETH: "text-[#627eea]", |
| 10 | + WETH: "text-[#627eea]", |
| 11 | + USDC: "text-[#2775ca]", |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * ETH is worth ~4 decimals; USDC is a dollar figure and reads wrong with more |
| 16 | + * than two. `maximumFractionDigits` alone would print `0` for auction dust, so |
| 17 | + * very small ETH amounts keep enough significant digits to stay non-zero. |
| 18 | + */ |
| 19 | +function formatAmount(amount: number, asset: InflowAsset, locale: string): string { |
| 20 | + if (asset === "USDC") { |
| 21 | + return amount.toLocaleString(locale, { minimumFractionDigits: 2, maximumFractionDigits: 2 }); |
| 22 | + } |
| 23 | + if (amount > 0 && amount < 0.0001) { |
| 24 | + return amount.toLocaleString(locale, { maximumSignificantDigits: 2 }); |
| 25 | + } |
| 26 | + return amount.toLocaleString(locale, { maximumFractionDigits: 4 }); |
| 27 | +} |
| 28 | + |
| 29 | +function ageLabel(at: string, now: number): string { |
| 30 | + const ms = now - new Date(at).getTime(); |
| 31 | + if (!Number.isFinite(ms) || ms < 0) return ""; |
| 32 | + const mins = Math.floor(ms / 60_000); |
| 33 | + if (mins < 60) return `${Math.max(1, mins)}m`; |
| 34 | + const hours = Math.floor(mins / 60); |
| 35 | + if (hours < 24) return `${hours}h`; |
| 36 | + const days = Math.floor(hours / 24); |
| 37 | + if (days < 30) return `${days}d`; |
| 38 | + return `${Math.floor(days / 30)}mo`; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * The treasury's most recent income, newest first. |
| 43 | + * |
| 44 | + * Auction settlements arrive as internal transfers and are marked as such — it |
| 45 | + * is the difference between "someone sent us money" and "the DAO earned it". |
| 46 | + */ |
| 47 | +export async function TreasuryInflows({ locale }: { locale: string }) { |
| 48 | + const t = await getTranslations("treasury.inflows"); |
| 49 | + const inflows = await loadTreasuryInflows(8); |
| 50 | + // Rendered once per request on the server; the timestamp IS the snapshot. |
| 51 | + // eslint-disable-next-line react-hooks/purity -- server component, render-time clock read for relative ages |
| 52 | + const now = Date.now(); |
| 53 | + |
| 54 | + return ( |
| 55 | + <Card className="gap-2"> |
| 56 | + <CardHeader> |
| 57 | + <CardTitle className="flex items-center gap-2 text-sm font-medium"> |
| 58 | + <ArrowDownLeft className="size-4 text-emerald-500" /> |
| 59 | + {t("title")} |
| 60 | + </CardTitle> |
| 61 | + </CardHeader> |
| 62 | + <CardContent> |
| 63 | + {inflows.length === 0 ? ( |
| 64 | + <p className="py-6 text-center text-sm text-muted-foreground">{t("empty")}</p> |
| 65 | + ) : ( |
| 66 | + <ul className="divide-y divide-border"> |
| 67 | + {inflows.map((flow) => ( |
| 68 | + <li key={flow.hash} className="flex items-center gap-3 py-2.5"> |
| 69 | + <div className="flex min-w-0 flex-1 items-center gap-2"> |
| 70 | + <AddressDisplay |
| 71 | + address={flow.from} |
| 72 | + variant="compact" |
| 73 | + showCopy={false} |
| 74 | + showExplorer={false} |
| 75 | + truncateLength={4} |
| 76 | + /> |
| 77 | + {flow.internal ? ( |
| 78 | + <span |
| 79 | + className="inline-flex shrink-0 items-center gap-1 rounded-full bg-muted px-2 py-0.5 text-[10px] font-medium text-muted-foreground" |
| 80 | + title={t("auctionHint")} |
| 81 | + > |
| 82 | + <Gavel className="size-3" /> |
| 83 | + {t("auction")} |
| 84 | + </span> |
| 85 | + ) : null} |
| 86 | + </div> |
| 87 | + |
| 88 | + <span className="shrink-0 whitespace-nowrap font-mono text-sm font-semibold tabular-nums"> |
| 89 | + +{formatAmount(flow.amount, flow.asset, locale)}{" "} |
| 90 | + <span className={ASSET_TONE[flow.asset]}>{flow.asset}</span> |
| 91 | + </span> |
| 92 | + |
| 93 | + <span className="w-9 shrink-0 text-right font-mono text-[11px] text-muted-foreground"> |
| 94 | + {ageLabel(flow.at, now)} |
| 95 | + </span> |
| 96 | + |
| 97 | + <a |
| 98 | + href={`https://basescan.org/tx/${flow.hash}`} |
| 99 | + target="_blank" |
| 100 | + rel="noopener noreferrer" |
| 101 | + aria-label={t("viewTx")} |
| 102 | + className="shrink-0 text-muted-foreground hover:text-foreground" |
| 103 | + > |
| 104 | + <ExternalLink className="size-3.5" /> |
| 105 | + </a> |
| 106 | + </li> |
| 107 | + ))} |
| 108 | + </ul> |
| 109 | + )} |
| 110 | + </CardContent> |
| 111 | + </Card> |
| 112 | + ); |
| 113 | +} |
0 commit comments