({ tokenId, currentPrice, compact }: { tokenId: string; currentPrice: number; compact?: boolean })
| 86 | } |
| 87 | |
| 88 | function MiniOrderBook({ tokenId, currentPrice, compact }: { tokenId: string; currentPrice: number; compact?: boolean }) { |
| 89 | const { t } = useI18n(); |
| 90 | const [book, setBook] = useState<BookData | null>(null); |
| 91 | const [loading, setLoading] = useState(true); |
| 92 | const abortRef = useRef<AbortController | null>(null); |
| 93 | |
| 94 | useEffect(() => { |
| 95 | let cancelled = false; |
| 96 | |
| 97 | const fetchLatest = async () => { |
| 98 | abortRef.current?.abort(); |
| 99 | const ac = new AbortController(); |
| 100 | abortRef.current = ac; |
| 101 | try { |
| 102 | const data = await loadOrderBook(tokenId, ac.signal); |
| 103 | if (!cancelled && !ac.signal.aborted) { |
| 104 | setBook(data); |
| 105 | setLoading(false); |
| 106 | } |
| 107 | } catch { |
| 108 | // aborted or network error |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | void fetchLatest(); |
| 113 | const iv = setInterval(() => { |
| 114 | void fetchLatest(); |
| 115 | }, 5_000); |
| 116 | |
| 117 | return () => { |
| 118 | cancelled = true; |
| 119 | clearInterval(iv); |
| 120 | abortRef.current?.abort(); |
| 121 | }; |
| 122 | }, [tokenId]); |
| 123 | |
| 124 | // Compute cumulative sizes for depth bars |
| 125 | const ROWS = 8; |
| 126 | const asks = useMemo(() => { |
| 127 | if (!book?.asks?.length) return []; |
| 128 | const sorted = [...book.asks].sort((a, b) => a.price - b.price).slice(0, ROWS); |
| 129 | let cum = 0; |
| 130 | return sorted.map(l => { cum += l.size; return { ...l, cum }; }); |
| 131 | }, [book]); |
| 132 | |
| 133 | const bids = useMemo(() => { |
| 134 | if (!book?.bids?.length) return []; |
| 135 | const sorted = [...book.bids].sort((a, b) => b.price - a.price).slice(0, ROWS); |
| 136 | let cum = 0; |
| 137 | return sorted.map(l => { cum += l.size; return { ...l, cum }; }); |
| 138 | }, [book]); |
| 139 | |
| 140 | const maxCum = Math.max(asks[asks.length - 1]?.cum ?? 0, bids[bids.length - 1]?.cum ?? 0) || 1; |
| 141 | const spread = asks[0] && bids[0] ? ((asks[0].price - bids[0].price) * 100).toFixed(1) : null; |
| 142 | |
| 143 | if (loading) return ( |
| 144 | <div className="flex items-center justify-center h-full text-[var(--text-dim)] text-[10px]">{t("common.loading")}</div> |
| 145 | ); |
nothing calls this directly
no test coverage detected