activity-list.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use client";
  2. import Link from "next/link";
  3. import { useCallback, useEffect, useState } from "react";
  4. import { Badge } from "@/components/ui/badge";
  5. import { Button, buttonVariants } from "@/components/ui/button";
  6. import {
  7. Card,
  8. CardContent,
  9. CardDescription,
  10. CardHeader,
  11. CardTitle,
  12. } from "@/components/ui/card";
  13. import { EmptyState } from "@/components/ui/empty-state";
  14. import {
  15. clearActivityForWallet,
  16. formatActivityTime,
  17. readActivityForWallet,
  18. shortHash,
  19. type MaviActivityItem,
  20. } from "@/lib/mavi/activity";
  21. import { shortAddress } from "@/lib/mavi/storage";
  22. import { cn } from "@/lib/utils";
  23. // Re-export for callers that imported from this module earlier
  24. export { notifyActivityChanged } from "@/lib/mavi/activity";
  25. export function ActivityList({
  26. walletAddress,
  27. /** Compact strip on home (fewer rows) */
  28. compact = false,
  29. className,
  30. }: {
  31. walletAddress: string;
  32. compact?: boolean;
  33. className?: string;
  34. }) {
  35. const [items, setItems] = useState<MaviActivityItem[]>([]);
  36. const [mounted, setMounted] = useState(false);
  37. const refresh = useCallback(() => {
  38. setItems(readActivityForWallet(walletAddress, compact ? 5 : 30));
  39. }, [compact, walletAddress]);
  40. useEffect(() => {
  41. setMounted(true);
  42. refresh();
  43. const onStorage = (e: StorageEvent) => {
  44. if (e.key === "mavi_activity_v1" || e.key === null) refresh();
  45. };
  46. window.addEventListener("storage", onStorage);
  47. window.addEventListener("mavi-activity", refresh);
  48. return () => {
  49. window.removeEventListener("storage", onStorage);
  50. window.removeEventListener("mavi-activity", refresh);
  51. };
  52. }, [refresh]);
  53. if (!mounted) {
  54. return (
  55. <Card className={className}>
  56. <CardContent className="py-8 text-center text-sm text-muted-foreground">
  57. Loading activity…
  58. </CardContent>
  59. </Card>
  60. );
  61. }
  62. return (
  63. <Card className={className}>
  64. <CardHeader className="flex flex-row flex-wrap items-start justify-between gap-2">
  65. <div>
  66. <CardTitle className="text-base">Activity</CardTitle>
  67. <CardDescription>
  68. Recent mavi actions in this browser (not a full chain explorer).
  69. </CardDescription>
  70. </div>
  71. {items.length > 0 && !compact ? (
  72. <Button
  73. type="button"
  74. variant="ghost"
  75. size="sm"
  76. onClick={() => {
  77. if (
  78. window.confirm(
  79. "Clear activity history for this wallet on this browser?",
  80. )
  81. ) {
  82. clearActivityForWallet(walletAddress);
  83. refresh();
  84. }
  85. }}
  86. >
  87. Clear
  88. </Button>
  89. ) : null}
  90. </CardHeader>
  91. <CardContent className="space-y-3">
  92. {items.length === 0 ? (
  93. <EmptyState
  94. className="border-0 bg-transparent py-6"
  95. title="No activity yet"
  96. description="When you send or get test ETH, it shows up here."
  97. action={
  98. <Link
  99. href="/portal/mavi/send"
  100. className={cn(buttonVariants({ size: "sm" }))}
  101. >
  102. Send
  103. </Link>
  104. }
  105. />
  106. ) : (
  107. <ul className="space-y-2">
  108. {items.map((item) => (
  109. <li
  110. key={item.id}
  111. className="flex flex-wrap items-center justify-between gap-2 rounded-lg border border-border/70 bg-muted/20 px-3 py-2.5"
  112. >
  113. <div className="min-w-0">
  114. <div className="flex flex-wrap items-center gap-2">
  115. <Badge variant="outline">{kindLabel(item.kind)}</Badge>
  116. {item.amountEth ? (
  117. <span className="text-sm font-medium tabular-nums">
  118. {item.kind === "send" ? "−" : "+"}
  119. {trimEth(item.amountEth)} ETH
  120. </span>
  121. ) : null}
  122. </div>
  123. <p className="mt-1 text-xs text-muted-foreground">
  124. {item.kind === "send" && item.to
  125. ? `To ${shortAddress(item.to)}`
  126. : item.kind === "fund"
  127. ? "From local test bank"
  128. : item.note ?? ""}
  129. {item.hash ? (
  130. <>
  131. {" · "}
  132. <span className="font-mono">{shortHash(item.hash)}</span>
  133. </>
  134. ) : null}
  135. </p>
  136. </div>
  137. <time
  138. className="shrink-0 text-[11px] text-muted-foreground"
  139. dateTime={item.at}
  140. >
  141. {formatActivityTime(item.at)}
  142. </time>
  143. </li>
  144. ))}
  145. </ul>
  146. )}
  147. {compact && items.length > 0 ? (
  148. <Link
  149. href="/portal/mavi/activity"
  150. className={cn(
  151. buttonVariants({ variant: "ghost", size: "sm" }),
  152. "px-0",
  153. )}
  154. >
  155. Full activity →
  156. </Link>
  157. ) : null}
  158. </CardContent>
  159. </Card>
  160. );
  161. }
  162. function kindLabel(kind: MaviActivityItem["kind"]): string {
  163. switch (kind) {
  164. case "send":
  165. return "Sent";
  166. case "fund":
  167. return "Test ETH";
  168. case "receive_note":
  169. return "Received";
  170. default:
  171. return kind;
  172. }
  173. }
  174. function trimEth(eth: string): string {
  175. const n = Number(eth);
  176. if (!Number.isFinite(n)) return eth;
  177. if (n === 0) return "0";
  178. if (n >= 1) return n.toFixed(4).replace(/\.?0+$/, "");
  179. return n.toFixed(6).replace(/\.?0+$/, "");
  180. }