|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useEffect, useLayoutEffect, useRef, useState } from "react"; |
4 | | -import { formatLava, formatLavaKMB, formatNumber } from "@/lib/format"; |
| 3 | +import { formatLava, formatLavaKMB } from "@/lib/format"; |
5 | 4 |
|
6 | 5 | interface LavaAmountProps { |
7 | 6 | amount: string | bigint | number; |
8 | 7 | showDenom?: boolean; |
9 | 8 | } |
10 | 9 |
|
11 | 10 | /** |
12 | | - * Displays a LAVA amount that adapts to its container width. |
13 | | - * Shows the full formatted number when there's room; collapses to |
14 | | - * K/M/B/T shorthand if the full string would overflow the parent. |
15 | | - * Hover (or long-press on touch) reveals the exact ulava value. |
| 11 | + * Displays a LAVA amount in compact K/M/B/T form on both mobile and desktop. |
| 12 | + * Hover (or long-press) reveals the exact LAVA value. |
16 | 13 | */ |
17 | 14 | export function LavaAmount({ amount, showDenom = true }: LavaAmountProps) { |
18 | 15 | const denom = showDenom ? " LAVA" : ""; |
19 | | - const fullText = `${formatLava(amount)}${denom}`; |
20 | 16 | const compactText = `${formatLavaKMB(amount)}${denom}`; |
21 | | - const raw = String(amount); |
22 | | - |
23 | | - const wrapperRef = useRef<HTMLSpanElement>(null); |
24 | | - const measureRef = useRef<HTMLSpanElement>(null); |
25 | | - const [useCompact, setUseCompact] = useState(false); |
26 | | - const [isMobile, setIsMobile] = useState(false); |
27 | | - |
28 | | - // Always compact on mobile — no measurement needed. |
29 | | - useEffect(() => { |
30 | | - if (typeof window === "undefined") return; |
31 | | - const mq = window.matchMedia("(max-width: 767px)"); |
32 | | - const update = () => setIsMobile(mq.matches); |
33 | | - update(); |
34 | | - mq.addEventListener("change", update); |
35 | | - return () => mq.removeEventListener("change", update); |
36 | | - }, []); |
37 | | - |
38 | | - useLayoutEffect(() => { |
39 | | - if (isMobile) return; // Mobile always uses compact, skip measurement. |
40 | | - const wrapper = wrapperRef.current; |
41 | | - const measure = measureRef.current; |
42 | | - if (!wrapper || !measure) return; |
43 | | - |
44 | | - const check = () => { |
45 | | - const parent = wrapper.parentElement; |
46 | | - if (!parent) return; |
47 | | - const available = parent.clientWidth - 6; |
48 | | - const needed = measure.scrollWidth; |
49 | | - if (available > 0 && needed > 0) setUseCompact(needed > available); |
50 | | - }; |
51 | | - |
52 | | - check(); |
53 | | - const parent = wrapper.parentElement; |
54 | | - if (!parent) return; |
55 | | - const ro = new ResizeObserver(check); |
56 | | - ro.observe(parent); |
57 | | - return () => ro.disconnect(); |
58 | | - }, [fullText, isMobile]); |
59 | | - |
60 | | - // Re-measure on font load (digit widths can shift between fallback and webfont) |
61 | | - useEffect(() => { |
62 | | - if (isMobile) return; |
63 | | - if (typeof document === "undefined" || !document.fonts) return; |
64 | | - const handler = () => { |
65 | | - const wrapper = wrapperRef.current; |
66 | | - const measure = measureRef.current; |
67 | | - if (!wrapper || !measure) return; |
68 | | - const parent = wrapper.parentElement; |
69 | | - if (!parent) return; |
70 | | - setUseCompact(measure.scrollWidth > parent.clientWidth - 6); |
71 | | - }; |
72 | | - document.fonts.ready.then(handler); |
73 | | - }, [isMobile]); |
74 | | - |
75 | | - const useCompactFinal = isMobile || useCompact; |
| 17 | + const fullText = `${formatLava(amount)}${denom}`; |
76 | 18 |
|
77 | 19 | return ( |
78 | | - <> |
79 | | - <span |
80 | | - ref={wrapperRef} |
81 | | - title={`${formatNumber(raw)} ulava`} |
82 | | - className="cursor-help" |
83 | | - > |
84 | | - {useCompactFinal ? compactText : fullText} |
85 | | - </span> |
86 | | - {/* Off-screen measurement element — has the full text so we can detect overflow without flip-flopping */} |
87 | | - <span |
88 | | - ref={measureRef} |
89 | | - aria-hidden="true" |
90 | | - className="absolute -left-[9999px] top-0 whitespace-nowrap pointer-events-none" |
91 | | - > |
92 | | - {fullText} |
93 | | - </span> |
94 | | - </> |
| 20 | + <span |
| 21 | + title={fullText} |
| 22 | + className="cursor-help" |
| 23 | + > |
| 24 | + {compactText} |
| 25 | + </span> |
95 | 26 | ); |
96 | 27 | } |
0 commit comments