Spaces:
Paused
Paused
File size: 7,690 Bytes
6512187 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | import { useState, useEffect, useCallback, useRef } from "react";
import { useWatchlist } from "../context/WatchlistContext.jsx";
const STATIC_ASSETS = "https://warframe.market/static/assets";
function computeStats(orders) {
const sells = orders
.filter((o) => o.type === "sell" && o.user?.status !== "invisible")
.sort((a, b) => a.platinum - b.platinum);
const buys = orders
.filter((o) => o.type === "buy" && o.user?.status !== "invisible")
.sort((a, b) => b.platinum - a.platinum);
const lowestSell = sells.length > 0 ? sells[0].platinum : null;
const highestBuy = buys.length > 0 ? buys[0].platinum : null;
const activeSells = sells.length;
return { lowestSell, highestBuy, activeSells };
}
export default function WatchlistTable({ priceMin, priceMax, onItemSelect }) {
const { watchlist, toggleItem } = useWatchlist();
const [catalog, setCatalog] = useState({});
const [ordersCache, setOrdersCache] = useState({});
const [loading, setLoading] = useState(true);
const [errors, setErrors] = useState({});
const [sortKey, setSortKey] = useState("name");
const [sortAsc, setSortAsc] = useState(true);
const intervalRef = useRef(null);
// Fetch catalog for watchlist items
useEffect(() => {
let cancelled = false;
async function loadCatalog() {
try {
const res = await fetch("/api/items");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
if (!cancelled) {
const map = {};
for (const item of data) map[item.slug] = item;
setCatalog(map);
}
} catch (err) {
console.error("Failed to load catalog:", err);
} finally {
if (!cancelled) setLoading(false);
}
}
loadCatalog();
return () => { cancelled = true; };
}, []);
const fetchOrders = useCallback(async () => {
for (const slug of watchlist) {
try {
const res = await fetch(`/api/orders/${encodeURIComponent(slug)}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
setOrdersCache((prev) => ({ ...prev, [slug]: data }));
setErrors((prev) => {
const next = { ...prev };
delete next[slug];
return next;
});
} catch (err) {
setErrors((prev) => ({ ...prev, [slug]: err.message }));
}
}
}, [watchlist]);
useEffect(() => {
if (watchlist.length === 0) return;
fetchOrders();
intervalRef.current = setInterval(fetchOrders, 60000);
return () => clearInterval(intervalRef.current);
}, [watchlist, fetchOrders]);
const rows = watchlist
.filter((slug) => catalog[slug])
.map((slug) => {
const item = catalog[slug];
const orders = ordersCache[slug] || [];
const stats = computeStats(orders);
const name = item.i18n?.en?.name || slug;
const thumb = item.i18n?.en?.thumb;
const tags = item.tags || [];
return { slug, name, thumb, tags, ...stats, hasError: !!errors[slug] };
});
const sorted = [...rows].sort((a, b) => {
let va, vb;
if (sortKey === "name") { va = a.name; vb = b.name; }
else if (sortKey === "platinum") { va = a.lowestSell ?? 99999; vb = b.lowestSell ?? 99999; }
else if (sortKey === "buy") { va = a.highestBuy ?? -1; vb = b.highestBuy ?? -1; }
else if (sortKey === "spread") {
va = (a.lowestSell != null && a.highestBuy != null) ? a.lowestSell - a.highestBuy : 99999;
vb = (b.lowestSell != null && b.highestBuy != null) ? b.lowestSell - b.highestBuy : 99999;
}
else { va = a.activeSells; vb = b.activeSells; }
if (va < vb) return sortAsc ? -1 : 1;
if (va > vb) return sortAsc ? 1 : -1;
return 0;
});
const filtered = sorted.filter((r) => {
if (r.lowestSell == null && r.highestBuy == null) return true;
const effective = r.lowestSell ?? 0;
return effective >= priceMin && effective <= priceMax;
});
const sortArrow = (key) => {
if (sortKey !== key) return "";
return <span className="sort-arrow">{sortAsc ? "\u25B2" : "\u25BC"}</span>;
};
if (loading) {
return <div className="loading"><div className="spinner" /> Loading catalog...</div>;
}
if (watchlist.length === 0) {
return (
<div className="empty-msg">
Your watchlist is empty. Go to the Catalog tab and star some items.
</div>
);
}
return (
<div className="table-wrap">
<table>
<thead>
<tr>
<th style={{ width: 40 }}></th>
<th onClick={() => { setSortKey("name"); setSortAsc(sortKey === "name" ? !sortAsc : true); }}>
Name{sortArrow("name")}
</th>
<th>Tags</th>
<th onClick={() => { setSortKey("platinum"); setSortAsc(sortKey === "platinum" ? !sortAsc : true); }}>
Platinum{sortArrow("platinum")}
</th>
<th onClick={() => { setSortKey("buy"); setSortAsc(sortKey === "buy" ? !sortAsc : true); }}>
Highest Buy{sortArrow("buy")}
</th>
<th onClick={() => { setSortKey("spread"); setSortAsc(sortKey === "spread" ? !sortAsc : true); }}>
Spread{sortArrow("spread")}
</th>
<th onClick={() => { setSortKey("sells"); setSortAsc(sortKey === "sells" ? !sortAsc : true); }}>
# Sells{sortArrow("sells")}
</th>
</tr>
</thead>
<tbody>
{filtered.map((row) => {
const spread =
row.lowestSell != null && row.highestBuy != null
? row.lowestSell - row.highestBuy
: null;
return (
<tr key={row.slug} style={{ cursor: "pointer" }} onClick={() => onItemSelect(row.slug)}>
<td>
<button
className="star-btn starred"
onClick={(e) => { e.stopPropagation(); toggleItem(row.slug); }}
title="Remove from watchlist"
>
{"\u2605"}
</button>
</td>
<td>
<div className="item-name">
{row.thumb && (
<img
className="item-thumb"
src={`${STATIC_ASSETS}/${row.thumb}`}
alt=""
loading="lazy"
/>
)}
<span>{row.name}</span>
{row.hasError && <span style={{ color: "var(--red)", fontSize: "0.75rem" }}> (error)</span>}
</div>
</td>
<td>
{row.tags.slice(0, 3).map((tag) => (
<span key={tag} className="tag-badge">{tag}</span>
))}
</td>
<td className="price-sell">
{row.lowestSell != null ? `${row.lowestSell}\u25BC` : "\u2014"}
</td>
<td className="price-buy">
{row.highestBuy != null ? `${row.highestBuy}\u25B2` : "\u2014"}
</td>
<td className="price-spread">
{spread != null ? (spread >= 0 ? `+${spread}` : spread) : "\u2014"}
</td>
<td>{row.activeSells}</td>
</tr>
);
})}
{filtered.length === 0 && (
<tr>
<td colSpan={7} className="empty-msg">
No watchlist items match the price filter.
</td>
</tr>
)}
</tbody>
</table>
</div>
);
}
|