@burdenoff/microfe-movethewheels 2026.1107.4 → 2026.1107.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,273 @@
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+
3
+ import { useMoveTheWheels } from '../providers/MoveTheWheelsProvider';
4
+ import { executeGraphQL } from '../utils/graphqlClient';
5
+
6
+ /**
7
+ * Scenario planning (S&OP) and capacity vs demand (BOFF-10761). Runs the
8
+ * read-only `demandScenarioComparison` query: the baseline lane forecast and
9
+ * up to four what-if scenarios, each compared against fleet capacity.
10
+ */
11
+
12
+ export type CapacitySource = 'OBSERVED' | 'OVERRIDE' | 'UNKNOWN';
13
+ export type LaneShareBasis = 'VEHICLE_HISTORY' | 'DEMAND_SHARE' | 'NONE';
14
+
15
+ export interface ScenarioLaneAdjustment {
16
+ laneKey: string;
17
+ demandChangePct: number;
18
+ }
19
+
20
+ export interface ScenarioDraft {
21
+ name: string;
22
+ demandChangePct: number;
23
+ capacityChangePct: number;
24
+ closedLanes: string[];
25
+ laneAdjustments: ScenarioLaneAdjustment[];
26
+ }
27
+
28
+ export interface ScenarioComparisonRequest {
29
+ weeks: number;
30
+ jobsPerVehiclePerWeek: number | null;
31
+ scenarios: ScenarioDraft[];
32
+ }
33
+
34
+ export interface CapacityBasis {
35
+ activeVehicles: number;
36
+ jobsPerVehiclePerWeek: number | null;
37
+ source: CapacitySource;
38
+ weeklyFleetCapacity: number | null;
39
+ laneShareBasis: LaneShareBasis;
40
+ observedVehicles: number;
41
+ }
42
+
43
+ export interface ScenarioWeekResult {
44
+ periodStart: string;
45
+ periodEnd: string;
46
+ baselineVolume: number;
47
+ scenarioVolume: number;
48
+ baselineRevenue: number;
49
+ scenarioRevenue: number;
50
+ capacity: number | null;
51
+ shortfall: number;
52
+ }
53
+
54
+ export interface ScenarioLaneResult {
55
+ laneKey: string;
56
+ laneLabel: string;
57
+ closed: boolean;
58
+ demandChangePct: number;
59
+ baselineVolume: number;
60
+ scenarioVolume: number;
61
+ baselineRevenue: number;
62
+ scenarioRevenue: number;
63
+ lostVolume: number;
64
+ lostRevenue: number;
65
+ capacity: number | null;
66
+ shortfall: number;
67
+ flagged: boolean;
68
+ }
69
+
70
+ export interface ScenarioResult {
71
+ name: string;
72
+ demandChangePct: number;
73
+ capacityChangePct: number;
74
+ closedLanes: string[];
75
+ unknownLanes: string[];
76
+ totalVolume: number;
77
+ totalRevenue: number;
78
+ volumeDelta: number;
79
+ revenueDelta: number;
80
+ lostVolume: number;
81
+ lostRevenue: number;
82
+ totalCapacity: number | null;
83
+ totalShortfall: number;
84
+ utilization: number | null;
85
+ shortfallLanes: number;
86
+ weeks: ScenarioWeekResult[];
87
+ lanes: ScenarioLaneResult[];
88
+ }
89
+
90
+ export interface ScenarioComparison {
91
+ generatedAt: string;
92
+ weeks: number;
93
+ capacity: CapacityBasis;
94
+ baseline: ScenarioResult;
95
+ scenarios: ScenarioResult[];
96
+ }
97
+
98
+ const RESULT_FIELDS = `
99
+ name
100
+ demandChangePct
101
+ capacityChangePct
102
+ closedLanes
103
+ unknownLanes
104
+ totalVolume
105
+ totalRevenue
106
+ volumeDelta
107
+ revenueDelta
108
+ lostVolume
109
+ lostRevenue
110
+ totalCapacity
111
+ totalShortfall
112
+ utilization
113
+ shortfallLanes
114
+ weeks {
115
+ periodStart
116
+ periodEnd
117
+ baselineVolume
118
+ scenarioVolume
119
+ baselineRevenue
120
+ scenarioRevenue
121
+ capacity
122
+ shortfall
123
+ }
124
+ lanes {
125
+ laneKey
126
+ laneLabel
127
+ closed
128
+ demandChangePct
129
+ baselineVolume
130
+ scenarioVolume
131
+ baselineRevenue
132
+ scenarioRevenue
133
+ lostVolume
134
+ lostRevenue
135
+ capacity
136
+ shortfall
137
+ flagged
138
+ }
139
+ `;
140
+
141
+ const SCENARIO_QUERY = `
142
+ query GetMovethewheelsDemandScenarioComparison($input: MtwDemandScenarioComparisonInput) {
143
+ demandScenarioComparison(input: $input) {
144
+ generatedAt
145
+ weeks
146
+ capacity {
147
+ activeVehicles
148
+ jobsPerVehiclePerWeek
149
+ source
150
+ weeklyFleetCapacity
151
+ laneShareBasis
152
+ observedVehicles
153
+ }
154
+ baseline { ${RESULT_FIELDS} }
155
+ scenarios { ${RESULT_FIELDS} }
156
+ }
157
+ }
158
+ `;
159
+
160
+ /** The scenarios a workspace sees before editing anything. */
161
+ export const DEFAULT_SCENARIOS: ScenarioDraft[] = [
162
+ {
163
+ name: 'Peak season',
164
+ demandChangePct: 25,
165
+ capacityChangePct: 0,
166
+ closedLanes: [],
167
+ laneAdjustments: [],
168
+ },
169
+ {
170
+ name: 'Peak + 20% fleet',
171
+ demandChangePct: 25,
172
+ capacityChangePct: 20,
173
+ closedLanes: [],
174
+ laneAdjustments: [],
175
+ },
176
+ ];
177
+
178
+ export const DEFAULT_REQUEST: ScenarioComparisonRequest = {
179
+ weeks: 4,
180
+ jobsPerVehiclePerWeek: null,
181
+ scenarios: DEFAULT_SCENARIOS,
182
+ };
183
+
184
+ interface UseScenarioPlanningResult {
185
+ comparison: ScenarioComparison | null;
186
+ isLoading: boolean;
187
+ error: Error | null;
188
+ /** Run a comparison; the latest request wins if several overlap. */
189
+ run: (request: ScenarioComparisonRequest) => void;
190
+ }
191
+
192
+ export function useScenarioPlanning(
193
+ initial: ScenarioComparisonRequest = DEFAULT_REQUEST
194
+ ): UseScenarioPlanningResult {
195
+ const { apiGatewayUrl, authToken, workspaceToken, workspaceId, tenantId, currentUser } =
196
+ useMoveTheWheels();
197
+ const [comparison, setComparison] = useState<ScenarioComparison | null>(null);
198
+ const [isLoading, setIsLoading] = useState(false);
199
+ const [error, setError] = useState<Error | null>(null);
200
+ const latest = useRef(0);
201
+ // The request to re-run when the auth context changes (a token refresh
202
+ // gives `load` a new identity): the last one the user ran, not the default,
203
+ // so a refresh never replaces their comparison with the initial scenarios.
204
+ const lastRequest = useRef(initial);
205
+
206
+ const graphQLOptions = useMemo(
207
+ () => ({
208
+ apiGatewayUrl,
209
+ authToken,
210
+ workspaceToken,
211
+ workspaceId,
212
+ tenantId,
213
+ actorId: currentUser?.id,
214
+ }),
215
+ [apiGatewayUrl, authToken, workspaceToken, workspaceId, tenantId, currentUser?.id]
216
+ );
217
+
218
+ const load = useCallback(
219
+ async (request: ScenarioComparisonRequest) => {
220
+ lastRequest.current = request;
221
+ const id = latest.current + 1;
222
+ latest.current = id;
223
+ setIsLoading(true);
224
+ setError(null);
225
+ try {
226
+ const data = await executeGraphQL<{ demandScenarioComparison: ScenarioComparison }>(
227
+ graphQLOptions,
228
+ SCENARIO_QUERY,
229
+ {
230
+ input: {
231
+ weeks: request.weeks,
232
+ jobsPerVehiclePerWeek: request.jobsPerVehiclePerWeek,
233
+ scenarios: request.scenarios.map((scenario) => ({
234
+ name: scenario.name.trim(),
235
+ demandChangePct: scenario.demandChangePct,
236
+ capacityChangePct: scenario.capacityChangePct,
237
+ closedLanes: scenario.closedLanes,
238
+ laneAdjustments: scenario.laneAdjustments,
239
+ })),
240
+ },
241
+ }
242
+ );
243
+ if (latest.current === id) {
244
+ setComparison(data.demandScenarioComparison);
245
+ }
246
+ } catch (fetchError) {
247
+ if (latest.current === id) {
248
+ setError(
249
+ fetchError instanceof Error ? fetchError : new Error('Failed to compare scenarios')
250
+ );
251
+ }
252
+ } finally {
253
+ if (latest.current === id) {
254
+ setIsLoading(false);
255
+ }
256
+ }
257
+ },
258
+ [graphQLOptions]
259
+ );
260
+
261
+ useEffect(() => {
262
+ void load(lastRequest.current);
263
+ }, [load]);
264
+
265
+ const run = useCallback(
266
+ (request: ScenarioComparisonRequest) => {
267
+ void load(request);
268
+ },
269
+ [load]
270
+ );
271
+
272
+ return { comparison, isLoading, error, run };
273
+ }
@@ -13,6 +13,7 @@ import { MetricCard, MetricGrid, MetricCardSkeleton } from '../components/Metric
13
13
  import { Card, CardContent, CardHeader, CardTitle, Progress, Button } from '../components/ui';
14
14
  import { TrendChart, VolumeBar, StatusDonut, type DonutSlice } from '../components/charts';
15
15
  import { UpcomingDemandPanel } from '../components/UpcomingDemandPanel';
16
+ import { ScenarioPlanningPanel } from '../components/ScenarioPlanningPanel';
16
17
  import { CarbonFootprintPanel } from '../components/CarbonFootprintPanel';
17
18
  import { EmphasisPanel, PagePurpose } from '@burdenoff/fe-libs/ui';
18
19
  import { useAnalytics, useOrderAnalytics, useFleetAnalytics } from '../hooks/useAnalytics';
@@ -153,6 +154,10 @@ export default function AnalyticsPage() {
153
154
  onRetry={refetchForecast}
154
155
  />
155
156
 
157
+ {/* Scenario planning and capacity vs demand (BOFF-10761) — what-if
158
+ scenarios on the lane forecast compared against fleet capacity. */}
159
+ <ScenarioPlanningPanel />
160
+
156
161
  <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
157
162
  <Card>
158
163
  <CardHeader>