2026-10-12 12:42:16 +08:00
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2026-10-15 14:02:02 +08:00
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
|
import { RootState } from '@/store';
|
|
|
|
|
|
import { sendEmergencyStop } from './emergencyStopControl';
|
2026-10-12 12:42:16 +08:00
|
|
|
|
import {
|
|
|
|
|
|
ShieldAlert,
|
|
|
|
|
|
Zap,
|
|
|
|
|
|
Siren,
|
|
|
|
|
|
BatteryWarning,
|
|
|
|
|
|
MoveVertical,
|
|
|
|
|
|
} from 'lucide-react';
|
|
|
|
|
|
|
|
|
|
|
|
export type RobotFeature =
|
|
|
|
|
|
| 'emergencyStop'
|
|
|
|
|
|
| 'response'
|
|
|
|
|
|
| 'obstacleAvoidance'
|
|
|
|
|
|
| 'lowBatteryReturn'
|
2026-10-13 17:22:47 +08:00
|
|
|
|
| 'tiltProtection'
|
|
|
|
|
|
| 'findCar'
|
|
|
|
|
|
| 'autoback';
|
|
|
|
|
|
|
2026-10-12 12:42:16 +08:00
|
|
|
|
export interface FeatureConfig {
|
|
|
|
|
|
key: RobotFeature;
|
|
|
|
|
|
icon: React.ReactNode;
|
|
|
|
|
|
activeColor: string;
|
|
|
|
|
|
inactiveColor: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-21 14:51:27 +08:00
|
|
|
|
// 功能方块:方块样式展示,不再包含急停(急停已移至方向盘中键)
|
2026-10-12 12:42:16 +08:00
|
|
|
|
const FEATURES: FeatureConfig[] = [
|
2026-10-21 14:51:27 +08:00
|
|
|
|
{ key: 'obstacleAvoidance', icon: <Siren className="w-6 h-6" />, activeColor: '#3b82f6', inactiveColor: '#94a3b8' },
|
|
|
|
|
|
{ key: 'lowBatteryReturn', icon: <BatteryWarning className="w-6 h-6" />, activeColor: '#f59e0b', inactiveColor: '#94a3b8' },
|
|
|
|
|
|
{ key: 'tiltProtection', icon: <MoveVertical className="w-6 h-6" />, activeColor: '#10b981', inactiveColor: '#94a3b8' },
|
|
|
|
|
|
{ key: 'autoback', icon: <Zap className="w-6 h-6" />, activeColor: '#22c55e', inactiveColor: '#94a3b8' },
|
|
|
|
|
|
{ key: 'findCar', icon: <ShieldAlert className="w-6 h-6" />, activeColor: '#ef4444', inactiveColor: '#94a3b8' },
|
2026-10-12 12:42:16 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
2026-10-21 14:51:27 +08:00
|
|
|
|
// 急停方块(仅在 showEmergencyStop 时展示,兼容其他页面)
|
|
|
|
|
|
const EMERGENCY: FeatureConfig = {
|
|
|
|
|
|
key: 'emergencyStop',
|
|
|
|
|
|
icon: <ShieldAlert className="w-6 h-6" />,
|
|
|
|
|
|
activeColor: '#ef4444',
|
|
|
|
|
|
inactiveColor: '#ef4444',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-10-12 12:42:16 +08:00
|
|
|
|
interface RobotFeatureButtonsProps {
|
|
|
|
|
|
activeFeatures?: RobotFeature[];
|
|
|
|
|
|
onToggle?: (feature: RobotFeature, enabled: boolean) => void;
|
|
|
|
|
|
className?: string;
|
2026-10-21 14:51:27 +08:00
|
|
|
|
showEmergencyStop?: boolean;
|
|
|
|
|
|
sendFindCar: () => {}
|
2026-10-12 12:42:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const RobotFeatureButtons: React.FC<RobotFeatureButtonsProps> = ({
|
|
|
|
|
|
activeFeatures = [],
|
|
|
|
|
|
onToggle,
|
|
|
|
|
|
className = '',
|
2026-10-21 14:51:27 +08:00
|
|
|
|
showEmergencyStop = true,
|
|
|
|
|
|
sendFindCar,
|
2026-10-12 12:42:16 +08:00
|
|
|
|
}) => {
|
|
|
|
|
|
const { t } = useTranslation();
|
2026-10-15 14:02:02 +08:00
|
|
|
|
const { userInfo } = useSelector((state: RootState) => state.user);
|
2026-10-12 12:42:16 +08:00
|
|
|
|
const [active, setActive] = useState<Set<RobotFeature>>(new Set(activeFeatures));
|
|
|
|
|
|
|
2026-10-21 14:51:27 +08:00
|
|
|
|
const list = showEmergencyStop ? [EMERGENCY, ...FEATURES] : FEATURES;
|
|
|
|
|
|
|
2026-10-12 12:42:16 +08:00
|
|
|
|
const handleClick = (feature: RobotFeature) => {
|
2026-10-21 14:51:27 +08:00
|
|
|
|
// 急停:点击时直接下发 button7(绕过控制权限校验)
|
|
|
|
|
|
if (feature === 'emergencyStop') {
|
|
|
|
|
|
sendEmergencyStop(userInfo);
|
|
|
|
|
|
onToggle?.(feature, true);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 寻车:下发指令的同时,与其他功能一样切换选中状态(toggle 效果)
|
|
|
|
|
|
if (feature === 'findCar') {
|
|
|
|
|
|
sendFindCar?.();
|
|
|
|
|
|
}
|
2026-10-12 12:42:16 +08:00
|
|
|
|
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 (
|
2026-10-21 14:51:27 +08:00
|
|
|
|
<div className={`flex gap-2 w-full ${className}`}>
|
|
|
|
|
|
{list.map((f) => {
|
2026-10-12 12:42:16 +08:00
|
|
|
|
const isActive = active.has(f.key);
|
|
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={f.key}
|
|
|
|
|
|
onClick={() => handleClick(f.key)}
|
2026-10-21 14:51:27 +08:00
|
|
|
|
className="flex flex-col items-center justify-center gap-1.5 rounded-lg border transition-all duration-200 select-none hover:shadow-md"
|
2026-10-12 12:42:16 +08:00
|
|
|
|
style={{
|
2026-10-21 14:51:27 +08:00
|
|
|
|
aspectRatio: '1 / 1',
|
|
|
|
|
|
height: 64,
|
2026-10-12 12:42:16 +08:00
|
|
|
|
color: isActive ? f.activeColor : f.inactiveColor,
|
2026-10-21 14:51:27 +08:00
|
|
|
|
backgroundColor: isActive ? `${f.activeColor}1a` : '#fafafa',
|
|
|
|
|
|
borderColor: isActive ? f.activeColor : '#e5e7eb',
|
2026-10-12 12:42:16 +08:00
|
|
|
|
boxShadow: isActive ? `0 0 12px ${f.activeColor}33` : 'none',
|
|
|
|
|
|
}}
|
|
|
|
|
|
title={t(`devices.features.${f.key}`)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<span className="flex items-center justify-center">{f.icon}</span>
|
2026-10-21 14:51:27 +08:00
|
|
|
|
<span className="text-xs font-medium text-center leading-tight px-1" style={{ color: isActive ? f.activeColor : '#595959' }}>
|
2026-10-12 12:42:16 +08:00
|
|
|
|
{t(`devices.features.${f.key}`)}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
})}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default RobotFeatureButtons;
|