import { useState } from 'react'; import { InformationCircleIcon, CheckCircleIcon, BoltIcon, PauseCircleIcon, ExclamationTriangleIcon, WifiIcon, } from '@heroicons/react/24/solid'; import type { MachineInfo } from '../types/machine'; import { MachineStatus } from '../types/machine'; import { ConfirmDialog } from './ConfirmDialog'; import { shouldConfirmDisconnect, getStateVisualInfo } from '../utils/machineStateHelpers'; import { hasError, getErrorDetails } from '../utils/errorCodeHelpers'; interface MachineConnectionProps { isConnected: boolean; machineInfo: MachineInfo | null; machineStatus: MachineStatus; machineStatusName: string; machineError: number; onConnect: () => void; onDisconnect: () => void; onRefresh: () => void; } export function MachineConnection({ isConnected, machineInfo, machineStatus, machineStatusName, machineError, onConnect, onDisconnect, }: MachineConnectionProps) { const [showDisconnectConfirm, setShowDisconnectConfirm] = useState(false); const handleDisconnectClick = () => { if (shouldConfirmDisconnect(machineStatus)) { setShowDisconnectConfirm(true); } else { onDisconnect(); } }; const handleConfirmDisconnect = () => { setShowDisconnectConfirm(false); onDisconnect(); }; const stateVisual = getStateVisualInfo(machineStatus); // Map icon names to Heroicons const stateIcons = { ready: CheckCircleIcon, active: BoltIcon, waiting: PauseCircleIcon, complete: CheckCircleIcon, interrupted: PauseCircleIcon, error: ExclamationTriangleIcon, }; const statusBadgeColors = { idle: 'bg-cyan-100 dark:bg-cyan-900/30 text-cyan-800 dark:text-cyan-300', info: 'bg-cyan-100 dark:bg-cyan-900/30 text-cyan-800 dark:text-cyan-300', active: 'bg-yellow-100 dark:bg-yellow-900/30 text-yellow-800 dark:text-yellow-300', waiting: 'bg-yellow-100 dark:bg-yellow-900/30 text-yellow-800 dark:text-yellow-300', warning: 'bg-yellow-100 dark:bg-yellow-900/30 text-yellow-800 dark:text-yellow-300', complete: 'bg-green-100 dark:bg-green-900/30 text-green-800 dark:text-green-300', success: 'bg-green-100 dark:bg-green-900/30 text-green-800 dark:text-green-300', interrupted: 'bg-red-100 dark:bg-red-900/30 text-red-800 dark:text-red-300', error: 'bg-red-100 dark:bg-red-900/30 text-red-800 dark:text-red-300', danger: 'bg-red-100 dark:bg-red-900/30 text-red-800 dark:text-red-300', }; // Only show error info when connected AND there's an actual error const errorInfo = (isConnected && hasError(machineError)) ? getErrorDetails(machineError) : null; return ( <> {!isConnected ? (

Machine

Ready to connect

) : (

Machine Info

{machineInfo?.modelNumber || 'Brother Embroidery Machine'}

{/* Error/Info Display */} {errorInfo && ( errorInfo.isInformational ? (
{errorInfo.title}
) : (
⚠️
{errorInfo.title}
Error Code: 0x{machineError.toString(16).toUpperCase().padStart(2, '0')}
) )} {/* Status Badge */}
Status: {(() => { const Icon = stateIcons[stateVisual.iconName]; return ; })()} {machineStatusName}
{/* Machine Info */} {machineInfo && (
Max Area {(machineInfo.maxWidth / 10).toFixed(1)} × {(machineInfo.maxHeight / 10).toFixed(1)} mm
{machineInfo.totalCount !== undefined && (
Total Stitches {machineInfo.totalCount.toLocaleString()}
)}
)}
)} setShowDisconnectConfirm(false)} variant="danger" /> ); }