188 lines
5.9 KiB
TypeScript
188 lines
5.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'
|
||
| 'controlMode'
|
||
| 'personFollow';
|
||
|
||
export interface FeatureConfig {
|
||
key: RobotFeature;
|
||
icon: React.ReactNode;
|
||
activeColor: string;
|
||
inactiveColor: string;
|
||
}
|
||
|
||
// 功能方块
|
||
const FEATURES: FeatureConfig[] = [
|
||
{ key: 'controlMode', icon: <ShieldAlert className="w-6 h-6" />, activeColor: '#0dab2d', inactiveColor: '#94a3b8' },
|
||
|
||
//{ 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' },
|
||
{ key: 'personFollow', icon: <MoveVertical className="w-6 h-6" />, activeColor: '#10b981', inactiveColor: '#94a3b8' },
|
||
];
|
||
|
||
// 急停方块
|
||
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?: () => void;
|
||
sendAutoBack?: () => void;
|
||
sendControlMode?: () => void;
|
||
deviceId?: string;
|
||
controlMode?: number | string;
|
||
}
|
||
|
||
const RobotFeatureButtons: React.FC<RobotFeatureButtonsProps> = ({
|
||
activeFeatures = [],
|
||
onToggle,
|
||
className = '',
|
||
showEmergencyStop = true,
|
||
sendFindCar,
|
||
sendAutoBack,
|
||
sendControlMode,
|
||
controlMode,
|
||
deviceId
|
||
}) => {
|
||
const { t } = useTranslation();
|
||
const { userInfo } = useSelector((state: RootState) => state.user);
|
||
const [active, setActive] = useState<Set<RobotFeature>>(new Set(activeFeatures));
|
||
// 临时按压闪烁标记
|
||
const [flashKey, setFlashKey] = useState<RobotFeature | null>(null);
|
||
|
||
// 一次性触发,不toggle的按钮列表
|
||
const oneShotKeys: RobotFeature[] = ['emergencyStop', 'findCar', 'autoback', 'controlMode', 'personFollow'];
|
||
|
||
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(deviceId || "");
|
||
onToggle?.(feature, true);
|
||
return;
|
||
}
|
||
if (feature === 'findCar') {
|
||
sendFindCar?.();
|
||
}
|
||
if (feature === 'autoback') {
|
||
sendAutoBack?.();
|
||
}
|
||
if (feature === 'controlMode') {
|
||
sendControlMode?.(3);
|
||
}
|
||
if (
|
||
feature === 'personFollow'
|
||
) sendControlMode?.(4);
|
||
|
||
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 (
|
||
<div className={`flex w-full ${className} flex-wrap content-start items-start gap-x-2 gap-y-2 justify-start self-start`}>
|
||
<style>
|
||
{`
|
||
@keyframes controlModeGlow {
|
||
0% { box-shadow: 0 0 5px rgba(13, 171, 45, 0.4); border-color: rgba(13, 171, 45, 0.4); }
|
||
50% { box-shadow: 0 0 15px rgba(13, 171, 45, 0.8); border-color: rgba(13, 171, 45, 0.8); }
|
||
100% { box-shadow: 0 0 5px rgba(13, 171, 45, 0.4); border-color: rgba(13, 171, 45, 0.4); }
|
||
}
|
||
.control-mode-glow {
|
||
animation: controlModeGlow 1.5s infinite ease-in-out;
|
||
}
|
||
`}
|
||
</style>
|
||
{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 (
|
||
<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-150 select-none hover:shadow-md ${shouldGlow ? 'control-mode-glow' : ''}`}
|
||
style={{
|
||
aspectRatio: '1 / 1',
|
||
height: 64,
|
||
// 一次性按钮基础样式 + 按压变暗
|
||
color: isOneShot
|
||
? (isFlashing ? '#333' : f.inactiveColor)
|
||
: (isActive ? f.activeColor : f.inactiveColor),
|
||
backgroundColor: isOneShot
|
||
? (isFlashing ? '#eaeaea' : '#fafafa')
|
||
: (isActive ? `${f.activeColor}1a` : '#fafafa'),
|
||
borderColor: isOneShot
|
||
? (isFlashing ? '#ccc' : (shouldGlow ? f.activeColor : '#e5e7eb'))
|
||
: (isActive ? f.activeColor : '#e5e7eb'),
|
||
boxShadow: isOneShot
|
||
? (isFlashing ? 'inset 0 1px 3px rgba(0,0,0,0.15)' : (shouldGlow ? `0 0 10px ${f.activeColor}66` : 'none'))
|
||
: (isActive ? `0 0 12px ${f.activeColor}33` : 'none'),
|
||
cursor: 'pointer',
|
||
transform: isFlashing ? 'scale(0.97)' : 'scale(1)',
|
||
}}
|
||
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: isOneShot
|
||
? (isFlashing ? '#222' : '#595959')
|
||
: (isActive ? f.activeColor : '#595959')
|
||
}}>
|
||
{t(`devices.features.${f.key}`)}
|
||
</span>
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default RobotFeatureButtons;
|