Spaces:
Paused
Paused
File size: 5,180 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 | import { useState, useEffect, useCallback, useRef } from "react";
import { useWatchlist } from "../context/WatchlistContext.jsx";
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
ResponsiveContainer,
} from "recharts";
const COLORS = [
"#f0b232", "#58a6ff", "#3fb950", "#f85149", "#bc8cff",
"#d2a8ff", "#79c0ff", "#56d364", "#ff7b72", "#ffa657",
"#a5d6ff", "#7ee787",
];
function computeLowestSell(orders) {
const sells = orders
.filter((o) => o.type === "sell" && o.user?.status !== "invisible");
if (sells.length === 0) return null;
return Math.min(...sells.map((o) => o.platinum));
}
export default function SummaryChart({ priceMin, priceMax }) {
const { watchlist } = useWatchlist();
const [catalog, setCatalog] = useState({});
const [ordersCache, setOrdersCache] = useState({});
const [loading, setLoading] = useState(true);
const [errors, setErrors] = useState({});
const intervalRef = useRef(null);
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("Catalog fetch failed:", 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 chartData = watchlist
.filter((slug) => catalog[slug])
.map((slug) => {
const item = catalog[slug];
const orders = ordersCache[slug] || [];
const lowestSell = computeLowestSell(orders);
const name = item.i18n?.en?.name || slug;
const tags = item.tags || [];
return { slug, name, lowestSell, tags };
})
.filter((d) => d.lowestSell != null)
.filter((d) => d.lowestSell >= priceMin && d.lowestSell <= priceMax)
.sort((a, b) => a.lowestSell - b.lowestSell);
// Determine unique categories for coloring
const categories = [...new Set(chartData.flatMap((d) => d.tags))].sort();
const colorMap = {};
categories.forEach((cat, i) => {
colorMap[cat] = COLORS[i % COLORS.length];
});
if (loading) {
return <div className="loading"><div className="spinner" /> Loading...</div>;
}
if (watchlist.length === 0) {
return (
<div className="empty-msg">
Add items to your watchlist to see the dashboard.
</div>
);
}
if (chartData.length === 0) {
return (
<div className="empty-msg">
No watchlist items with price data. Make sure items have active orders.
</div>
);
}
// Build stacked data: for each item, one bar segment per first tag
const stackedData = chartData.map((d) => {
const entry = { name: d.name, slug: d.slug };
// Assign to first tag category
const cat = d.tags[0] || "other";
entry[cat] = d.lowestSell;
entry._cat = cat;
return entry;
});
// Get the categories actually used in stacked data
const usedCats = [...new Set(stackedData.map((d) => d._cat))];
return (
<div className="chart-wrap">
<h3>Lowest Sell Price per Watchlisted Item</h3>
<ResponsiveContainer width="100%" height={400}>
<BarChart data={stackedData} margin={{ top: 10, right: 30, left: 0, bottom: 60 }}>
<CartesianGrid strokeDasharray="3 3" stroke="#30363d" />
<XAxis
dataKey="name"
tick={{ fill: "#8b949e", fontSize: 11 }}
angle={-45}
textAnchor="end"
height={80}
/>
<YAxis tick={{ fill: "#8b949e" }} />
<Tooltip
contentStyle={{
background: "#161b22",
border: "1px solid #30363d",
borderRadius: 8,
color: "#e6edf3",
}}
/>
<Legend wrapperStyle={{ color: "#8b949e" }} />
{usedCats.map((cat) => (
<Bar
key={cat}
dataKey={cat}
fill={colorMap[cat]}
stackId="stack"
name={cat}
radius={[4, 4, 0, 0]}
/>
))}
</BarChart>
</ResponsiveContainer>
</div>
);
}
|