// Balance Board Panel
// Purpose: Shows exactly what the Bluetooth board is doing and its current total weight.
// Scope: Owns optional feature gating and the live weight-frame subscription only.
import { useEffect, useState } from 'react';
import { useSocket } from '../../context/SocketContext.jsx';
import { useSessionSelector } from '../../context/SessionContext.jsx';
import { isFeatureEnabled } from '../../lib/features.js';
import CardFrame from '../CardFrame/index.jsx';
const EMPTY_CORNERS = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0,
};
const EMPTY_FRAME = {
totalKg: 0,
batteryPercent: null,
// Null distinguishes "no live frame received yet" from a legitimate record
// of zero, allowing the persisted session value to remain visible while the
// socket room subscription is being established.
recordKg: null,
recordedAt: null,
corners: EMPTY_CORNERS,
};
function formatWeight(value) {
const weight = Number(value);
return Number.isFinite(weight) ? `${weight.toFixed(2)} kg` : '0.00 kg';
}
function finiteNumber(value) {
if (value == null || value === '') return null;
const number = Number(value);
return Number.isFinite(number) ? number : null;
}
function centerOfPressure(corners) {
const topLeft = Math.max(0, finiteNumber(corners.topLeft) || 0);
const topRight = Math.max(0, finiteNumber(corners.topRight) || 0);
const bottomLeft = Math.max(0, finiteNumber(corners.bottomLeft) || 0);
const bottomRight = Math.max(0, finiteNumber(corners.bottomRight) || 0);
const total = topLeft + topRight + bottomLeft + bottomRight;
// Only an exact zero stays centered because dividing by zero cannot produce a
// position. Every positive reading participates immediately, with no minimum
// weight or center deadzone hiding small shifts reported by the load cells.
if (total === 0) return { left: 50, top: 50, active: false };
const horizontal = ((topRight + bottomRight) - (topLeft + bottomLeft)) / total;
const vertical = ((bottomLeft + bottomRight) - (topLeft + topRight)) / total;
return {
left: 50 + Math.max(-1, Math.min(1, horizontal)) * 37,
top: 50 + Math.max(-1, Math.min(1, vertical)) * 37,
active: true,
};
}
function CornerReading({ className, label, value }) {
return (
{label}
{formatWeight(value)}
);
}
export default function BalanceBoardPanel() {
const enabled = useSessionSelector((state) => isFeatureEnabled(state, 'balanceBoard'));
// Keep feature ownership inside the component so layouts do not need special
// cases or empty wrappers when the optional hardware is disabled.
if (!enabled) return null;
return ;
}
function BalanceBoardPanelContent() {
const socket = useSocket();
const board = useSessionSelector((state) => state.session?.balanceBoard || null);
const role = useSessionSelector((state) => state.session?.role || null);
const [frame, setFrame] = useState(EMPTY_FRAME);
const [unpairing, setUnpairing] = useState(false);
const [zeroRequesting, setZeroRequesting] = useState(false);
const [resettingRecord, setResettingRecord] = useState(false);
useEffect(() => {
if (!socket) return undefined;
const handleFrame = (next = {}) => setFrame({ ...EMPTY_FRAME, ...next });
// Socket.IO room membership belongs to one server-side connection, not to
// the long-lived browser socket object. A brief network interruption gives
// the browser a new server-side socket while React keeps this component and
// this effect mounted, so subscribing only here would silently lose all
// later weight frames. Rejoin after every connection as well as immediately
// for the already-connected case.
const subscribe = () => {
socket.emit('balanceBoard:subscribe', {}, () => {});
};
socket.on('balanceBoard:frame', handleFrame);
socket.on('connect', subscribe);
subscribe();
return () => {
socket.off('balanceBoard:frame', handleFrame);
socket.off('connect', subscribe);
// The panel is the only consumer represented by this component. Leaving
// the room on unmount prevents an inactive route or tab from continuing
// to receive the board's continuous measurement stream.
socket.emit('balanceBoard:unsubscribe');
};
}, [socket]);
// Mask the previous reading immediately when disconnected. Keeping the last
// socket frame in state avoids effect-driven state resets and stale flashes.
const liveFrame = board?.connected ? frame : EMPTY_FRAME;
const corners = { ...EMPTY_CORNERS, ...(liveFrame.corners || {}) };
const center = centerOfPressure(corners);
const liveBattery = finiteNumber(liveFrame.batteryPercent);
const sessionBattery = finiteNumber(board?.batteryPercent);
const battery = liveBattery ?? sessionBattery;
// Live frames make a newly reached record move immediately. The session copy
// remains available while the board sleeps or before this panel subscribes,
// which is important because the record belongs to the installation rather
// than to one Bluetooth connection.
const liveRecord = board?.connected ? finiteNumber(frame.recordKg) : null;
const sessionRecord = finiteNumber(board?.recordKg);
const record = liveRecord ?? sessionRecord ?? 0;
const sleeping = board?.status === 'sleeping';
const isAdmin = role === 'admin' || role === 'lockdown';
const calibration = board?.calibration || null;
const zeroing = Boolean(calibration?.active);
const zero = () => {
if (zeroRequesting || zeroing || !board?.connected) return;
if (!window.confirm('Use the board’s current load as zero? Keep everything still for ten seconds.')) return;
setZeroRequesting(true);
socket.emit('balanceBoard:zero', {}, (response = {}) => {
setZeroRequesting(false);
if (response.error) window.alert(response.error);
});
};
const unpair = () => {
if (unpairing || !board?.paired) return;
if (!window.confirm('Unpair this Balance Board and require the red Sync button to pair it again?')) return;
setUnpairing(true);
socket.emit('balanceBoard:unpair', {}, (response = {}) => {
setUnpairing(false);
if (response.error) {
window.alert(response.error);
} else if (response.warning) {
window.alert('Board forgotten locally, but BlueZ reported a bond-removal warning.');
}
});
};
const resetRecord = () => {
if (resettingRecord) return;
if (!window.confirm('Reset the highest weight record?')) return;
setResettingRecord(true);
socket.emit('balanceBoard:resetRecord', {}, (response = {}) => {
setResettingRecord(false);
if (response.error) window.alert(response.error);
});
};
const actions = isAdmin ? (
) : null;
return (
{sleeping ? (
The Balance Board is asleep
Press the front power button on the board to wake it.
) : null}
{/* Keep the measurement column narrow and fixed so the board remains the
dominant visual while record and battery stay in one predictable
place. Both pieces use the shared dark panel treatment instead of
introducing a Balance Board-specific background style. */}