import { useState, useEffect } from "react"; const STATIC_ASSETS = "https://warframe.market/static/assets"; function median(arr) { if (arr.length === 0) return null; const sorted = [...arr].sort((a, b) => a - b); const mid = Math.floor(sorted.length / 2); return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; } export default function ItemDetail({ slug, onBack }) { const [orders, setOrders] = useState(null); const [item, setItem] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { let cancelled = false; async function load() { setLoading(true); setError(null); try { const [ordersRes, catalogRes] = await Promise.all([ fetch(`/api/orders/${encodeURIComponent(slug)}`), fetch("/api/items"), ]); if (!ordersRes.ok) throw new Error(`Orders HTTP ${ordersRes.status}`); const ordersData = await ordersRes.json(); if (!cancelled) setOrders(ordersData); if (catalogRes.ok) { const catalogData = await catalogRes.json(); const found = catalogData.find((i) => i.slug === slug); if (!cancelled) setItem(found); } } catch (err) { if (!cancelled) setError(err.message); } finally { if (!cancelled) setLoading(false); } } load(); return () => { cancelled = true; }; }, [slug]); if (loading) { return
Loading orders...
; } if (error) { return (

Failed to load orders: {error}

); } const name = item?.i18n?.en?.name || slug; const thumb = item?.i18n?.en?.thumb; const onlineFilter = (o) => o.user?.status === "ingame" || o.user?.status === "online"; const sells = orders .filter((o) => o.type === "sell" && onlineFilter(o)) .sort((a, b) => a.platinum - b.platinum) .slice(0, 5); const buys = orders .filter((o) => o.type === "buy" && onlineFilter(o)) .sort((a, b) => b.platinum - a.platinum) .slice(0, 5); const allSells = orders.filter((o) => o.type === "sell" && onlineFilter(o)); const sellPrices = allSells.map((o) => o.platinum); const stats = { median: median(sellPrices), min: sellPrices.length > 0 ? Math.min(...sellPrices) : null, max: sellPrices.length > 0 ? Math.max(...sellPrices) : null, count: allSells.length, }; return (
{thumb && ( {name} )}

{name}

/v2/orders/item/{slug}
Median Sell
{stats.median != null ? `${stats.median}p` : "\u2014"}
Min Sell
{stats.min != null ? `${stats.min}p` : "\u2014"}
Max Sell
{stats.max != null ? `${stats.max}p` : "\u2014"}
Active Sells
{stats.count}
{sells.length === 0 && buys.length === 0 ? (
No active orders from online/ingame users.
) : ( <> {sells.length > 0 && (

Top Sell Orders (low to high)

{sells.map((o) => ( ))}
Price Qty User Rep Status
{o.platinum}p {o.quantity} {o.user?.ingameName || "?"} {o.user?.reputation ?? "?"} {o.user?.status}
)} {buys.length > 0 && (

Top Buy Orders (high to low)

{buys.map((o) => ( ))}
Price Qty User Rep Status
{o.platinum}p {o.quantity} {o.user?.ingameName || "?"} {o.user?.reputation ?? "?"} {o.user?.status}
)} )}
); }