import React, { useState, useEffect } from 'react'; import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts'; const API_BASE = import.meta.env.VITE_API_URL || 'http://localhost:8000'; const STORES = [ { id: 'store_001', name: 'Patia Dark Store' }, { id: 'store_002', name: 'Infocity Hub' }, { id: 'store_003', name: 'Saheed Nagar Node' }, ]; export default function DarkStoreIntel() { const [forecast, setForecast] = useState(null); const [storeHealth, setStoreHealth] = useState(null); const [selectedStore, setSelectedStore] = useState('store_001'); const [loading, setLoading] = useState(true); const fetchData = async (storeId) => { setLoading(true); try { const [fcRes, healthRes] = await Promise.all([ fetch(`${API_BASE}/api/ml/demand-forecast?store_id=${storeId}`), fetch(`${API_BASE}/api/ml/store-health`), ]); setForecast(await fcRes.json()); setStoreHealth(await healthRes.json()); } catch { // Backend not running — show placeholder } setLoading(false); }; useEffect(() => { fetchData(selectedStore); }, [selectedStore]); const currentStore = storeHealth?.stores?.find(s => s.id === selectedStore); const chartData = forecast?.forecast?.map(f => ({ name: f.label, demand: f.predicted_units, lower: f.lower_ci, upper: f.upper_ci, isPeak: f.is_peak, })) || []; return (
{/* Header */}
Dark Store Intel
{forecast?.model || 'Heteroscedastic Tobit Regression (Type I Right-Censored)'} · Live demand forecasting
{STORES.map(s => ( ))}
{/* KPI row */}
Health Score
{loading ? '—' : currentStore?.health_score?.toFixed(1) ?? '—'}
/ 100 composite
Active Orders
{loading ? '—' : currentStore?.active_orders ?? '—'}
live queue
Avg Fill Time
{loading ? '—' : `${currentStore?.avg_fill_time_min ?? '—'}m`}
pick + pack
Model R²
{forecast?.model_rsq ?? '—'}
Tobit fit quality
{/* Stock health bars */} {currentStore && (
Stock Distribution
)} {/* Demand forecast chart */}
24-Hour Demand Forecast
Predicted units with 95% confidence interval · Peak hours highlighted
Model live
{loading ? (
Loading forecast...
) : ( )}
{/* All stores overview */} {storeHealth && (
All Stores
{storeHealth.stores.map(s => (
{s.name} {s.health_score} {s.active_orders} orders {s.in_stock_pct}% in stock
))}
)}
); } function StockBar({ label, pct, color }) { return (
{label}
{pct}%
); }