Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | 3x 28x 28x 23x 22x 17x 16x 14x 23x 4x 1x 5x 1x 2x 10x 23x 18x 16x 23x 22x 21x 18x 28x 26x 2x 1x 2x 1x 69x 46x 3x 28x 28x 28x 28x 5x 5x 23x 28x 28x 23x 23x 28x 28x 28x 28x 28x 14x 14x 14x 28x 28x 14x 14x 28x 28x 23x 23x 23x 23x 5x 28x 28x 3x | import React, { useState, useEffect, useRef } from "react";
import clsx from "clsx";
import type { SimulationStatus, ClientState } from "@shared/types/arduino.types";
import type { CompilationStatus } from "@/types/compilation.types";
import type { ConnectionState } from "@/lib/websocket-manager";
// ── Constants ─────────────────────────────────────────────────────────────────
/** Minimum ms any state label stays visible before switching to a new one. */
const STATE_MIN_MS = 600;
// ── Pure helpers ──────────────────────────────────────────────────────────────
function deriveClientState(
simulationStatus: SimulationStatus,
compilationStatus: CompilationStatus,
pendingExternalStart: boolean = false,
): ClientState {
// pendingExternalStart means START_SIMULATION was received before the WS
// connected (or before the backend was reachable). The instance is waiting
// for the compile + WS handshake phase — NOT for a simulation runner slot.
// Without this check, simulationStatus === "queued" (set client-side by
// handleExternalStartSimulation) would make the badge show QUEUED_FOR_SIMULATION
// while the instance is actually queued for compilation.
Iif (pendingExternalStart) return "QUEUED_FOR_COMPILING";
if (compilationStatus === "compiling") return "COMPILING";
if (simulationStatus === "queued") return "QUEUED_FOR_SIMULATION";
if (simulationStatus === "running") return "RUNNING";
if (simulationStatus === "paused") return "PAUSED";
if (compilationStatus === "error") return "ERROR";
return "IDLE";
}
function clientStateColor(state: ClientState): string {
switch (state) {
case "RUNNING": return "text-emerald-400";
case "RUNNING_STARTING": return "text-orange-400";
case "PAUSED": return "text-amber-300";
case "COMPILING":
case "QUEUED_FOR_COMPILING": return "text-blue-300";
case "QUEUED_FOR_SIMULATION": return "text-violet-300";
case "ERROR": return "text-red-400";
default: return "text-white/50";
}
}
function compileDotClass(status: CompilationStatus): string {
if (status === "compiling") return "bg-blue-400 animate-pulse";
if (status === "error") return "bg-red-500";
return "bg-white/30";
}
/**
* WS dot — based on wsConnectionState only (not simulation telemetry).
* gray = never connected | amber(pulse) = connecting | green = connected | red = connection lost
*/
function wsDotClass(wsState: ConnectionState, hasEverConnected: boolean): string {
if (wsState === "connected") return "bg-emerald-400";
if (wsState === "connecting" || wsState === "reconnecting") return "bg-amber-400 animate-pulse";
if (hasEverConnected) return "bg-red-500";
return "bg-white/30";
}
/** True when WS previously connected but is now disconnected/lost. */
function isWsError(wsState: ConnectionState, hasEverConnected: boolean): boolean {
if (wsState === "connected" || wsState === "connecting" || wsState === "reconnecting") return false;
return hasEverConnected;
}
function simulationModeLabel(sandboxMode: string): string {
if (sandboxMode === "docker-sandbox") return "DOCKER";
Eif (sandboxMode === "local-limited") return "LOCAL";
return "—";
}
function simulationModeColorClass(sandboxMode: string): string {
if (sandboxMode === "docker-sandbox") return "text-cyan-300";
Eif (sandboxMode === "local-limited") return "text-amber-300";
return "text-white/40";
}
// ── Sub-components ────────────────────────────────────────────────────────────
interface StatCellProps {
readonly label: string;
readonly value: React.ReactNode;
readonly valueClass?: string;
}
/** A compact 2-row stat cell: dim label on top, bright value below. */
function StatCell({ label, value, valueClass }: StatCellProps) {
return (
<div className="flex flex-col items-start leading-tight">
<span
className="uppercase tracking-wider text-cyan-500/50 whitespace-nowrap"
style={{ fontSize: "calc(9px * var(--ui-font-scale))" }}
>
{label}
</span>
<span
className={clsx("font-bold font-mono whitespace-nowrap", valueClass ?? "text-white/50")}
style={{ fontSize: "calc(11px * var(--ui-font-scale))" }}
>
{value}
</span>
</div>
);
}
function ColSep() {
return <div className="w-px h-5 bg-white/10 self-center mx-0.5 shrink-0" />;
}
// ── Component interface ───────────────────────────────────────────────────────
interface SimCockpitProps {
batchStats?: unknown;
simulationStatus?: SimulationStatus;
compilationStatus?: CompilationStatus;
sandboxMode?: string;
workerIndex?: number;
workerTotal?: number;
backendReachable?: boolean;
isConnected?: boolean;
wsConnectionState?: ConnectionState;
wsHasEverConnected?: boolean;
baudRate?: number;
debugMode?: boolean;
/** When true, a START_SIMULATION arrived before the WS connected; the instance
* is waiting for compilation, not for a simulation runner slot. Without this
* flag deriveClientState incorrectly shows QUEUED_FOR_SIMULATION instead of
* QUEUED_FOR_COMPILING because simulationStatus is set to "queued" client-side
* by handleExternalStartSimulation. */
pendingExternalStart?: boolean;
/** @deprecated kept for prop compatibility; no longer used for WS dot logic */
serverStatus?: unknown;
}
export const SimCockpit: React.FC<SimCockpitProps> = React.memo(({
simulationStatus = "idle",
compilationStatus = "ready",
sandboxMode = "unknown",
backendReachable = true,
wsConnectionState = "disconnected",
wsHasEverConnected = false,
workerIndex,
workerTotal,
debugMode = false,
pendingExternalStart = false,
}) => {
// ── Compilation dot visual delay ───────────────────────────────────────
// Keep the blue dot visible for at least STATE_MIN_MS even on fast compiles.
const [visualCompStatus, setVisualCompStatus] = useState<CompilationStatus>(compilationStatus);
const httpTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => {
if (compilationStatus === "compiling") {
Iif (httpTimerRef.current) clearTimeout(httpTimerRef.current);
setVisualCompStatus("compiling");
} else {
httpTimerRef.current = setTimeout(() => {
setVisualCompStatus(compilationStatus);
}, STATE_MIN_MS);
}
return () => {
if (httpTimerRef.current) {
clearTimeout(httpTimerRef.current);
httpTimerRef.current = null;
}
};
}, [compilationStatus]);
// ── Client state visual delay ──────────────────────────────────────────
// Show active states immediately; delay the downgrade back to IDLE so it
// stays readable for at least STATE_MIN_MS.
const clientState = deriveClientState(simulationStatus, compilationStatus, pendingExternalStart);
const [visualClientState, setVisualClientState] = useState<ClientState>(clientState);
const clientStateTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => {
if (clientState === "IDLE") {
clientStateTimerRef.current = setTimeout(() => {
setVisualClientState(clientState);
}, STATE_MIN_MS);
} else {
Iif (clientStateTimerRef.current) clearTimeout(clientStateTimerRef.current);
setVisualClientState(clientState);
}
return () => {
if (clientStateTimerRef.current) {
clearTimeout(clientStateTimerRef.current);
clientStateTimerRef.current = null;
}
};
}, [clientState]);
const wsError = isWsError(wsConnectionState, wsHasEverConnected);
// ── Debug mode: 3-group status row ───────────────────────────────────
if (debugMode) {
// Compile slot: only visible while a compilation is in progress.
const compileSlotVal = visualCompStatus === "compiling"
&& !wsError
&& workerIndex !== undefined
&& workerTotal !== undefined
? `#${workerIndex + 1}/${workerTotal}`
: null;
// Simulation runner: only visible while simulation is active (running/paused/queued).
const simActive = simulationStatus === "running" || simulationStatus === "paused" || simulationStatus === "queued";
const simSlotVal = simActive
&& !wsError
&& workerIndex !== undefined
&& workerTotal !== undefined
? `#${workerIndex + 1}/${workerTotal}`
: null;
return (
<div
className="hidden lg:flex items-center gap-2 text-[10px] font-medium"
data-testid="sim-cockpit-debug"
>
{/* GROUP 1: CLIENT state */}
<StatCell
label="CLIENT"
value={<span data-testid="client-state-badge">{visualClientState}</span>}
valueClass={clientStateColor(visualClientState)}
/>
<ColSep />
{/* GROUP 2: COMPILATION — HTTP dot + slot (slot only while compiling) */}
<StatCell
label="COMPILATION"
value={(
<span className="flex items-center gap-1">
<span className="text-white/50">HTTP:</span>
<span className={clsx("inline-block w-2 h-2 rounded-full", compileDotClass(visualCompStatus))} />
{compileSlotVal && (
<>
<span className="text-white/30">|</span>
<span className="text-white/50">SLOT:</span>
<span className="text-white/50">{compileSlotVal}</span>
</>
)}
</span>
)}
/>
<ColSep />
{/* GROUP 3: SIMULATION — WS dot + mode + runner (mode/runner only while active) */}
<StatCell
label="SIMULATION"
value={(
<span className="flex items-center gap-1">
<span className="text-white/50">WS:</span>
<span className={clsx("inline-block w-2 h-2 rounded-full", wsDotClass(wsConnectionState, wsHasEverConnected))} />
{!wsError && simSlotVal && (
<>
<span className="text-white/30">|</span>
<span className={clsx("font-bold font-mono whitespace-nowrap", simulationModeColorClass(sandboxMode))}>
{simulationModeLabel(sandboxMode)}
</span>
<span className="text-white/30">|</span>
<span className="text-cyan-300 font-bold font-mono whitespace-nowrap">{simSlotVal}</span>
</>
)}
</span>
)}
/>
</div>
);
}
// ── Normal mode: minimal SERVER/OFFLINE pill ──────────────────────────
const httpDotClass = backendReachable ? "bg-emerald-500" : "bg-red-600";
const httpTextClass = backendReachable ? "text-emerald-400" : "text-red-400";
return (
<div className="hidden lg:flex items-center gap-2 bg-black/20 backdrop-blur-md border border-white/10 rounded-lg px-3 py-1.5 text-[10px] uppercase tracking-wider font-medium shadow-2xl">
<div className="relative flex h-2.5 w-2.5">
{backendReachable && (
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75" />
)}
<span className={clsx("relative inline-flex rounded-full h-2.5 w-2.5", httpDotClass)} />
</div>
<span className={clsx("text-[9px] font-bold", httpTextClass)}>
{backendReachable ? "SERVER" : "OFFLINE"}
</span>
</div>
);
});
SimCockpit.displayName = "SimCockpit";
|