124 lines
3.9 KiB
TypeScript
124 lines
3.9 KiB
TypeScript
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';
|
||
|
||
export interface FeatureConfig {
|
||
key: RobotFeature;
|
||
icon: React.ReactNode;
|
||
activeColor: string;
|
||
inactiveColor: string;
|
||
}
|
||
|
||
// 功能方块:方块样式展示,不再包含急停(急停已移至方向盘中键)
|
||
const FEATURES: FeatureConfig[] = [
|
||
{ 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' },
|
||
];
|
||
|
||
// 急停方块(仅在 showEmergencyStop 时展示,兼容其他页面)
|
||
const EMERGENCY: FeatureConfig = {
|
||
key: 'emergencyStop',
|
||
icon: <ShieldAlert className="w-6 h-6" />,
|
||
activeColor: '#ef4444',
|
||
inactiveColor: '#ef4444',
|
||
};
|
||
|
||
interface RobotFeatureButtonsProps {
|
||
activeFeatures?: RobotFeature[];
|
||
onToggle?: (feature: RobotFeature, enabled: boolean) => void;
|
||
className?: string;
|
||
showEmergencyStop?: boolean;
|
||
sendFindCar: () => {};
|
||
sendAutoBack?: () => {};
|
||
}
|
||
|
||
const RobotFeatureButtons: React.FC<RobotFeatureButtonsProps> = ({
|
||
activeFeatures = [],
|
||
onToggle,
|
||
className = '',
|
||
showEmergencyStop = true,
|
||
sendFindCar,
|
||
sendAutoBack
|
||
}) => {
|
||
const { t } = useTranslation();
|
||
const { userInfo } = useSelector((state: RootState) => state.user);
|
||
const [active, setActive] = useState<Set<RobotFeature>>(new Set(activeFeatures));
|
||
|
||
const list = showEmergencyStop ? [EMERGENCY, ...FEATURES] : FEATURES;
|
||
|
||
const handleClick = (feature: RobotFeature) => {
|
||
// 急停:点击时直接下发 button7(绕过控制权限校验)
|
||
if (feature === 'emergencyStop') {
|
||
sendEmergencyStop(userInfo);
|
||
onToggle?.(feature, true);
|
||
return;
|
||
}
|
||
// 寻车:下发指令的同时,与其他功能一样切换选中状态(toggle 效果)
|
||
if (feature === 'findCar') {
|
||
sendFindCar?.();
|
||
}
|
||
if (feature === 'autoback') {
|
||
sendAutoBack?.();
|
||
}
|
||
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 (
|
||
<div className={`flex gap-2 w-full ${className}`}>
|
||
{list.map((f) => {
|
||
const isActive = active.has(f.key);
|
||
return (
|
||
<button
|
||
key={f.key}
|
||
onClick={() => handleClick(f.key)}
|
||
className="flex flex-col items-center justify-center gap-1.5 rounded-lg border transition-all duration-200 select-none hover:shadow-md"
|
||
style={{
|
||
aspectRatio: '1 / 1',
|
||
height: 64,
|
||
color: isActive ? f.activeColor : f.inactiveColor,
|
||
backgroundColor: isActive ? `${f.activeColor}1a` : '#fafafa',
|
||
borderColor: isActive ? f.activeColor : '#e5e7eb',
|
||
boxShadow: isActive ? `0 0 12px ${f.activeColor}33` : 'none',
|
||
cursor: 'pointer',
|
||
}}
|
||
title={t(`devices.features.${f.key}`)}
|
||
>
|
||
<span className="flex items-center justify-center">{f.icon}</span>
|
||
<span className="text-xs font-medium text-center leading-tight px-1" style={{ color: isActive ? f.activeColor : '#595959' }}>
|
||
{t(`devices.features.${f.key}`)}
|
||
</span>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default RobotFeatureButtons;
|