/* global window, React */
// ============================================================
// Strategic Horizon — 5-day forward view
// Sections:
// 1. Header + KPI strip
// 2. Lane × Day capacity grid (the centerpiece)
// 3. Pre-tender opportunities + Cost variance attribution
// 4. Stale orders panel
// ============================================================
const { useState: useStratState } = React;
// ---------- helpers ----------
function cellState(cell) {
const gap = cell.loads - cell.cap;
if (cell.loads === 0) return 'empty';
if (gap > 0) return 'red';
if (gap === 0) return 'amber';
if (cell.cap - cell.loads === 1) return 'tight';
return 'ok';
}
function fmt$(n) {
const abs = Math.abs(n);
if (abs >= 1000) return (n < 0 ? '−$' : '$') + (abs / 1000).toFixed(abs >= 10000 ? 0 : 1) + 'K';
return (n < 0 ? '−$' : '$') + abs.toLocaleString();
}
// ---------- KPI cards (strategic-tinted, slightly different from overview) ----------
function StratKpi({ label, value, sub, kind, trend }) {
return (
{label}
{value}
{trend && {trend.label} }
{sub &&
{sub}
}
);
}
// ---------- Capacity grid cell ----------
function CapacityCell({ cell, onClick, isSelected }) {
const st = cellState(cell);
const gap = cell.loads - cell.cap;
const covered = cell.priv + cell.ded + cell.mkt;
const total = Math.max(cell.loads, cell.cap, 1);
const pPriv = (cell.priv / total) * 100;
const pDed = (cell.ded / total) * 100;
const pMkt = (cell.mkt / total) * 100;
const pUnc = (cell.uncovered / total) * 100;
const pCapHeadroom = (Math.max(0, cell.cap - covered) / total) * 100;
return (
{cell.loads}
/
{cell.cap}
{gap > 0 && −{gap} }
{cell.priv > 0 && }
{cell.ded > 0 && }
{cell.mkt > 0 && }
{cell.uncovered > 0 && }
{pCapHeadroom > 0 && }
);
}
// ---------- Detail panel that appears when a cell is selected ----------
function CellDetail({ lane, dayIdx, days, cell, onClose, onJumpPreTender }) {
const day = days[dayIdx];
const gap = cell.loads - cell.cap;
return (
{day.dow} {day.date} · {lane.from} → {lane.to}
{cell.loads} loads · {cell.cap} capacity
{gap > 0 && −{gap} uncovered }
×
Private
{cell.priv}
Dedicated
{cell.ded}
Market tendered
{cell.mkt}
Uncovered
0 ? 'var(--red-2)' : 'var(--ink-3)' }}>{cell.uncovered}
{gap > 0 ? (
AGENT
Pre-tender recommended. Spot premium trending up — locking now saves vs forecast.
See pre-tender →
) : (
STATUS
Capacity is in balance. {cell.cap - cell.loads} unit{cell.cap - cell.loads === 1 ? '' : 's'} of slack.
)}
);
}
// ---------- Pre-tender card ----------
function PreTenderCard({ rec, onAction }) {
return (
{rec.uncovered} uncovered
{rec.lane}
{rec.day}
flagged {rec.surfaced}
{rec.title}
Lane plan
${rec.math.plan.toLocaleString()}
→
Lock today
${rec.math.lockNow.toLocaleString()}
vs
Wait → spot
${rec.math.waitSpot.toLocaleString()}
Net savings
${rec.math.savings.toLocaleString()}
onAction(rec, 'primary')}>{rec.action}
onAction(rec, 'alt')}>{rec.altAction}
);
}
// ---------- Cost variance attribution ----------
function CostVariance({ data }) {
const max = Math.max(...data.drivers.map(d => Math.abs(d.amount)));
return (
Plan
${(data.plan / 1000).toFixed(0)}K
+
Variance
+${(data.variance / 1000).toFixed(1)}K
+{data.pct.toFixed(1)}%
=
Actual
${(data.actual / 1000).toFixed(0)}K
{data.drivers.map(d => {
const pct = (Math.abs(d.amount) / max) * 100;
const isSave = d.amount < 0;
return (
{d.label}
{isSave ? '−' : '+'}${(Math.abs(d.amount) / 1000).toFixed(d.amount < 1500 && d.amount > -1500 ? 1 : 1)}K
{d.detail}
);
})}
Next 5d forecast
${(data.forecast.fiveDay / 1000).toFixed(0)}K
plan ${(data.forecast.fiveDayPlan / 1000).toFixed(0)}K · +{(((data.forecast.fiveDay - data.forecast.fiveDayPlan) / data.forecast.fiveDayPlan) * 100).toFixed(1)}%
YTD vs plan
${(data.forecast.ytd / 1000000).toFixed(2)}M
+{(((data.forecast.ytd - data.forecast.ytdPlan) / data.forecast.ytdPlan) * 100).toFixed(1)}% over plan
);
}
// ---------- Stale orders panel ----------
function StaleOrders({ orders, onAction }) {
return (
Order
Customer
Destination
Ship
Days out
Pallets
In pool
Reason
Agent suggestion
{orders.map(o => (
{o.id}
{o.customer}
{o.dest}
{o.ship}
{o.daysOut.toFixed(1)}d
{o.pal}{o.lbs.toLocaleString()} lb
{o.inPool}
{o.reason}
TP
{o.suggest}
onAction(o)}>Action →
))}
);
}
// ---------- Main view ----------
function StrategicHorizon() {
const S = window.STRATEGIC;
const [selectedCell, setSelectedCell] = useStratState(null); // { laneId, dayIdx }
const [range, setRange] = useStratState('5d');
const [toast, setToast] = useStratState(null);
const showToast = (msg) => {
setToast(msg);
setTimeout(() => setToast(null), 3200);
};
const totals = S.days.map((_, di) =>
S.lanes.reduce((acc, lane) => {
const c = lane.cells[di];
acc.loads += c.loads;
acc.cap += c.cap;
acc.unc += c.uncovered;
return acc;
}, { loads: 0, cap: 0, unc: 0 })
);
const totalUncovered = totals.reduce((a, t) => a + t.unc, 0);
const totalLoads = totals.reduce((a, t) => a + t.loads, 0);
const totalCap = totals.reduce((a, t) => a + t.cap, 0);
const totalGapLanes = S.lanes.filter(l => l.cells.some(c => c.loads > c.cap)).length;
const selectedLane = selectedCell ? S.lanes.find(l => l.id === selectedCell.laneId) : null;
const selectedCellData = selectedLane ? selectedLane.cells[selectedCell.dayIdx] : null;
const onCellClick = (laneId, dayIdx) => {
if (selectedCell && selectedCell.laneId === laneId && selectedCell.dayIdx === dayIdx) {
setSelectedCell(null);
} else {
setSelectedCell({ laneId, dayIdx });
}
};
const onPreTenderAction = (rec, kind) => {
if (kind === 'primary') {
showToast(`✓ Pre-tender executed · ${rec.uncovered} loads locked on ${rec.lane} (${rec.day}) · est. savings $${rec.math.savings.toLocaleString()}`);
} else {
showToast(`Alternatives opened for ${rec.lane} · ${rec.day}.`);
}
};
const onStaleAction = (o) => {
showToast(`Routing ${o.id} → ${o.suggest}`);
};
return (
{/* Top bar */}
STRATEGIC HORIZON · 5-DAY
Forward plan · {S.rangeLabel}
8 uncovered loads across 2 lanes in the next 5 days ·
3 pre-tender moves identified · est. lock-now savings $3,135
{S.rangeOptions.map(r => (
setRange(r)}>{r}
))}
⤓ Export plan
showToast('✓ 3 pre-tender carriers locked · 8 loads secured · est. $3,135 saved')}>
Lock 3 pre-tender moves →
{/* KPI strip */}
{/* Capacity grid */}
Lane × day capacity grid
Private
Dedicated
Market
Uncovered
Headroom
Cells show loads / capacity . Red = gap. Click a cell to drill in.
Lane
{S.lanes.length} lanes · Chicago hub
{S.days.map((d, i) => (
{d.dow}
{d.date}
0 ? 's-grid__col-totals--bad' : ''}`}>
{totals[i].loads}/{totals[i].cap}
{totals[i].unc > 0 && −{totals[i].unc} }
))}
TOTAL
5-day
{totalLoads}/{totalCap}
{S.lanes.map(lane => {
const laneLoads = lane.cells.reduce((a, c) => a + c.loads, 0);
const laneCap = lane.cells.reduce((a, c) => a + c.cap, 0);
const laneUnc = lane.cells.reduce((a, c) => a + c.uncovered, 0);
return (
{lane.from}→ {lane.to}
{lane.miles.toLocaleString()} mi · {lane.mode}
{lane.cells.map((cell, di) => (
onCellClick(lane.id, di)}
/>
))}
0 ? 's-grid__row-total--bad' : ''}`}>
{laneLoads}/{laneCap}
{laneUnc > 0 && −{laneUnc} unc }
);
})}
{selectedLane && selectedCellData && (
setSelectedCell(null)}
onJumpPreTender={() => {
setSelectedCell(null);
const target = document.getElementById('s-pretender');
if (target) target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}}
/>
)}
{/* Two-column: pre-tender + cost variance */}
Pre-tender opportunities
Lock capacity now while contracted carriers have HOS — vs. waiting for spot to move.
3 agent-flagged
{S.preTender.map(rec => (
))}
Cost variance attribution
{S.costVariance.period}
+5.2%
{/* Stale orders */}
Stale orders · pool aging toward ship date
Orders without a trip assignment, ordered by days-to-ship.
{S.staleOrders.length} pending
{toast && (
{toast}
)}
);
}
Object.assign(window, { StrategicHorizon });