receive-panel.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "use client";
  2. /**
  3. * Receive — show address + QR so others can send to this mavi wallet.
  4. * Address is public metadata (no unlock required).
  5. */
  6. import Link from "next/link";
  7. import QRCode from "qrcode";
  8. import { useEffect, useState } from "react";
  9. import { MaviWalletLogo } from "@/components/wallet/wallet-logos";
  10. import { Button, buttonVariants } from "@/components/ui/button";
  11. import {
  12. Card,
  13. CardContent,
  14. CardDescription,
  15. CardHeader,
  16. CardTitle,
  17. } from "@/components/ui/card";
  18. import { EmptyState } from "@/components/ui/empty-state";
  19. import { ErrorBanner } from "@/components/ui/error-banner";
  20. import { hasVault, readVault, shortAddress } from "@/lib/mavi/storage";
  21. import { cn } from "@/lib/utils";
  22. export function ReceivePanel() {
  23. const [mounted, setMounted] = useState(false);
  24. const [address, setAddress] = useState<string | null>(null);
  25. const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);
  26. const [copied, setCopied] = useState(false);
  27. const [error, setError] = useState<string | null>(null);
  28. const [shareOk, setShareOk] = useState(false);
  29. useEffect(() => {
  30. setMounted(true);
  31. setShareOk(typeof navigator !== "undefined" && Boolean(navigator.share));
  32. if (!hasVault()) {
  33. setAddress(null);
  34. return;
  35. }
  36. const vault = readVault();
  37. setAddress(vault?.address ?? null);
  38. }, []);
  39. useEffect(() => {
  40. if (!address) {
  41. setQrDataUrl(null);
  42. return;
  43. }
  44. let cancelled = false;
  45. void QRCode.toDataURL(address, {
  46. width: 240,
  47. margin: 2,
  48. color: {
  49. dark: "#0a0a0a",
  50. light: "#ffffff",
  51. },
  52. errorCorrectionLevel: "M",
  53. })
  54. .then((url) => {
  55. if (!cancelled) setQrDataUrl(url);
  56. })
  57. .catch(() => {
  58. if (!cancelled) {
  59. setError("Could not draw the QR code. You can still copy the address.");
  60. }
  61. });
  62. return () => {
  63. cancelled = true;
  64. };
  65. }, [address]);
  66. function onCopy() {
  67. if (!address) return;
  68. void navigator.clipboard.writeText(address).then(() => {
  69. setCopied(true);
  70. setTimeout(() => setCopied(false), 1600);
  71. });
  72. }
  73. async function onShare() {
  74. if (!address || !navigator.share) return;
  75. try {
  76. await navigator.share({
  77. title: "mavi wallet address",
  78. text: address,
  79. });
  80. } catch {
  81. /* user cancelled */
  82. }
  83. }
  84. if (!mounted) {
  85. return (
  86. <Card>
  87. <CardContent className="py-10 text-center text-sm text-muted-foreground">
  88. Loading…
  89. </CardContent>
  90. </Card>
  91. );
  92. }
  93. if (!address) {
  94. return (
  95. <EmptyState
  96. title="No mavi wallet yet"
  97. description="Create or import a wallet first. Then this page shows your address and a QR code so others can send to you."
  98. action={
  99. <div className="flex w-full max-w-sm flex-col gap-2">
  100. <Link
  101. href="/portal/mavi/create"
  102. className={cn(
  103. buttonVariants({ variant: "primary" }),
  104. "inline-flex items-center justify-center gap-2",
  105. )}
  106. >
  107. <MaviWalletLogo size={18} />
  108. Create mavi wallet
  109. </Link>
  110. <Link
  111. href="/portal/mavi/import"
  112. className={cn(buttonVariants({ variant: "outline" }), "w-full")}
  113. >
  114. Import recovery phrase
  115. </Link>
  116. </div>
  117. }
  118. />
  119. );
  120. }
  121. return (
  122. <Card>
  123. <CardHeader>
  124. <div className="mb-2 flex items-center gap-2">
  125. <MaviWalletLogo size={28} />
  126. <CardTitle>Receive</CardTitle>
  127. </div>
  128. <CardDescription>
  129. Share this address so someone can send to your mavi wallet. Anyone
  130. with the address can send; only you control spending with your
  131. password and recovery phrase.
  132. </CardDescription>
  133. </CardHeader>
  134. <CardContent className="space-y-5">
  135. {error ? <ErrorBanner title="QR note">{error}</ErrorBanner> : null}
  136. <div className="flex flex-col items-center gap-3">
  137. <div className="rounded-2xl border border-border bg-white p-4 shadow-sm">
  138. {qrDataUrl ? (
  139. // eslint-disable-next-line @next/next/no-img-element
  140. <img
  141. src={qrDataUrl}
  142. alt={`QR code for ${shortAddress(address)}`}
  143. width={240}
  144. height={240}
  145. className="h-60 w-60"
  146. />
  147. ) : (
  148. <div
  149. className="flex h-60 w-60 items-center justify-center text-sm text-muted-foreground"
  150. aria-busy="true"
  151. >
  152. Drawing QR…
  153. </div>
  154. )}
  155. </div>
  156. <p className="text-center text-xs text-muted-foreground">
  157. Scan with a wallet camera · short form{" "}
  158. <span className="font-mono text-foreground">
  159. {shortAddress(address)}
  160. </span>
  161. </p>
  162. </div>
  163. <div className="rounded-xl border border-border bg-muted/30 px-4 py-3">
  164. <p className="text-xs uppercase tracking-wide text-muted-foreground">
  165. Your address
  166. </p>
  167. <p className="mt-1 break-all font-mono text-sm text-foreground">
  168. {address}
  169. </p>
  170. </div>
  171. <div className="flex flex-col gap-2 sm:flex-row">
  172. <Button type="button" className="sm:flex-1" onClick={onCopy}>
  173. {copied ? "Copied" : "Copy address"}
  174. </Button>
  175. {shareOk ? (
  176. <Button
  177. type="button"
  178. variant="outline"
  179. className="sm:flex-1"
  180. onClick={() => void onShare()}
  181. >
  182. Share
  183. </Button>
  184. ) : null}
  185. <Link
  186. href="/portal/mavi"
  187. className={cn(
  188. buttonVariants({ variant: "ghost" }),
  189. "sm:flex-1 text-center",
  190. )}
  191. >
  192. Back to mavi
  193. </Link>
  194. </div>
  195. </CardContent>
  196. </Card>
  197. );
  198. }