Loading files
05241c15dThis file is pinned to the selected release. Download the original to open it in your design tool.
"use client";
import { ArrowKey } from "@/components/cabinet/ArrowKey";
import { useTeleop } from "@/components/control/TeleopProvider";
import { useSystemStatus } from "@/components/control/SystemStatusProvider";
import { STATUS_TONE_TEXT } from "@/components/arcade/StatusMarquee";
import { QUEUE_TURN_SECONDS } from "@/lib/config";
/** Human ETA for a wait in seconds — "now" / "~45s" / "~3m". */
function etaFmt(s: number): string {
if (s <= 5) return "now";
if (s < 60) return `~${Math.round(s / 5) * 5}s`;
return `~${Math.round(s / 60)}m`;
}
// The ETA math assumes an average turn is HALF of the configured max; most operators leave before
// timing out, so a full-turn estimate over-forecasts. Example: 4 in queue at a 2-min max ⇒ ~4 min wait.
const AVG_TURN_SECONDS = QUEUE_TURN_SECONDS / 2;
// The generic info panel — used by every non-driving state (JOIN, QUEUED, SPECTATING, DEMO, OPEN
// ELSEWHERE, blocked). Same rounded shell as the driving cluster with generous padding + large text
// so every state reads with the same visual weight.
const PANEL =
"pointer-events-auto flex max-w-[calc(100vw-1.5rem)] flex-wrap items-center justify-center gap-x-5 gap-y-3 rounded-2xl border border-cab-600/70 bg-cab-900/70 px-6 py-4 font-pixel backdrop-blur-md";
// The driving cluster is a single flex ROW at every size — Move | Lift | [Gripper stacked over END
// CONTROL] on mobile (3 columns, tight gaps so it fits one row + frees vertical space), and
// Move | Lift | Gripper | END on desktop (the Gripper+END wrapper is `sm:contents`, so it dissolves
// and those two become separate columns again). `items-end` lines up the lowest keys across columns.
const DRIVE_SHELL =
"pointer-events-auto flex w-full items-end justify-around gap-x-1 rounded-2xl border border-cab-600/70 bg-cab-900/70 px-1 py-2 font-pixel backdrop-blur-md sm:w-fit sm:justify-center sm:gap-x-8 sm:px-4 sm:py-3";
// heading above each control group ("Move" / "Gripper" / "Lift")
const GROUP_LABEL = "font-pixel text-[9px] tracking-wide text-neon-cyan/70";
// Sub-caption under every state's headline ("set LIVEKIT_* env…", "40s until your turn", etc.). White
// at 75% for readable contrast against the dark panel; the theme's cab-500 was near-invisible.
const CAPTION = "font-pixel text-center text-[10px] text-white/75 sm:text-xs";
// Shared shape of every state's headline — 2xl on mobile, 4xl on desktop, centered so wrapped lines
// align nicely (colors differ by state so they're appended per-branch).
const HEADLINE_BASE = "font-pixel text-center text-2xl neon-text sm:text-4xl";
// A red LEAVE / RESUME / etc. keycap — matches the driving cluster's action key so every action across
// every state reads at the same size. flex-col + gap accommodates two-line labels like "END / CONTROL"
// and "LEAVE / QUEUE" arcade-style.
const ACTION_KEYCAP =
"touch-control flex h-12 w-24 flex-col items-center justify-center gap-0.5 rounded-lg border-2 font-pixel text-[10px] leading-none neon-box transition-transform active:translate-y-0.5 disabled:opacity-40 sm:h-14 sm:w-28 sm:text-xs";
const LEAVE_KEYCAP = `${ACTION_KEYCAP} border-neon-red bg-neon-red/10 text-neon-red`;
/**
* The cabinet's bottom control bar — a thin row along the bottom that shows only what the current
* state needs: JOIN QUEUE while viewing, queue status + LEAVE while queued, and the compact teleop
* cluster (joystick + grip/Z buttons + timer + LEAVE) only while you actually hold an arm.
*/
export function ControlBar() {
const {
phase, controlling, queuePosition, queueCount, session,
join, leave, setJog, setZ, setGrip, canDrive, unconfigured,
} = useTeleop();
const st = useSystemStatus();
// arm availability + how soon an arm frees up, for the CTA label and the queue ETA. An arm counts as
// free to TAKE only if it has no driver AND is online — an offline/faulted arm also has no driver, but
// you can't drive it, so it must not flip the CTA to "CONTROL ARM" (online omitted ⇒ treat as online).
const arms = session ? Object.values(session.arms) : [];
const freeArm = arms.some((a) => !a.driver && a.online !== false);
const occupiedRemaining = arms
.filter((a) => a.driver && typeof a.remaining_s === "number")
.map((a) => a.remaining_s as number);
const soonestFreeS = freeArm || occupiedRemaining.length === 0 ? 0 : Math.min(...occupiedRemaining);
// any non-LIVE status pauses driving + the queue; the bridge parks the active arm, so here we just
// reflect it: disable the inputs and show the status instead of a CTA the user can't act on.
const blocked = !st.drivable;
const tone = STATUS_TONE_TEXT[st.tone];
let content: React.ReactNode;
if (unconfigured) {
content = (
<div className={PANEL}>
<div className="flex flex-col items-center gap-1 leading-none">
<span className={`${HEADLINE_BASE} text-neon-amber blink`}>DEMO MODE</span>
<span className={CAPTION}>set LIVEKIT_* env to go live</span>
</div>
</div>
);
} else if (!canDrive) {
content = (
<div className={PANEL}>
<div className="flex flex-col items-center gap-1 leading-none">
<span className={`${HEADLINE_BASE} text-neon-magenta`}>SPECTATING</span>
<span className={CAPTION}>read-only — can't drive</span>
</div>
</div>
);
} else if (controlling) {
content = (
<div className={DRIVE_SHELL}>
{/* Move — 4-way jog, laid out as a compact inverted-T arrow-key cluster */}
<div className="flex flex-col items-center gap-1.5">
<span className={GROUP_LABEL}>Move</span>
<div className="grid grid-cols-3 gap-1">
<span />
<ArrowKey glyph="up" accent="cyan" ariaLabel="Move forward" label="Fwd" disabled={blocked} onPress={() => setJog(0, -1)} onRelease={() => setJog(0, 0)} />
<span />
<ArrowKey glyph="left" accent="cyan" ariaLabel="Move left" label="Left" disabled={blocked} onPress={() => setJog(-1, 0)} onRelease={() => setJog(0, 0)} />
<ArrowKey glyph="down" accent="cyan" ariaLabel="Move back" label="Back" disabled={blocked} onPress={() => setJog(0, 1)} onRelease={() => setJog(0, 0)} />
<ArrowKey glyph="right" accent="cyan" ariaLabel="Move right" label="Right" disabled={blocked} onPress={() => setJog(1, 0)} onRelease={() => setJog(0, 0)} />
</div>
</div>
{/* Lift — up / down (Z axis), stacked so up sits above down */}
<div className="flex flex-col items-center gap-1.5">
<span className={GROUP_LABEL}>Lift</span>
<div className="flex flex-col items-center gap-1">
<ArrowKey glyph="up" accent="amber" ariaLabel="Lift up" label="Up" disabled={blocked} onPress={() => setZ(1)} onRelease={() => setZ(0)} />
<ArrowKey glyph="down" accent="purple" ariaLabel="Lift down" label="Down" disabled={blocked} onPress={() => setZ(-1)} onRelease={() => setZ(0)} />
</div>
</div>
{/* Gripper + END CONTROL — one stacked column on mobile (frees a row); `sm:contents` dissolves
this wrapper on desktop so they become two separate columns as before. */}
<div className="flex flex-col items-center gap-2 sm:contents">
{/* Gripper — open (arrows apart) / close (arrows together) */}
<div className="flex flex-col items-center gap-1.5">
<span className={GROUP_LABEL}>Gripper</span>
<div className="flex items-start gap-2">
<ArrowKey glyph="open" accent="green" ariaLabel="Open gripper" label="Open" disabled={blocked} onPress={() => setGrip(1)} onRelease={() => setGrip(0)} />
<ArrowKey glyph="close" accent="red" ariaLabel="Close gripper" label="Close" disabled={blocked} onPress={() => setGrip(-1)} onRelease={() => setGrip(0)} />
</div>
</div>
{/* END CONTROL — the empty label spacer shows only on desktop, where it aligns the button with
the Gripper keys; on mobile it sits directly under the Gripper (no spacer). */}
<div className="flex flex-col items-center gap-1.5">
<span className={`${GROUP_LABEL} hidden sm:block`} aria-hidden="true"> </span>
<button
onClick={leave}
disabled={st.isDisabled("leave")}
aria-label="End control and release the arm"
className={LEAVE_KEYCAP}
>
<span>END</span>
<span>CONTROL</span>
</button>
</div>
</div>
</div>
);
} else if (blocked) {
// viewing or queued while the system is paused/degraded — show the big status message; LEAVE
// only when queued (so you can back out without waiting for recovery)
content = (
<div className={PANEL}>
<div className="flex flex-col items-center gap-1 leading-none">
<span className={`${HEADLINE_BASE} ${tone}`}>{st.label}</span>
<span className={CAPTION}>{st.caption}</span>
</div>
{phase === "queueing" && (
<button
onClick={leave}
disabled={st.isDisabled("leave")}
aria-label="Leave the queue"
className={LEAVE_KEYCAP}
>
<span>LEAVE</span>
<span>QUEUE</span>
</button>
)}
</div>
);
} else if (phase === "queueing") {
const eta = soonestFreeS + Math.max(0, queuePosition - 1) * AVG_TURN_SECONDS;
// "YOU'RE NEXT" for P1, "IN QUEUE" + big #N focal point for later positions
content = (
<div className={PANEL}>
<div className="flex flex-col items-center gap-1 leading-none">
<span className={`${HEADLINE_BASE} text-neon-amber`}>
{queuePosition === 1 ? "YOU'RE NEXT" : `QUEUE #${queuePosition}`}
</span>
<span className={CAPTION}>
{eta > 5 ? `${etaFmt(eta)} until your turn` : "up any moment"}
</span>
<span className="font-pixel text-center text-[10px] uppercase text-neon-amber/85 sm:text-xs">
Do not close this tab
</span>
</div>
<button
onClick={leave}
aria-label="Leave the queue"
className={LEAVE_KEYCAP}
>
<span>LEAVE</span>
<span>QUEUE</span>
</button>
</div>
);
} else {
// no queue AND an arm is free (or arm state isn't known yet) → you drive immediately; only fall
// back to "JOIN QUEUE" when arms are known to be busy
const canTakeNow = queueCount === 0 && (freeArm || arms.length === 0);
const joinEta = soonestFreeS + queueCount * AVG_TURN_SECONDS;
content = (
<div className={PANEL}>
<button
onClick={join}
className="touch-control rounded-lg border-2 border-neon-green bg-neon-green/15 px-8 py-3 text-lg text-neon-green neon-box transition-transform hover:bg-neon-green/25 active:translate-y-0.5 sm:px-10 sm:py-4 sm:text-2xl"
>
{canTakeNow ? "CONTROL ARM" : "JOIN QUEUE"}
</button>
{canTakeNow ? (
<span className="font-pixel text-xs text-neon-green/90 sm:text-sm">ARM READY</span>
) : (
<span className="flex flex-col font-pixel text-xs leading-tight text-neon-green/90 sm:text-sm">
<span>{queueCount} IN QUEUE</span>
{joinEta > 5 && <span className="text-white/70">{etaFmt(joinEta)} wait</span>}
</span>
)}
</div>
);
}
return <div className="pointer-events-none flex w-full justify-center px-1 pb-6 sm:px-3">{content}</div>;
}