import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import { RootState } from '@/store'; import { sendEmergencyStop } from './emergencyStopControl'; import { ShieldAlert, Zap, Siren, BatteryWarning, MoveVertical, } from 'lucide-react'; export type RobotFeature = | 'emergencyStop' | 'response' | 'obstacleAvoidance' | 'lowBatteryReturn' | 'tiltProtection' | 'findCar' | 'autoback' | 'controlMode'; export interface FeatureConfig { key: RobotFeature; icon: React.ReactNode; activeColor: string; inactiveColor: string; } // 功能方块 const FEATURES: FeatureConfig[] = [ { key: 'controlMode', icon: , activeColor: '#0dab2d', inactiveColor: '#94a3b8' }, { key: 'obstacleAvoidance', icon: , activeColor: '#3b82f6', inactiveColor: '#94a3b8' }, { key: 'lowBatteryReturn', icon: , activeColor: '#f59e0b', inactiveColor: '#94a3b8' }, { key: 'tiltProtection', icon: , activeColor: '#10b981', inactiveColor: '#94a3b8' }, { key: 'autoback', icon: , activeColor: '#22c55e', inactiveColor: '#94a3b8' }, { key: 'findCar', icon: , activeColor: '#ef4444', inactiveColor: '#94a3b8' }, ]; // 急停方块 const EMERGENCY: FeatureConfig = { key: 'emergencyStop', icon: , activeColor: '#ef4444', inactiveColor: '#ef4444', }; interface RobotFeatureButtonsProps { activeFeatures?: RobotFeature[]; onToggle?: (feature: RobotFeature, enabled: boolean) => void; className?: string; showEmergencyStop?: boolean; sendFindCar?: () => void; sendAutoBack?: () => void; sendControlMode?: () => void; controlMode?: number | string; } const RobotFeatureButtons: React.FC = ({ activeFeatures = [], onToggle, className = '', showEmergencyStop = true, sendFindCar, sendAutoBack, sendControlMode, controlMode }) => { const { t } = useTranslation(); const { userInfo } = useSelector((state: RootState) => state.user); const [active, setActive] = useState>(new Set(activeFeatures)); // 临时按压闪烁标记 const [flashKey, setFlashKey] = useState(null); // 一次性触发,不toggle的按钮列表 const oneShotKeys: RobotFeature[] = ['emergencyStop', 'findCar', 'autoback', 'controlMode']; const list = showEmergencyStop ? [EMERGENCY, ...FEATURES] : FEATURES; const handleClick = (feature: RobotFeature) => { // 触发按压反馈 setFlashKey(feature); setTimeout(() => { setFlashKey(null); }, 200); // 一次性按钮:只发指令,不切换active状态 if (oneShotKeys.includes(feature)) { if (feature === 'emergencyStop') { sendEmergencyStop(userInfo); onToggle?.(feature, true); return; } if (feature === 'findCar') { sendFindCar?.(); } if (feature === 'autoback') { sendAutoBack?.(); } if (feature === 'controlMode') { sendControlMode?.(); } return; } // 开关类:保留toggle const next = !active.has(feature); const nextSet = new Set(active); if (next) nextSet.add(feature); else nextSet.delete(feature); setActive(nextSet); onToggle?.(feature, next); }; return (
{list.map((f) => { const isOneShot = oneShotKeys.includes(f.key); const isActive = active.has(f.key); const isFlashing = flashKey === f.key; const shouldGlow = f.key === 'controlMode' && controlMode != 3; return ( ); })}
); }; export default RobotFeatureButtons;