Files
gofly-platform-services/zlt-web/yingyan/src/pages/client/hooks/useNavigationState.ts
T

137 lines
4.1 KiB
TypeScript
Raw Normal View History

2026-03-27 18:31:21 +08:00
/**
* 沉浸模式状态机
* 管理 sailing / preview / idle 三态切换及相关操作回调
*/
import { useState, useCallback } from 'react';
import { Modal, message } from 'antd';
import { sendBoatCommand } from '../services/clientApi';
type ActiveRouteMode = 'sailing' | 'preview' | null;
interface ActiveRouteState {
mode: ActiveRouteMode;
routeId: string | null;
}
interface UseNavigationStateReturn {
activeRoute: ActiveRouteState;
isPaused: boolean;
currentWaypointIndex: number;
previewStopSignal: number;
isSailing: boolean;
isPreviewing: boolean;
isImmersive: boolean;
activeRouteId: string | null;
handleStartSailing: (routeId: string) => void;
handlePauseSailing: () => void;
handleResumeSailing: () => void;
handleStopSailing: () => void;
handleStopPreview: () => void;
handleEmergencyStop: () => void;
handlePreviewChange: (previewing: boolean) => void;
}
export function useNavigationState(
deviceSn: string,
selectedRouteId: string | null,
setSelectedRouteId: (id: string | null) => void,
): UseNavigationStateReturn {
const [activeRoute, setActiveRoute] = useState<ActiveRouteState>({ mode: null, routeId: null });
const [isPaused, setIsPaused] = useState(false);
const [currentWaypointIndex, setCurrentWaypointIndex] = useState(0);
const [previewStopSignal, setPreviewStopSignal] = useState(0);
const isSailing = activeRoute.mode === 'sailing';
const isPreviewing = activeRoute.mode === 'preview';
const isImmersive = activeRoute.mode !== null;
const activeRouteId = activeRoute.routeId;
const handleStartSailing = useCallback((routeId: string) => {
if (activeRoute.mode === 'preview') {
setPreviewStopSignal((prev) => prev + 1);
}
setActiveRoute({ mode: 'sailing', routeId });
setSelectedRouteId(routeId);
setIsPaused(false);
setCurrentWaypointIndex(0);
}, [activeRoute.mode, setSelectedRouteId]);
const handlePauseSailing = useCallback(() => {
if (!isSailing) return;
setIsPaused(true);
sendBoatCommand(deviceSn, 'pause', 'press').catch(() => {
message.error('暂停指令发送失败');
});
}, [deviceSn, isSailing]);
const handleResumeSailing = useCallback(() => {
if (!isSailing) return;
setIsPaused(false);
sendBoatCommand(deviceSn, 'resume', 'press').catch(() => {
message.error('继续指令发送失败');
});
}, [deviceSn, isSailing]);
const handleStopSailing = useCallback(() => {
Modal.confirm({
title: '确认结束航行',
content: '结束后将退��导航模式,返回线路选择',
okText: '结束航行',
cancelText: '取消',
onOk: () => {
setActiveRoute({ mode: null, routeId: null });
setIsPaused(false);
setCurrentWaypointIndex(0);
sendBoatCommand(deviceSn, 'stop', 'press').catch(() => {});
},
});
}, [deviceSn]);
const handleStopPreview = useCallback(() => {
setPreviewStopSignal((prev) => prev + 1);
}, []);
const handleEmergencyStop = useCallback(() => {
setActiveRoute({ mode: null, routeId: null });
setIsPaused(false);
setCurrentWaypointIndex(0);
sendBoatCommand(deviceSn, 'stop', 'press').catch(() => {
message.error('紧急停靠指令发送失败');
});
message.warning('已执行紧急停靠');
}, [deviceSn]);
const handlePreviewChange = useCallback((previewing: boolean) => {
setActiveRoute((prev) => {
if (previewing) {
if (prev.mode === 'sailing') return prev;
const nextRouteId = selectedRouteId ?? prev.routeId;
if (!nextRouteId) return prev;
return { mode: 'preview', routeId: nextRouteId };
}
if (prev.mode === 'preview') {
return { mode: null, routeId: null };
}
return prev;
});
}, [selectedRouteId]);
return {
activeRoute,
isPaused,
currentWaypointIndex,
previewStopSignal,
isSailing,
isPreviewing,
isImmersive,
activeRouteId,
handleStartSailing,
handlePauseSailing,
handleResumeSailing,
handleStopSailing,
handleStopPreview,
handleEmergencyStop,
handlePreviewChange,
};
}